def main():
    cart = ShoppingCart()
    cart.add_item()
    cart.remove_item()
    cart.add_item()
    cart.add_item()
    cart.add_item()
    cart.remove_item()
    cart.checkout()
    cart.paid_for()
    cart.add_item()
    cart.checkout()
    cart.paid_for()
def main():
    print("====> first cart")
    cart = ShoppingCart()
    cart.add_item()
    cart.remove_item()
    cart.add_item()
    cart.add_item()
    cart.add_item()
    cart.remove_item()
    cart.checkout()
    cart.pay()

    # Go shopping again
    print("====> second cart")
    cart = ShoppingCart()
    cart.add_item()
    cart.add_item()
    cart.checkout()
    cart.empty_cart()
    cart.add_item()
    cart.checkout()
    cart.pay()

    # Try to add another item
    print('====> Expect an error here.')
    cart.add_item()
class ShoppinCartTestCase(unittest.TestCase):
    """Testing the ShoppingCart """
    def setUp(self):
        """
        Setting up the instance and assigning attributes for testing
        """
        self.my_cart = ShoppingCart()
        self.my_cart_2 = Shop()

    def test_add_item_total(self):
        """Test if method adds items and updates total accordingly"""
        my_total = self.my_cart.add_item('Cookies', 2, 400)
        self.assertEqual(my_total, 800)

#    def test_add_item_entry(self):
#        """Test if items dictionary is updated after adding an item"""
#        self.assertDictEqual(self.my_cart.items,{'Cookies':2},msg="Invalid dictionary")

    def test_remove_item_total(self):
        """Test whether total cash is updated after removing an item"""
        self.my_cart.add_item('Chocolate', 5, 300)
        self.my_cart.remove_item('Chocolate', 2, 300)
        self.assertEqual(self.my_cart.total,
                         900,
                         msg='Remove item method inaccurate')

    def test_remove_item_entry(self):
        """Tests whether items dictionary is updated after removing an item"""
        self.my_cart.add_item('Chocolate', 5, 300)
        self.my_cart.remove_item('Chocolate', 2, 300)
        self.assertDictEqual(self.my_cart.items, {'Chocolate': 3},
                             msg="Invalid dictionary")

    def test_exceeding_current_quantity(self):
        """Tests that all entries of an item are to be removed if quantity exeeds current."""
        self.my_cart.add_item('Chocolate', 5, 300)
        self.my_cart.remove_item('Chocolate', 7, 300)
        self.assertDictEqual(
            self.my_cart.items, {},
            msg="Doesn't remove all entries if quantity exeeds the current")

    def test_checkout(self):
        """Tests balance after payment"""
        self.my_cart.add_item('Chocolate', 2, 300)
        self.assertEqual(self.my_cart.checkout(700), 100)

    def test_checkout_not_enough(self):
        """Tests if payment is enough """
        self.my_cart.add_item('Chocolate', 2, 300)
        self.assertEqual(self.my_cart.checkout(100), "Cash paid not enough")

    def test_shop_if_subclass(self):
        """Tests whether Shop is a subclass of ShoppingCart"""
        self.assertTrue(issubclass(Shop, ShoppingCart),
                        msg="Shop doesn't inherit from ShoppingCart")

    def test_shop_quantity(self):
        """Tests whether Shop quantity attribute is initialized to 100"""
        self.assertEqual(self.my_cart_2.quantity,
                         100,
                         msg="Shop quantity attribute not equal to 100")

    def test_override_remove_item(self):
        """Tests that calling Shop's remove_item with no arguments decrements quantity by one"""
        self.my_cart_2.add_item('Chocolate', 5, 300)
        self.my_cart_2.remove_item()
        self.assertDictEqual(self.my_cart_2.items, {'Chocolate': 4},
                             msg="Does not remove quantity by one")

#   def test_cart_property_initialization(self):
#     self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
#     self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')

    def test_add_item(self):
        self.cart.add_item('Mango', 3, 10)
        self.assertEqual(self.cart.total,
                         30,
                         msg='Cart total not correct after adding items')
        self.assertEqual(self.cart.items['Mango'],
                         3,
                         msg='Quantity of items not correct after adding item')
Exemple #4
0
class TestShoppingCart(unittest.TestCase):
    """A Class to Test ShoppingCart Class"""
    def setUp(self):
        """Setting up the ShoppingCart Class"""
        self.my_cart = ShoppingCart()
        self.my_cart2 = Shop()

    def test_add_item_total(self):
        """Test if the method add_item updates total accordingly"""
        my_total = self.my_cart.add_item('Cookies', 2, 400)
        self.assertEqual(my_total, 800)
        self.assertDictEqual(self.my_cart.items, {'Cookies', 2},
                             msg="Invalid Dictionary")

    def test_add_item_entry(self):
        """Tests whether an entry is added accordingly in the items dict"""
        my_total = self.my_cart.add_item('Cookies', 2, 400)
        self.assertEqual(my_total, 800)

    def test_remove_item_total(self):
        """Tests whether an item is removed accordingly in the items dict"""
        self.my_cart.add_item('Cookies', 5, 1500)
        self.my_cart.remove_item('Cookies', 2, 300)
        self.assertEqual(self.my_cart.total, 900, "Inacurate remove method")

    def test_remove_item_entry(self):
        """Tests whether the dictionary is updated accordingly"""
        self.my_cart.add_item('Cookies', 5, 1500)
        self.my_cart.remove_item('Cookies', 2, 300)
        self.assertDictEqual(self.my_cart.items, {'Cookies', 3},
                             msg="Invalid Dictionary")

    def test_exceeds_current_quantity(self):
        """Test that all entries are removed if the quantity exceeds the current"""
        self.my_cart.add_item('Cookies', 5, 1500)
        self.my_cart.remove_item('Cookies', 2, 300)
        self.assertEqual(
            self.my_cart.items, {},
            msg="Doesnt remove all if the quantity exceeds the current")

    def test_checkout(self):
        """Test whether it returns the correct balance"""
        self.my_cart.add_item('Cookies', 5, 1500)
        self.my_cart.checkout(self.my_cart.checkout(700), 100)

    def test_checkout_not_enough(self):
        """Tests if the payment is enough"""
        self.my_cart.add_item('Cookies', 5, 1500)
        self.assertEqual(self.my_cart.checkout(100), "Cash paid not enough")

    def test_shop_if_subclass(self):
        """Tests whether shop inherits from ShoppingCart"""
        self.assertTrue(issubclass(Shop, ShoppingCart),
                        msg="Shop doesnt inherits from ShoppingCart")

    def test_shop_quantity(self):
        """Tests whether Shop quantity attribute is initialized to 100"""
        self.assertEqual(self.my_cart2.quantity, 100)

    def test_override_remove_item(self):
        """Tests whether calling Shop's remove_item with no arguments decrements quantity by one."""
        self.my_cart2.add_item('Cookies', 5, 1500)
        self.my_cart2.remove_item()
        self.assertDictEqual(self.my_cart2.items, {'Cookies': 5},
                             msg="Does not remove quantity by one")