Skip to content

Instantly share code, notes, and snippets.

@Animesh-Ghosh
Created November 19, 2022 04:17
Show Gist options
  • Select an option

  • Save Animesh-Ghosh/e63558a57cd61c36cc7f1a4f67a687fd to your computer and use it in GitHub Desktop.

Select an option

Save Animesh-Ghosh/e63558a57cd61c36cc7f1a4f67a687fd to your computer and use it in GitHub Desktop.
Tests that got me a job 😎
'''Test for Payment Processor.
Author: Animesh Ghosh
'''
import unittest
from unittest.mock import patch
from io import StringIO
from payment_processor import PaymentProcessor
class TestPaymentProcessor(unittest.TestCase):
def setUp(self):
self.payment_processor = PaymentProcessor()
def test_add_item_to_process_invalid_type(self):
self.assertRaises(TypeError, self.payment_processor.add_item_to_process, True)
self.assertRaises(TypeError, self.payment_processor.add_item_to_process, 3.14)
self.assertRaises(TypeError, self.payment_processor.add_item_to_process, 42)
def test_add_item_to_process_unsupported_item(self):
self.assertRaises(
ValueError, self.payment_processor.add_item_to_process, 'oven'
)
self.assertRaises(
ValueError, self.payment_processor.add_item_to_process, 'fridge'
)
@patch('sys.stdout', new_callable=StringIO)
def test_process_items_without_conditional_rules(self, mock_stdout):
self.payment_processor.add_item_to_process('membership')
self.payment_processor.add_item_to_process('stove')
self.payment_processor.add_item_to_process('digital_item')
expected_output = '''Upgrading the user to "pro" account.
Printing "safety instructions"
Sending email to download digital items.
'''
self.payment_processor.process_items()
self.assertMultiLineEqual(mock_stdout.getvalue(), expected_output)
@patch('sys.stdout', new_callable=StringIO)
@patch('builtins.input')
def test_process_items_book_conditional_rule_yes(self, mock_input, mock_stdout):
mock_input.return_value = 'Y'
self.payment_processor.add_item_to_process('book')
expected_output = '''Printing mailing label.
Sending commission to the agent.
'''
self.payment_processor.process_items()
self.assertMultiLineEqual(mock_stdout.getvalue(), expected_output)
@patch('sys.stdout', new_callable=StringIO)
@patch('builtins.input')
def test_process_items_book_conditional_rule_no(self, mock_input, mock_stdout):
mock_input.return_value = 'n'
self.payment_processor.add_item_to_process('book')
expected_output = '''Printing mailing label.
'''
self.payment_processor.process_items()
self.assertMultiLineEqual(mock_stdout.getvalue(), expected_output)
@patch('sys.stdout', new_callable=StringIO)
@patch('builtins.input')
def test_process_items(self, mock_input, mock_stdout):
mock_input.return_value = 'Y'
self.payment_processor.add_item_to_process('book')
self.payment_processor.add_item_to_process('membership')
self.payment_processor.add_item_to_process('stove')
self.payment_processor.add_item_to_process('digital_item')
expected_output = '''Printing mailing label.
Sending commission to the agent.
Upgrading the user to "pro" account.
Printing "safety instructions"
Sending email to download digital items.
'''
self.payment_processor.process_items()
self.assertMultiLineEqual(mock_stdout.getvalue(), expected_output)
'''Tests for the ShoppingCart class.
Author: Animesh Ghosh
'''
import unittest
from shopping_cart import ShoppingCart
class TestShoppingCart(unittest.TestCase):
def setUp(self):
self.cart = ShoppingCart()
def test_add_item_incorrect_type(self):
self.assertRaises(TypeError, self.cart.add_item, True)
self.assertRaises(TypeError, self.cart.add_item, 3.75)
self.assertRaises(TypeError, self.cart.add_item, 4)
def test_add_item_unsupported_item(self):
self.assertRaises(ValueError, self.cart.add_item, 'orange')
self.assertRaises(ValueError, self.cart.add_item, 'butter')
def test_get_total_price(self):
# milk,milk, bread,banana,bread,bread,bread,milk,apple
self.cart.add_item('milk')
self.cart.add_item('milk')
self.cart.add_item('bread')
self.cart.add_item('banana')
self.cart.add_item('bread')
self.cart.add_item('bread')
self.cart.add_item('bread')
self.cart.add_item('milk')
self.cart.add_item('apple')
self.assertAlmostEqual(self.cart.get_total_price(), 19.02)
def test_get_amount_saved(self):
# milk,milk, bread,banana,bread,bread,bread,milk,apple
self.cart.add_item('milk')
self.cart.add_item('milk')
self.cart.add_item('bread')
self.cart.add_item('banana')
self.cart.add_item('bread')
self.cart.add_item('bread')
self.cart.add_item('bread')
self.cart.add_item('milk')
self.cart.add_item('apple')
self.assertAlmostEqual(self.cart.get_amount_saved(), 3.45)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment