def test_remove_item_from_cart(self): username = '******' item_id = 1 quantity = 20 ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id, quantity, "")) ShoppingCartDB.remove_item_shopping_cart(username, item_id) cart_items = ShoppingCartDB.get_cart_items(username) self.assertEqual(len(cart_items), 0)
def test_add_item_to_cart(self): username = '******' item_id = 1 quantity = 20 ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id, quantity, "")) cart_items = ShoppingCartDB.get_cart_items(username) self.assertEqual(len(cart_items), 1) self.assertEqual(cart_items[0].username, username) self.assertEqual(cart_items[0].item_id, item_id) self.assertEqual(cart_items[0].item_quantity, quantity)
def test_get_cart_items(self): username = '******' item_id1 = 1 quantity1 = 20 item_id2 = 2 quantity2 = 2 ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id1, quantity1, "")) ShoppingCartDB.add_item_shopping_cart( ShoppingCartDB.ShoppingCartItem(username, item_id2, quantity2, "")) cart_items = ShoppingCartDB.get_cart_items(username) self.assertEqual(len(cart_items), 2) self.assertEqual(username, cart_items[0].username) self.assertEqual(item_id1, cart_items[0].item_id) self.assertEqual(quantity1, cart_items[0].item_quantity) self.assertEqual(username, cart_items[1].username) self.assertEqual(item_id2, cart_items[1].item_id) self.assertEqual(quantity2, cart_items[1].item_quantity)
def add_all_shopping_cart_to_user(shopping_cart): for cart_item in shopping_cart: status = ShoppingCartDB.add_item_shopping_cart(cart_item) if status is False: return False if cart_item.code is not None: status = ShoppingCartDB.update_code_shopping_cart(cart_item.username, cart_item.item_id, cart_item.code) if status is False: return False return True
def add_item_shopping_cart(shop_cart_item): if shop_cart_item.username is not None and shop_cart_item.item_id is not None and shop_cart_item.item_quantity > 0: if ItemsLogic.check_in_stock(shop_cart_item.item_id, shop_cart_item.item_quantity) is False: return False existing = ShoppingCartDB.get_shopping_cart_item(shop_cart_item) if existing is not False: return update_item_shopping_cart( shop_cart_item.username, shop_cart_item.item_id, shop_cart_item.item_quantity + existing.item_quantity) else: return ShoppingCartDB.add_item_shopping_cart(shop_cart_item) return False