|
| 1 | +import unittest |
| 2 | +import main as c |
| 3 | + |
| 4 | + |
| 5 | +class TestOnlineStore(unittest.TestCase): |
| 6 | + # Example 1 - shows a valid and successful payment for a tv |
| 7 | + def test_1(self): |
| 8 | + tv_item = c.Item(type="product", description="tv", amount=1000.00, quantity=1) |
| 9 | + payment = c.Item( |
| 10 | + type="payment", description="invoice_1", amount=1000.00, quantity=1 |
| 11 | + ) |
| 12 | + order_1 = c.Order(id="1", items=[payment, tv_item]) |
| 13 | + self.assertEqual(c.validorder(order_1), "Order ID: 1 - Full payment received!") |
| 14 | + |
| 15 | + # Example 2 - successfully detects payment imbalance as tv was never paid |
| 16 | + def test_2(self): |
| 17 | + tv_item = c.Item(type="product", description="tv", amount=1000.00, quantity=1) |
| 18 | + order_2 = c.Order(id="2", items=[tv_item]) |
| 19 | + self.assertEqual( |
| 20 | + c.validorder(order_2), "Order ID: 2 - Payment imbalance: $-1000.00" |
| 21 | + ) |
| 22 | + |
| 23 | + # Example 3 - successfully reimburses client for a return so payment imbalance exists |
| 24 | + def test_3(self): |
| 25 | + tv_item = c.Item(type="product", description="tv", amount=1000.00, quantity=1) |
| 26 | + payment = c.Item( |
| 27 | + type="payment", description="invoice_3", amount=1000.00, quantity=1 |
| 28 | + ) |
| 29 | + payback = c.Item( |
| 30 | + type="payment", description="payback_3", amount=-1000.00, quantity=1 |
| 31 | + ) |
| 32 | + order_3 = c.Order(id="3", items=[payment, tv_item, payback]) |
| 33 | + self.assertEqual( |
| 34 | + c.validorder(order_3), "Order ID: 3 - Payment imbalance: $-1000.00" |
| 35 | + ) |
| 36 | + |
| 37 | + # Example 4 - handles invalid input such as placing an invalid order for 1.5 device |
| 38 | + def test_4(self): |
| 39 | + tv = c.Item(type="product", description="tv", amount=1000, quantity=1.5) |
| 40 | + order_1 = c.Order(id="1", items=[tv]) |
| 41 | + try: |
| 42 | + c.validorder(order_1) |
| 43 | + except: |
| 44 | + self.fail("Invalid order detected") |
| 45 | + |
| 46 | + # Example 5 - handles an invalid item type called 'service' |
| 47 | + def test_5(self): |
| 48 | + service = c.Item( |
| 49 | + type="service", description="order shipment", amount=100, quantity=1 |
| 50 | + ) |
| 51 | + order_1 = c.Order(id="1", items=[service]) |
| 52 | + self.assertEqual(c.validorder(order_1), "Invalid item type: service") |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + unittest.main() |
0 commit comments