Advertisement
ugochukwu15

Untitled

Jul 26th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. import unittest;
  2.  
  3. class Test(unittest.TestCase):
  4.     def setUp(self):
  5.         self.cart = ShoppingCart()
  6.         self.shop = Shop()
  7.  
  8.     def test_cart_property_initialization(self):
  9.         self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
  10.         self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')
  11.  
  12.     def test_add_item(self):
  13.         self.cart.add_item('Mango', 3, 10)
  14.         self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
  15.         self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')
  16.  
  17.     def test_add_item_hidden(self):
  18.         self.cart.add_item('Mango', 3, 10)
  19.         self.cart.add_item('Orange', 16, 10)
  20.         self.assertEqual(self.cart.total, 190, msg='Cart total not correct after adding items')
  21.         self.assertEqual(self.cart.items['Orange'], 16, msg='Quantity of items not correct after adding item')
  22.  
  23.     def test_remove_item(self):
  24.         self.cart.add_item('Mango', 3, 10)
  25.         self.cart.remove_item('Mango', 2, 10)
  26.         self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
  27.         self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
  28.  
  29.     def test_remove_item_hidden(self):
  30.         self.cart.add_item('Mango', 3, 10)
  31.         self.cart.add_item('Orange', 16, 10)
  32.         self.cart.remove_item('Mango', 2, 10)
  33.         self.assertEqual(self.cart.total, 170, msg='Cart total not correct after removing item')
  34.         self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
  35.         self.cart.remove_item('Mango', 2, 10)
  36.         self.assertEqual(self.cart.total, 160, msg='Cart total not correct after removing item')
  37.         with self.assertRaises(KeyError):
  38.             self.cart.items['Mango']
  39.  
  40.     def test_checkout_returns_correct_value(self):
  41.         self.cart.add_item('Mango', 3, 10)
  42.         self.cart.add_item('Orange', 16, 10)
  43.         self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
  44.         self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')
  45.  
  46.  
  47.     def test_shop_is_instance_of_shopping_cart(self):
  48.         self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')
  49.  
  50.     def test_shop_initializaton(self):
  51.         self.assertEqual(self.shop.quantity, 100, msg='Shop quantity not initialized correctly')
  52.  
  53.     def test_shop_remove_item_method(self):
  54.         for i in range(15):
  55.             self.shop.remove_item()
  56.  
  57.         self.assertEqual(self.shop.quantity, 85)
  58.  
  59.  
  60.  
  61. class ShoppingCart(object):
  62.   def __init__(self):
  63.       self.total = 0
  64.       self.items = dict()
  65.      
  66.   def add_item(self, item_name, quantity, price):
  67.       self.item_name = item_name
  68.       self.quantity = int(quantity)
  69.       self.price = int(price)
  70.       if item_name not in self.items:
  71.         self.items[item_name] = quantity
  72.        
  73.       self.total +=  int(quantity * price)
  74.    
  75.   def remove_item(self, item_name, quantity, price):
  76.     self.item_name = item_name
  77.     self.quantity = int(quantity)
  78.     self.price = int(price)
  79.     if item_name in self.items:
  80.       if quantity > self.items[item_name]:
  81.         self.total -= (self.items[item_name] * price)
  82.         del self.items[item_name]
  83.       else:
  84.         self.items[item_name] -= int(quantity)
  85.         self.total -= int(quantity * price)
  86.      
  87.    
  88.      
  89.  
  90.   def checkout(self, cash_paid):
  91.     self.cash_paid = cash_paid 
  92.     if self.total > cash_paid:
  93.       return "Cash paid not enough"
  94.     else:
  95.       return cash_paid - self.total
  96.  
  97.  
  98. class Shop(ShoppingCart):
  99.     def __init__(self):
  100.         self.quantity = 100
  101.     def remove_item(self):
  102.         self.quantity -= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement