class BasketTests(unittest.TestCase): def setUp(self): self.jarin_ostoskori = Basket("Jari", ["maito","banaani","hernekeitto"], 15) def tearDown(self): del self.jarin_ostoskori def test_customer_is_string(self): self.assertTrue(isinstance(self.jarin_ostoskori.customer, str), "Variable customer type should be string.") def test_contents_is_list(self): self.assertTrue(isinstance(self.jarin_ostoskori.contents, list), "Variable contents type should be list.") def test_price_is_number(self): self.assertTrue(isinstance(self.jarin_ostoskori.price, Number), "Variable price type should be Number") def test_can_add_product(self): self.jarin_ostoskori.add_product("kala", 5) self.assertIn("kala", self.jarin_ostoskori.contents, "Variable contents did not contain added item.") def test_can_delete_product(self): self.jarin_ostoskori.delete_product("kala", 5) self.assertNotIn("kala", self.jarin_ostoskori.contents, "Variable contents should not contain deleted item.")
class BasketTests(unittest.TestCase): #setup method to create test object def setUp(self): print("setting up!") self.keijon_ostoskori = Basket("Keijo", ["kissa", "pasi"], 20, 0.24) #teardown method to delete the test object def tearDown(self): print("tearing down!") del self.keijon_ostoskori #Lets test if variable customer is a string def test_customer_is_string(self): self.assertTrue(isinstance(self.keijon_ostoskori.customer, str), "variable customer name should be string") #let's test if the variable contents is a list def test_contents_is_list(self): self.assertTrue(isinstance(self.keijon_ostoskori.contents, list), "variable contents should be a list") #let's test if variable price is a number def test_price_is_number(self): self.assertTrue( isinstance(self.keijon_ostoskori.price, numbers.Number), "variable price should be numbers") #let's test if add_product method works def test_list_add(self): self.keijon_ostoskori.add_product("kala", 5) self.assertIn("kala", self.keijon_ostoskori.contents, "add_product did not add product to the list") #let's test if delete_product method works def test_list_delete(self): self.keijon_ostoskori.delete_product("kala", 5) self.assertNotIn( "kala", self.keijon_ostoskori.contents, "delete_product did not delete product from the list") #test average price def test_average_price(self): self.assertEqual( self.keijon_ostoskori.calculate_average_price(), 10, "calculate_average_price does not calculate correct value") #test vat def test_vat(self): self.assertEqual(self.keijon_ostoskori.calculate_vat(), 15.2, "calculate_vat does not calculate") #test vat is number def test_vat_is_number(self): self.assertTrue(isinstance(self.keijon_ostoskori.vat, numbers.Number), "variable vat should be numbers")
class CashRegister: basket = None basketOnHold = None def __init__(self): pass def start(self): # verification caisse # Lancement nouveau panier self.new_basket() def new_basket(self): self.basket = Basket() while True: print("--------------------") print("Scan a product") code = input("code: ") print("--------------------") self.scan_product(code) def scan_product(self, code): product = ProductDbConnector.get_product_by_code(code) if product: product_to_add = Product(product) self.basket.add_product(product_to_add) else: print('Produit n\'existe pas') def delete_product_from_basket(self, product): try: self.basket.remove_product(product) except ValueError: return "Product not found" def total_price(self): return self.basket.totalPrice def pay(self): print('Pay') def put_basket_on_hold(self): self.basketOnHold = BasketOnHold(self.basket) self.basket = None print('On hold') def get_basket_on_hold(self): return self.basketOnHold
def order_button_handler(call): obj_id = str.split(call.data)[1] product = self.bd.find_product_by_str_obj_id(obj_id) if product['qty'] < 1: message = 'No product at store' self.bot.answer_callback_query(call.id, message, show_alert=True) else: basket = Basket(call.from_user.id) basket.add_product(product) message = self.make_modal_message(product) self.bot.answer_callback_query(call.id, message, show_alert=True) self.bot.send_message( call.from_user.id, product[CATEGORY_NAME], reply_markup=self.buttons.show_products_from_category( product[CATEGORY_NAME]))
class Basket_tests(unittest.TestCase): # define setUp for tests def setUp(self): self.product = Product("Lego1", "Lego Toy", 2.50) self.basket = Basket() # create test for instance of Product class def product_instance_test(self): # create instance of product with name, description, and price attribute self.product = Product("test", "This is a sample", 2.50) # verify attributes of instance of product self.assertEqual("test", self.product.name) self.assertEqual("This is a sample", self.product.description) self.assertEqual(2.50, self.product.price) # create test for instance of Basket class def basket_instance_test(self): # create instance of basket class self.basket = Basket() # create test to display product details def display_product_details_test(self): result = self.product.display_product_details() self.assertEqual("Product name: Lego1, Desc: Lego Toy, Price: £2.50", result) # create test to add a product to the basket def add_product_to_basket_test(self): result = self.basket.add_product(self.product) self.assertEqual(["Lego1", "Lego Toy", 2.50], result) # create a test to add product using a dictionary as the container def add_prod_to_basket_test(self): result = self.basket.add_prods(self.product, 5) self.assertEqual({1:["Lego1", "Lego Toy", 2.50, 5]}, result) # create test to remove product item from basket items def remove_item_from_basket_test(self): # add product to basket first self.basket.add_prods(self.product) result = self.basket.remove_item(self.product) self.assertEqual({}, result) # create test to raise error when item is tried to be removed from empty basket def error_when_item_removed_from_empty_basket_test(self): # remove item from empty basket #result = self.basket.remove_item(self.product) # confirm exception error message is raised self.assertRaises(NameError, self.basket.remove_item, self.product)
def test_basket_3(self): """ Check subtotal, discount and total are as expected for basket 3 """ basket_3 = Basket( product_catalog=catalog.products, offers=[CheapestFree(product_category="shampoo", min_items=3)], ) basket_3.add_product("shampoo-l") basket_3.add_product("shampoo-l") basket_3.add_product("shampoo-l") basket_3.add_product("shampoo-m") basket_3.add_product("shampoo-s") basket_3.add_product("shampoo-s") self.assertEqual(basket_3.subtotal(), 17) self.assertEqual(basket_3.discount(), 5.5) self.assertEqual(basket_3.total(), 11.5)
class TestBasket(TestCase): def setUp(self): self.test_products = [ Product('Baked Beans', 0.99), Product('Biscuits', 1.20), Product('Sardines', 1.89), Product('Shampoo (Small)', 2.00), Product('Shampoo (Medium)', 2.50), Product('Shampoo (Large)', 3.50), ] self.test_catalouge = Catalogue(self.test_products) def test_construct_basket(self): self.test_basket = Basket(self.test_catalouge) self.assertEqual(len(self.test_basket.products), 0) def test_construct_basket_with_product_lists(self): self.test_basket = Basket(self.test_catalouge) for product in self.test_products: self.test_basket.add_product(product) self.assertEqual(len(self.test_basket.products), len(self.test_products)) def test_add_product_to_basket(self): self.test_basket = Basket(self.test_catalouge) self.test_basket.add_product(self.test_products[0]) self.assertEqual(len(self.test_basket.products), 1) self.assertEqual(self.test_basket.products[self.test_products[0].name].quantity, 1) def test_add_multi_product_to_basket(self): self.test_basket = Basket(self.test_catalouge) self.test_basket.add_product(self.test_products[0]) self.test_basket.add_product(self.test_products[0]) self.assertEqual(len(self.test_basket.products), 1) self.assertEqual(self.test_basket.products[self.test_products[0].name].quantity, 2) def test_add_product_not_in_catalogue_to_basket(self): self.test_basket = Basket(self.test_catalouge) with self.assertRaises(ProductNotInCatalogueException): self.test_basket.add_product(Product('Dummy Product', 1.00)) def test_removing_product_from_basket(self): self.test_basket = Basket(self.test_catalouge) for product in self.test_products: self.test_basket.add_product(product) self.test_basket.remove_product(self.test_products[0]) self.assertEqual(len(self.test_basket.products), len(self.test_products) - 1) self.assertEqual(self.test_products[0].name in self.test_basket.products, False) def test_empty_basket(self): self.test_basket = Basket(self.test_catalouge) for product in self.test_products: self.test_basket.add_product(product) self.assertEqual(len(self.test_basket.products), len(self.test_products)) self.test_basket.empty() self.assertEqual(len(self.test_basket.products), 0) def test_basket_subtotal(self): # One of each item self.test_basket = Basket(self.test_catalouge) for product in self.test_products: self.test_basket.add_product(product) self.assertEqual(self.test_basket.calculate_subtotal(), 12.08) self.test_basket.empty() # 4 Baked Beans and 1 Biscuits # Add 4 Baked Beans self.test_basket.add_product(self.test_products[0], 4) # Add 1 Biscuits self.test_basket.add_product(self.test_products[1]) self.assertEqual(self.test_basket.calculate_subtotal(), 5.16) self.test_basket.empty() # With Sardines 25% discount self.test_basket = Basket(self.test_catalouge) # Add 2 Baked Beans self.test_basket.add_product(self.test_products[0], 2) # Add 1 Biscuits self.test_basket.add_product(self.test_products[1]) # Add 2 Sardines self.test_basket.add_product(self.test_products[2], 2) self.assertEqual(self.test_basket.calculate_subtotal(), 6.96) def test_basket_discount_buy_2_get_1_free(self): # With Baked Beans buy 2 get 1 free self.buy_and_get_free_offer = BuyAndGetFreeOffer(self.test_products[0], 2, 1) self.test_basket = Basket(self.test_catalouge, [self.buy_and_get_free_offer]) # Add 4 Baked Beans self.test_basket.add_product(self.test_products[0], 4) # Add 1 Biscuits self.test_basket.add_product(self.test_products[1]) self.assertEqual(self.test_basket.calculate_discount(), 0.99) # Add 4 Baked Beans self.test_basket.add_product(self.test_products[0], 4) self.assertEqual(self.test_basket.calculate_discount(), 1.98) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[0].price * 8 + self.test_products[1].price - self.test_basket.calculate_discount(), 2)) def test_basket_discount_percent_offer(self): # With Sardines 25% discount self.percentage_offer = PercentageOffer(self.test_products[2], 0.25) self.test_basket = Basket(self.test_catalouge, [self.percentage_offer]) # Add 2 Baked Beans self.test_basket.add_product(self.test_products[0], 2) # Add 1 Biscuits self.test_basket.add_product(self.test_products[1]) # Add 2 Sardines self.test_basket.add_product(self.test_products[2], 2) self.assertEqual(self.test_basket.calculate_discount(), 2 * round_half_up(self.test_products[2].price * 0.25, 2)) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[0].price * 2 + self.test_products[1].price + self.test_products[2].price * 2 - self.test_basket.calculate_discount(), 2)) def test_basket_discount_percent_and_buy_2_get_1_free_offers(self): # With Baked Beans buy 2 get 1 free and 25% discount self.buy_and_get_free_offer = BuyAndGetFreeOffer(self.test_products[0], 2, 1) self.percentage_offer = PercentageOffer(self.test_products[0], 0.25) self.test_basket = Basket(self.test_catalouge, [self.buy_and_get_free_offer, self.percentage_offer]) # Add 4 Baked Beans self.test_basket.add_product(self.test_products[0], 4) # Add 1 Biscuits self.test_basket.add_product(self.test_products[1]) # Baked Beans Buy 2 get one free: 0.99 # Baked Beans 25% percent discount: 0.99 * 0.25 = 0.25 # Total: 0.99 + 0.25 = 1.24 self.assertEqual(self.test_basket.calculate_discount(), 1.24) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[0].price * 4 + self.test_products[1].price - self.test_basket.calculate_discount(), 2)) # Add 4 Baked Beans self.test_basket.add_product(self.test_products[0], 4) # Buy 2 get 1 free twice: 0.99 * 2 = 1.98 # Percentage discount on remaining 2: 0.99 * 0.25 * 2 = 0.50 # total: 1.98 + 0.50 = 2.48 self.assertEqual(self.test_basket.calculate_discount(), 2.48) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[0].price * 8 + self.test_products[1].price - self.test_basket.calculate_discount(), 2)) # With Baked Beans buy 2 get 1 free and 50% discount self.buy_and_get_free_offer = BuyAndGetFreeOffer(self.test_products[0], 2, 1) self.percentage_offer = PercentageOffer(self.test_products[0], 0.50) self.test_basket = Basket(self.test_catalouge, [self.buy_and_get_free_offer, self.percentage_offer]) # Add 4 Baked Beans self.test_basket.add_product(self.test_products[0], 4) # Add 1 Biscuits self.test_basket.add_product(self.test_products[1]) # Baked Beans Buy 2 get one free: 0 # Baked Beans 50% percent discount: 0.99 * 0.5 = 2.00 # Total: 2.00 self.assertEqual(self.test_basket.calculate_discount(), 2.00) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[0].price * 4 + self.test_products[1].price - self.test_basket.calculate_discount(), 2)) def test_basket_discount_bundle_offer(self): # With bundle offer on all shampoo buy 3 get the cheapest free self.test_offer = BundleOffer([self.test_products[3], self.test_products[4], self.test_products[5] ], 3) self.test_basket = Basket(self.test_catalouge, [self.test_offer]) # Add 2 small shampoo self.test_basket.add_product(self.test_products[3], 2) # Add 1 medium shampoo self.test_basket.add_product(self.test_products[4]) # Add 3 large shampoo self.test_basket.add_product(self.test_products[5], 3) # One large shampoo for free (3.5) and One small shampoo for free (2.00) # Total: 5.5 self.assertEqual(self.test_basket.calculate_discount(), 5.5) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[3].price * 2 + self.test_products[4].price + self.test_products[5].price * 3 - self.test_basket.calculate_discount(), 2)) def test_basket_discount_multiple_bundle_offers(self): # With bundle offer on all shampoo buy 3 get the cheapest free self.bundle_offer_all_shampoo = BundleOffer([self.test_products[3], self.test_products[4], self.test_products[5] ], 3) # With bundle offer on small shampoo and sardines buy 2 get the cheapest free self.bundle_offer_small_shampoo_and_sardines = BundleOffer([self.test_products[3], self.test_products[2] ], 2) self.test_basket = Basket(self.test_catalouge, [ self.bundle_offer_all_shampoo, self.bundle_offer_small_shampoo_and_sardines ]) # Add 1 sardines self.test_basket.add_product(self.test_products[2], 1) # Add 3 small shampoo self.test_basket.add_product(self.test_products[3], 3) # Add 2 medium shampoo self.test_basket.add_product(self.test_products[4], 2) # Add 3 large shampoo self.test_basket.add_product(self.test_products[5], 3) # One large shampoo for free (3.5) # Two small shampoo for free (4.00) # Total: 7.5 self.assertEqual(self.test_basket.calculate_discount(), 7.5) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[2].price + self.test_products[3].price * 3 + self.test_products[4].price * 2 + self.test_products[5].price * 3 - self.test_basket.calculate_discount(), 2)) def test_basket_discount_all_offers(self): # With bundle offer on all shampoo buy 3 get the cheapest free self.bundle_offer_all_shampoo = BundleOffer([self.test_products[3], self.test_products[4], self.test_products[5] ], 3) # With bundle offer on small shampoo and sardines buy 2 get the cheapest free self.bundle_offer_small_shampoo_and_sardines = BundleOffer([self.test_products[3], self.test_products[2] ], 2) self.buy_and_get_free_offer_baked_beans = BuyAndGetFreeOffer(self.test_products[0], 2, 1) self.percentage_offer_baked_beans = PercentageOffer(self.test_products[0], 0.25) self.test_basket = Basket(self.test_catalouge, [ self.bundle_offer_all_shampoo, self.bundle_offer_small_shampoo_and_sardines, self.buy_and_get_free_offer_baked_beans, self.percentage_offer_baked_beans ]) # Add 4 Baked Beans self.test_basket.add_product(self.test_products[0], 4) # Add 1 Biscuits self.test_basket.add_product(self.test_products[1]) # Add 1 sardines self.test_basket.add_product(self.test_products[2], 1) # Add 3 small shampoo self.test_basket.add_product(self.test_products[3], 3) # Add 2 medium shampoo self.test_basket.add_product(self.test_products[4], 2) # Add 3 large shampoo self.test_basket.add_product(self.test_products[5], 3) # Baked Beans Buy 2 get one free: 0.99 # Baked Beans 25% percent discount: 0.99 * 0.25 = 0.25 # One large shampoo for free (3.5) # Two small shampoo for free (4.00) # Total: 7.5 + 0.99 + 0.25 = 8.74 self.assertEqual(self.test_basket.calculate_discount(), 8.74) self.assertEqual(self.test_basket.calculate_total(), round_half_up(self.test_products[0].price * 4 + self.test_products[1].price + self.test_products[2].price + self.test_products[3].price * 3 + self.test_products[4].price * 2 + self.test_products[5].price * 3 - self.test_basket.calculate_discount(), 2))
class TestOffer(TestCase): def setUp(self): self.test_products = [ Product('Baked Beans', 0.99), Product('Biscuits', 1.20), Product('Sardines', 1.89), Product('Shampoo (Small)', 2.00), Product('Shampoo (Medium)', 2.50), Product('Shampoo (Large)', 3.50), ] self.test_catalouge = Catalogue(self.test_products) def test_buy_and_get_free_offer(self): """ Tests the creation of a valid buy and get free offer """ # Baked Beans buy one get one free self.test_offer = BuyAndGetFreeOffer(self.test_products[0], 1, 1) self.assertEqual(self.test_offer.product, self.test_products[0].name) self.assertEqual(self.test_offer.buy_quantity, 1) self.assertEqual(self.test_offer.free_quantity, 1) def test_buy_and_get_free_offer_discount(self): # Baked Beans buy one get one free self.test_offer = BuyAndGetFreeOffer(self.test_products[0], 1, 1) self.test_basket = Basket(self.test_catalouge) self.test_basket.add_product(self.test_products[0], 1) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], 0.00) # Add 1 more Baked Beans for a total of 2 self.test_basket.add_product(self.test_products[0], 1) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], self.test_products[0].price) def test_percentage_offer(self): """ Tests the creation of a valid percentage offer """ # Baked Beans 25% off self.test_offer = PercentageOffer(self.test_products[0], 0.25) self.assertEqual(self.test_offer.product, self.test_products[0].name) self.assertEqual(self.test_offer.discount_percent, 0.25) def test_percentage_offer_discount(self): # Baked Beans buy one get one free self.test_offer = PercentageOffer(self.test_products[0], 0.25) self.test_basket = Basket(self.test_catalouge) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], 0.00) self.test_basket.add_product(self.test_products[0], 1) self.assertEqual(self.test_offer.get_discount( self.test_basket.products)[0], round_half_up(self.test_products[0].price * 0.25, 2) ) # Add 1 more Baked Beans for a total of 2 self.test_basket.add_product(self.test_products[0], 1) self.assertEqual(self.test_offer.get_discount( self.test_basket.products)[0], round_half_up(self.test_products[0].price * 0.25, 2) ) def test_bundle_offer(self): """ Tests the creation of a valid percentage offer """ # Baked Beans, Biscuits, Sardines self.test_offer = BundleOffer([self.test_products[0], self.test_products[1], self.test_products[2] ], 3) self.assertEqual(len(self.test_offer.bundle_items), 3) self.assertEqual(type(self.test_offer.bundle_items), list) self.assertEqual(type(self.test_offer.bundle_items[0]), str) self.assertEqual(self.test_offer.required_items, 3) def test_bundle_offer_discount(self): # Baked Beans, Biscuits, Sardines self.test_offer = BundleOffer([self.test_products[0], self.test_products[1], self.test_products[2] ], 3) self.test_basket = Basket(self.test_catalouge) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], 0.00) self.test_basket.add_product(self.test_products[0], 1) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], 0.00) self.test_basket.add_product(self.test_products[1], 1) self.test_basket.add_product(self.test_products[2], 1) self.test_basket.add_product(self.test_products[3], 1) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], self.test_products[0].price) # Add 1 more Baked Beans for a total of 2 self.test_basket.add_product(self.test_products[0], 1) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], self.test_products[0].price) # Add 1 more Biscuits for a total of 2 self.test_basket.add_product(self.test_products[1], 1) self.assertEqual(self.test_offer.get_discount(self.test_basket.products)[0], self.test_products[1].price)
class TestBasket(unittest.TestCase): """ Check the Basket performs as expected """ def setUp(self): """ Create the two baskets described in assignment.py """ offers = [ GetOneFree(product_sku="beans", min_items=2), PercentageDiscount(product_sku="sardines", discount_percent=25), ] self.basket_1 = Basket(product_catalog=catalog.products, offers=offers) self.basket_1.add_product("beans") self.basket_1.add_product("beans") self.basket_1.add_product("beans") self.basket_1.add_product("beans") self.basket_1.add_product("biscuits") self.basket_2 = Basket(product_catalog=catalog.products, offers=offers) self.basket_2.add_product("beans") self.basket_2.add_product("beans") self.basket_2.add_product("biscuits") self.basket_2.add_product("sardines") self.basket_2.add_product("sardines") def test_subtotal(self): """ Check subtotal() returns the expected sum of all products' prices """ self.assertEqual(self.basket_1.subtotal(), 5.16) self.assertEqual(self.basket_2.subtotal(), 6.96) def test_discount(self): """ Check discount() tells us how much is discounted based on added offers """ self.assertEqual(self.basket_1.discount(), 0.99) self.assertEqual(self.basket_2.discount(), 0.95) def test_total(self): """ Check total() returns the expected total for both baskets """ self.assertEqual(self.basket_1.total(), 4.17) self.assertEqual(self.basket_2.total(), 6.01) def test_basket_3(self): """ Check subtotal, discount and total are as expected for basket 3 """ basket_3 = Basket( product_catalog=catalog.products, offers=[CheapestFree(product_category="shampoo", min_items=3)], ) basket_3.add_product("shampoo-l") basket_3.add_product("shampoo-l") basket_3.add_product("shampoo-l") basket_3.add_product("shampoo-m") basket_3.add_product("shampoo-s") basket_3.add_product("shampoo-s") self.assertEqual(basket_3.subtotal(), 17) self.assertEqual(basket_3.discount(), 5.5) self.assertEqual(basket_3.total(), 11.5)