def test_remove_basket(self): self.cart.baskets.clear() basket1 = Basket(1234) basket2 = Basket(5678) self.cart.baskets[1234] = basket1 self.cart.baskets[5678] = basket2 self.cart.remove_basket(1234) self.assertTrue(1234 not in self.cart.baskets.keys()) self.assertTrue(5678 in self.cart.baskets.keys())
def test_buy(self): self.test_add_product_to_store() cart = Cart() cart.baskets = {self.idx - 1: Basket(self.idx - 1)} cart.get_basket(self.idx - 1).add_product(Product(*self.products[-1]), 2) self.assertTrue(self.store_manager.buy(jsonpickle.encode(cart))) cart = Cart() cart.baskets = {self.idx + 1: Basket(self.idx + 1)} cart.get_basket(self.idx + 1).add_product(Product(*self.products[-1]), 2) self.assertFalse(self.store_manager.buy(jsonpickle.encode(cart))) cart = Cart() cart.baskets = {self.idx - 1: Basket(self.idx - 1)} cart.get_basket(self.idx - 1).add_product(Product(*self.products[-1]), 30) self.assertFalse(self.store_manager.buy(jsonpickle.encode(cart)))
def setUp(self) -> None: # 1234 for store id self.basket = Basket(1234) date_str = '04-10-2020' dt = timedelta(days=100000) date_object = datetime.strptime(date_str, '%m-%d-%Y') self.product_orange = Product("orange", 2, "food", None, 100) self.product_orange.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_apple = Product("apple", 2, "food", None, 100) self.product_apple.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_petunia = Product("petunia", 5, "food", None, 100) self.product_petunia.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_begonia = Product("begonia", 15, "food", None, 100) self.product_begonia.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50))
def test_get_basket(self): self.cart.baskets.clear() basket1 = Basket(1234) self.cart.baskets[1234] = basket1 basket2 = self.cart.get_basket(2222) basket1_tag = self.cart.get_basket(1234) self.assertIsNone(basket2) self.assertEqual(basket1, basket1_tag)
def test_get_total(self): self.cart.baskets.clear() # total price = 10 basket1 = Basket(1234) basket1.products["orange"] = (self.product_orange, 3) basket1.products["apple"] = (self.product_apple, 2) # total price = 125 basket2 = Basket(5678) basket2.products["petunia"] = (self.product_petunia, 10) basket2.products["begonia"] = (self.product_begonia, 5) self.cart.baskets[1234] = basket1 self.cart.baskets[5678] = basket2 total_price = self.cart.get_total() self.assertEqual(total_price, 67.5)
class TestBasket(TestCase): def setUp(self) -> None: # 1234 for store id self.basket = Basket(1234) date_str = '04-10-2020' dt = timedelta(days=100000) date_object = datetime.strptime(date_str, '%m-%d-%Y') self.product_orange = Product("orange", 2, "food", None, 100) self.product_orange.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_apple = Product("apple", 2, "food", None, 100) self.product_apple.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_petunia = Product("petunia", 5, "food", None, 100) self.product_petunia.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_begonia = Product("begonia", 15, "food", None, 100) self.product_begonia.discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) def test_get_total(self): self.basket.products.clear() self.basket.products["orange"] = (self.product_orange, 3) total = self.basket.get_total() self.assertEqual(total, 3) def test_add_product(self): # reset products in basket self.basket.products.clear() self.basket.products["orange"] = (self.product_orange, 3) # products test dict contains 5 oranges and 2 apples test_products = {} pass test_products["orange"] = (self.product_orange, 5) test_products["apple"] = (self.product_apple, 2) # adding 2 oranges and 2 apples to basket self.basket.add_product(self.product_orange, 2) self.basket.add_product(self.product_apple, 2) # test equality of dicts self.assertDictEqual(test_products, self.basket.products) def test_remove_product(self): # reset products in basket self.basket.products.clear() self.basket.products["orange"] = (self.product_orange, 3) # remove all products from basket self.basket.remove_product(self.product_orange, 3) self.assertDictEqual(self.basket.products, {})
def test_remove_product(self): self.cart.baskets.clear() basket1 = Basket(1234) basket1.products["orange"] = (self.product_orange, 3) basket1.products["apple"] = (self.product_apple, 2) basket2 = Basket(5678) basket2.products["petunia"] = (self.product_petunia, 10) self.cart.baskets[1234] = basket1 self.cart.baskets[5678] = basket2 self.cart.remove_product(1234, self.product_orange, 3) self.cart.remove_product(1234, self.product_apple, 2) self.cart.remove_product(5678, self.product_petunia, 5) self.assertTrue(1234 not in self.cart.baskets.keys()) self.assertEqual(basket2.products["petunia"][1], 5) self.assertFalse( self.cart.remove_product(5678, self.product_begonia, 5))
def setUp(self): self.store = Store(0, "test store", "test owner") self.store.store_managers = { "Moshe": [], "Amit": [Store.add_product], "Hadar": [], "Lielle": [Store.remove_product], "Noa": [Store.add_visible_product_discount], "Evgeny": [Store.update_product] } self.standard_users = ["Avishay", "Alex", "Ron"] self.store.inventory.products = { "Apple": Product("Apple", 20, ["Food"], ["Fruits"], 30), "Banana": Product("Banana", 20, ["Food"], ["Fruits"], 10), "Orange": Product("Orange", 20, ["Food"], ["Fruits"], 10), "Tomato": Product("Tomato", 20, ["Food"], ["Vegetables"], 10), "Cucumber": Product("Cucumber", 20, ["Food"], ["Vegetables"], 10), "Carrot": Product("Carrot", 20, ["Food"], ["Vegetables"], 10), "Iphone": Product("Iphone", 20, ["Electronics"], ["Computers"], 10), "Hard Disk": Product("Hard Disk", 20, ["Electronics"], ["Computers"], 10), "Keyboard": Product("Keyboard", 20, ["Electronics"], ["Computers"], 10) } self.discount = VisibleProductDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 5) self.discount1 = VisibleProductDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 5) self.discount2 = ConditionalProductDiscount( datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 5, 2, 2) self.discount3 = ConditionalProductDiscount( datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 5, 7, 1) self.discount4 = ConditionalProductDiscount( datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 5, 6, 3) self.discount5 = ConditionalProductDiscount( datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 100, 3, 1) self.tup_disc_prod_name_list = [] self.tup_disc_prod_name_list.append( (self.discount3, ["Apple", "Carrot", "Keyboard"])) self.tup_disc_prod_name_list.append( (self.discount4, ["Apple", "Tomato"])) self.discount9 = VisibleProductDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 30) self.discount6 = CompositeDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), LogicOperator.AND, self.tup_disc_prod_name_list, [self.discount, self.discount9]) self.discount7 = CompositeDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), LogicOperator.OR, self.tup_disc_prod_name_list, [self.discount, self.discount9]) self.discount8 = CompositeDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), LogicOperator.XOR, self.tup_disc_prod_name_list, [self.discount, self.discount9]) # self.store.discounts[0] = self.discount # self.store.discounts[1] = self.discount1 # self.store.discounts[2] = self.discount2 # self.store.discounts[3] = self.discount3 # self.store.discounts[4] = self.discount4 # self.store.discounts[5] = self.discount5 # self.store.discounts[6] = self.discount6 # self.store.discounts[7] = self.discount7 # self.store.discounts[8] = self.discount8 self.discount.products_in_discount[ "Apple"] = self.store.inventory.products["Apple"] self.discount.products_in_discount[ "Tomato"] = self.store.inventory.products["Tomato"] self.discount.products_in_discount[ "Carrot"] = self.store.inventory.products["Carrot"] self.discount.products_in_discount[ "Keyboard"] = self.store.inventory.products["Keyboard"] self.discount.products_in_discount[ "Hard Disk"] = self.store.inventory.products["Hard Disk"] self.discount1.products_in_discount[ "Apple"] = self.store.inventory.products["Apple"] self.discount2.products_in_discount[ "Orange"] = self.store.inventory.products["Orange"] self.discount3.products_in_discount[ "Apple"] = self.store.inventory.products["Apple"] self.discount3.products_in_discount[ "Carrot"] = self.store.inventory.products["Carrot"] self.discount3.products_in_discount[ "Keyboard"] = self.store.inventory.products["Keyboard"] self.discount4.products_in_discount[ "Apple"] = self.store.inventory.products["Apple"] self.discount5.products_in_discount[ "Tomato"] = self.store.inventory.products["Tomato"] self.discount5.products_in_discount[ "Hard Disk"] = self.store.inventory.products["Hard Disk"] self.discount6.products_in_discount[ "Iphone"] = self.store.inventory.products["Iphone"] self.discount6.products_in_discount[ "Hard Disk"] = self.store.inventory.products["Hard Disk"] self.discount7.products_in_discount[ "Iphone"] = self.store.inventory.products["Iphone"] self.discount7.products_in_discount[ "Hard Disk"] = self.store.inventory.products["Hard Disk"] self.discount8.products_in_discount[ "Iphone"] = self.store.inventory.products["Iphone"] self.discount8.products_in_discount[ "Hard Disk"] = self.store.inventory.products["Hard Disk"] self.discount9.products_in_discount[ "Hard Disk"] = self.store.inventory.products["Hard Disk"] self.basket = Basket(self.store.store_id) self.basket.products["Apple"] = ( self.store.inventory.products["Apple"], 10) self.basket.products["Keyboard"] = ( self.store.inventory.products["Keyboard"], 5) self.basket.products["Carrot"] = ( self.store.inventory.products["Carrot"], 1) self.basket.products["Orange"] = ( self.store.inventory.products["Orange"], 3) self.basket.products["Tomato"] = ( self.store.inventory.products["Tomato"], 4) self.basket1 = Basket(self.store.store_id) self.basket1.products["Apple"] = ( self.store.inventory.products["Apple"], 10) self.basket1.products["Keyboard"] = ( self.store.inventory.products["Keyboard"], 10) self.basket1.products["Carrot"] = ( self.store.inventory.products["Carrot"], 10) self.basket1.products["Banana"] = ( self.store.inventory.products["Banana"], 10) self.basket1.products["Orange"] = ( self.store.inventory.products["Orange"], 10) self.basket1.products["Cucumber"] = ( self.store.inventory.products["Cucumber"], 10) self.basket1.products["Iphone"] = ( self.store.inventory.products["Iphone"], 10) self.basket1.products["Hard Disk"] = ( self.store.inventory.products["Hard Disk"], 10) self.basket1.products["Tomato"] = ( self.store.inventory.products["Tomato"], 10)
def init_cart(self): basket = Basket(1) basket.products["Apple"] = 10 basket.products["Keyboard"] = 5 basket.products["Carrot"] = 1 basket1 = Basket(2) basket1.products["Apple"] = 10 basket1.products["Keyboard"] = 10 basket1.products["Carrot"] = 10 basket1.products["Banana"] = 10 basket1.products["Orange"] = 10 basket1.products["Cucumber"] = 10 basket1.products["Iphone"] = 10 basket1.products["Hard Disk"] = 10 basket1.products["Tomato"] = 10 cart = Cart() cart.baskets[1] = basket cart.baskets[2] = basket1 return cart
def add_basket(self, store_id): if store_id not in self.baskets.keys(): basket = Basket(store_id) self.baskets[store_id] = basket return basket
def setUp(self) -> None: self.users_manager = UsersManager() date_str = '04-10-2020' dt = timedelta(days=10) date_object = datetime.datetime(2020, 4, 10) self.product_orange = Product("orange", 2, "food", None, 100) self.product_orange.visible_discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_apple = Product("apple", 2, "food", None, 100) self.product_apple.visible_discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_petunia = Product("petunia", 5, "food", None, 100) self.product_petunia.visible_discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) self.product_begonia = Product("begonia", 15, "food", None, 100) self.product_begonia.visible_discount.append( VisibleProductDiscount(date_object, date_object + dt, 50)) ############################################### self.store_manager = StoresManager() self.guest_user = self.users_manager.add_guest_user() self.idx = 0 self.store_id1 = self.users_manager.store_manager.open_store( "test store1", "test owner1") self.store_id2 = self.users_manager.store_manager.open_store( "test store2", "test owner2") self.store_id3 = self.users_manager.store_manager.open_store( "test store3", "test owner3") self.store_id4 = self.users_manager.store_manager.open_store( "test store4", "test owner4") self.products = { "Apple": Product("Apple", 20, ["Food"], ["Fruits"], 10), "Banana": Product("Banana", 20, ["Food"], ["Fruits"], 10), "Orange": Product("Orange", 20, ["Food"], ["Fruits"], 10), "Tomato": Product("Tomato", 20, ["Food"], ["Vegetables"], 10), "Cucumber": Product("Cucumber", 20, ["Food"], ["Vegetables"], 10), "Carrot": Product("Carrot", 20, ["Food"], ["Vegetables"], 10), "Iphone": Product("Iphone", 20, ["Electronics"], ["Computers"], 10), "Hard Disk": Product("Hard Disk", 20, ["Electronics"], ["Computers"], 10), "Keyboard": Product("Keyboard", 20, ["Electronics"], ["Computers"], 10) } self.users_manager.store_manager.get_store( self.store_id1).inventory.products = self.products self.users_manager.store_manager.get_store( self.store_id2).inventory.products = self.products self.users_manager.store_manager.get_store( self.store_id3).inventory.products = self.products self.users_manager.store_manager.get_store( self.store_id4).inventory.products = self.products self.discount = VisibleProductDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 10) self.discount1 = VisibleProductDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 30) self.users_manager.store_manager.get_store( self.store_id1).discounts[self.discount.id] = self.discount self.users_manager.store_manager.get_store( self.store_id1).discounts[self.discount1.id] = self.discount1 self.users_manager.store_manager.get_store( self.store_id2).discounts[self.discount.id] = self.discount self.users_manager.store_manager.get_store( self.store_id3).discounts[self.discount1.id] = self.discount1 self.discount.products_in_discount[ "Apple"] = self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Apple"] self.discount.products_in_discount[ "Tomato"] = self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Tomato"] self.discount1.products_in_discount[ "Apple"] = self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Apple"] self.discount.products_in_discount[ "Carrot"] = self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Carrot"] self.discount.products_in_discount[ "Keyboard"] = self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Keyboard"] self.discount.products_in_discount[ "Apple"] = self.users_manager.store_manager.get_store( self.store_id2).inventory.products["Apple"] self.discount.products_in_discount[ "Tomato"] = self.users_manager.store_manager.get_store( self.store_id2).inventory.products["Tomato"] self.discount.products_in_discount[ "Carrot"] = self.users_manager.store_manager.get_store( self.store_id2).inventory.products["Carrot"] self.discount.products_in_discount[ "Keyboard"] = self.users_manager.store_manager.get_store( self.store_id2).inventory.products["Keyboard"] self.discount.products_in_discount[ "Apple"] = self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Apple"] self.discount.products_in_discount[ "Tomato"] = self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Tomato"] self.discount1.products_in_discount[ "Apple"] = self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Apple"] self.discount.products_in_discount[ "Carrot"] = self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Carrot"] self.discount.products_in_discount[ "Keyboard"] = self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Keyboard"] self.basket1 = Basket( self.users_manager.store_manager.get_store( self.store_id1).store_id) self.basket2 = Basket( self.users_manager.store_manager.get_store( self.store_id2).store_id) self.basket3 = Basket( self.users_manager.store_manager.get_store( self.store_id3).store_id) self.basket1.products["Apple"] = ( self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Apple"], 10) self.basket1.products["Keyboard"] = ( self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Keyboard"], 5) self.basket1.products["Carrot"] = ( self.users_manager.store_manager.get_store( self.store_id1).inventory.products["Carrot"], 1) self.basket2.products["Apple"] = ( self.users_manager.store_manager.get_store( self.store_id2).inventory.products["Apple"], 3) self.basket2.products["Keyboard"] = ( self.users_manager.store_manager.get_store( self.store_id2).inventory.products["Keyboard"], 7) self.basket2.products["Carrot"] = ( self.users_manager.store_manager.get_store( self.store_id2).inventory.products["Carrot"], 5) self.basket3.products["Apple"] = ( self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Apple"], 5) self.basket3.products["Keyboard"] = ( self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Keyboard"], 5) self.basket3.products["Carrot"] = ( self.users_manager.store_manager.get_store( self.store_id3).inventory.products["Carrot"], 2) self.cart = self.users_manager.get_cart(self.guest_user) self.cart.baskets[self.store_id1] = self.basket1 self.cart.baskets[self.store_id2] = self.basket2 self.cart.baskets[self.store_id3] = self.basket3
def setUp(self) -> None: self.store_manager = StoresManager() self.idx = 0 self.store_id = self.store_manager.open_store("test store", "test owner") self.products = [("t-shirt", 20, ["cloth"], ["green"], 7), ("apple", 1, ["food", "green"], ["vegetable"], 10), ("orange", 1, ["food", "orange"], ["fruits"], 10), ("iphone", 5000, ["electronics", "bad and expensive phone "], ["fruits"], 10)] self.store_manager.get_store(self.store_id).inventory.products = { "Apple": Product("Apple", 20, ["Food"], ["Fruits"], 10), "Banana": Product("Banana", 20, ["Food"], ["Fruits"], 10), "Orange": Product("Orange", 20, ["Food"], ["Fruits"], 10), "Tomato": Product("Tomato", 20, ["Food"], ["Vegetables"], 10), "Cucumber": Product("Cucumber", 20, ["Food"], ["Vegetables"], 10), "Carrot": Product("Carrot", 20, ["Food"], ["Vegetables"], 10), "Iphone": Product("Iphone", 20, ["Electronics"], ["Computers"], 10), "Hard Disk": Product("Hard Disk", 20, ["Electronics"], ["Computers"], 10), "Keyboard": Product("Keyboard", 20, ["Electronics"], ["Computers"], 10) } self.discount = VisibleProductDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 10) self.discount1 = VisibleProductDiscount(datetime.datetime(2018, 6, 1), datetime.datetime(2020, 5, 17), 30) self.purchase_store = PurchaseStorePolicy(0, 10, 2) self.purchase_store1 = PurchaseStorePolicy(9, 10, 3) self.purchase = PurchaseProductPolicy(0, 5, 0) self.purchase1 = PurchaseProductPolicy(3, 6, 1) self.store_manager.get_store(self.store_id).purchase_policies[ self.purchase_store.id] = self.purchase_store self.store_manager.get_store(self.store_id).purchase_policies[ self.purchase_store1.id] = self.purchase_store1 self.store_manager.get_store( self.store_id).purchase_policies[self.purchase.id] = self.purchase self.store_manager.get_store(self.store_id).purchase_policies[ self.purchase1.id] = self.purchase1 self.store_manager.get_store( self.store_id).discounts[self.discount.id] = self.discount self.store_manager.get_store( self.store_id).discounts[self.discount1.id] = self.discount1 self.purchase.products_in_policy[ "Apple"] = self.store_manager.get_store( self.store_id).inventory.products["Apple"] self.purchase1.products_in_policy[ "Iphone"] = self.store_manager.get_store( self.store_id).inventory.products["Iphone"] self.discount.products_in_discount[ "Apple"] = self.store_manager.get_store( self.store_id).inventory.products["Apple"] self.discount.products_in_discount[ "Tomato"] = self.store_manager.get_store( self.store_id).inventory.products["Tomato"] self.discount1.products_in_discount[ "Apple"] = self.store_manager.get_store( self.store_id).inventory.products["Apple"] self.discount.products_in_discount[ "Carrot"] = self.store_manager.get_store( self.store_id).inventory.products["Carrot"] self.discount.products_in_discount[ "Keyboard"] = self.store_manager.get_store( self.store_id).inventory.products["Keyboard"] self.basket_legal_pruchace = Basket( self.store_manager.get_store(self.store_id).store_id) self.basket_ilegal_pruchace = Basket( self.store_manager.get_store(self.store_id).store_id) self.basket_some_legal_pruchace = Basket( self.store_manager.get_store(self.store_id).store_id) self.basket_legal_pruchace.products["Apple"] = ( self.store_manager.get_store( self.store_id).inventory.products["Apple"], 5) self.basket_legal_pruchace.products["Iphone"] = ( self.store_manager.get_store( self.store_id).inventory.products["Iphone"], 5) self.basket_ilegal_pruchace.products["Apple"] = ( self.store_manager.get_store( self.store_id).inventory.products["Apple"], 6) self.basket_ilegal_pruchace.products["Iphone"] = ( self.store_manager.get_store( self.store_id).inventory.products["Iphone"], 2) self.basket_some_legal_pruchace.products["Apple"] = ( self.store_manager.get_store( self.store_id).inventory.products["Apple"], 4) self.basket_some_legal_pruchace.products["Iphone"] = ( self.store_manager.get_store( self.store_id).inventory.products["Iphone"], 2) self.basket = Basket( self.store_manager.get_store(self.store_id).store_id) self.basket.products["Apple"] = (self.store_manager.get_store( self.store_id).inventory.products["Apple"], 10) self.basket.products["Keyboard"] = (self.store_manager.get_store( self.store_id).inventory.products["Keyboard"], 5) self.basket.products["Carrot"] = (self.store_manager.get_store( self.store_id).inventory.products["Carrot"], 1)