def test_add_item_to_basket(): basket = Basket() basket.add_item("peach", 2) assert len(basket.contents) == 1 assert basket.contents["peach"] == 2
def test_add_same_item_increases_quantity(): basket = Basket() basket.add_item("peach", 1) basket.add_item("peach", 3) assert basket.contents["peach"] == 4
def test_calculate_percentage_discount_for_one_item(): # Discount tomato by 25% offers = {"BuyXGetY": {"": []}, "PercentageDiscount": {"tomato": 0.25}} basket = Basket() basket.add_item("tomato", 1) subtotal, discount, total = checkout_basket(basket, catalogue, offers) assert subtotal == 0.60 assert discount == 0.15 assert total == 0.45
def test_single_item(): basket = Basket({"baked_beans": 1}) catalogue = {"baked_beans": 0.99} basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=[]) subtotal, discount, total = basket_pricer.totals() assert subtotal == 0.99 assert discount == 0.0 assert total == 0.99
def test_multiple_of_one_item(): basket = Basket({"baked_beans": 2}) catalogue = {"baked_beans": 0.99} basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=[]) subtotal, discount, total = basket_pricer.totals() assert subtotal == 1.98 assert discount == 0.0 assert total == 1.98
def test_checkout_basket_with_empty_basket(): basket = Basket() subtotal, discount, total = checkout_basket(basket, catalogue) assert subtotal == 0 assert discount == 0 assert total == 0
def test_multiple_items(): basket = Basket({"baked_beans": 2, "biscuits": 1}) catalogue = {"baked_beans": 0.99, "biscuits": 1.20} basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=[]) subtotal, discount, total = basket_pricer.totals() print(subtotal, discount, total) assert subtotal == 3.18 assert discount == 0.0 assert total == 3.18
def test_add_multiple_items_to_basket(): basket = Basket() basket.add_item("peach", 1) basket.add_item("tomato", 3) basket.add_item("corn", 8) assert len(basket.contents) == 3 assert basket.contents["tomato"] == 3 assert basket.contents["peach"] == 1 assert basket.contents["corn"] == 8
def test_buy_2_get_1_free(): basket = Basket({"baked_beans": 3, "biscuits": 1}) catalogue = {"baked_beans": 0.99, "biscuits": 1.20} offers = [BuyXGetYFree("baked_beans", x=2, y=1)] basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=offers) subtotal, discount, total = basket_pricer.totals() assert subtotal == 4.17 assert discount == 0.99 assert total == 3.18
def test_buy_4_get_2_free(): basket = Basket({"baked_beans": 6, "biscuits": 1}) catalogue = {"baked_beans": 0.99, "biscuits": 1.20} offers = [BuyXGetYFree("baked_beans", x=2, y=1)] basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=offers) subtotal, discount, total = basket_pricer.totals() assert subtotal == 7.14 assert discount == 1.98 assert total == 5.16
def test_discount_not_in_basket(): basket = Basket({"biscuits": 1}) catalogue = {"sardines": 1.89, "biscuits": 1.20} offers = [DiscountPercent("sardines", discount_percent=0.25)] basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=offers) subtotal, discount, total = basket_pricer.totals() assert subtotal == 1.20 assert discount == 0.0 assert total == 1.20
def test_multiple_item_discount(): basket = Basket({"sardines": 2, "biscuits": 1}) catalogue = {"sardines": 1.89, "biscuits": 1.20} offers = [DiscountPercent("sardines", discount_percent=25)] basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=offers) subtotal, discount, total = basket_pricer.totals() assert subtotal == 4.98 assert discount == 0.94 # .95 actually, assert total == 4.04 # 4.03 actually, see below
def test_calculate_buy_x_get_y_for_one_item_type(): # Buy 2 Get 1 Free on peaches offers = { "BuyXGetY": { "peach": [2, 1] }, "PercentageDiscount": { "cornflakes": 0.25 } } basket = Basket() basket.add_item("peach", 3) subtotal, discount, total = checkout_basket(basket, catalogue, offers) assert subtotal == 4.59 assert discount == 1.53 assert total == 3.06
def test_buy_n_get_cheapest_a_free(): basket = Basket({"shampoo_l": 3, "shampoo_m": 1, "shampoo_s": 2}) catalogue = {"shampoo_l": 3.50, "shampoo_m": 2.50, "shampoo_s": 2} offers = [ BuyNGetCheapestAFree({"shampoo_l", "shampoo_m", "shampoo_s"}, n=3, a=1) ] basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=offers) subtotal, discount, total = basket_pricer.totals() assert subtotal == 17 assert discount == 5.50 assert total == 11.5
def test_multiple_offers(): basket = Basket({"baked_beans": 3, "biscuits": 1, "sardines": 1}) catalogue = {"baked_beans": 0.99, "biscuits": 1.20, "sardines": 1.89} offers = [ BuyXGetYFree("baked_beans", x=2, y=1), DiscountPercent("sardines", 25) ] basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=offers) subtotal, discount, total = basket_pricer.totals() assert subtotal == 6.06 assert discount == 0.99 + 0.47 assert total == 4.60
def test_buy_three_get_two_and_buy_four_get_one(): # Buy 4 Get 1 Free on peaches && Buy 3 Get 2 Free on tomatos offers = { "BuyXGetY": { "peach": [4, 1], "tomato": [3, 2] }, "PercentageDiscount": { "cornflakes": 0.25 }, } basket = Basket() basket.add_item("peach", 16) basket.add_item("tomato", 23) subtotal, discount, total = checkout_basket(basket, catalogue, offers) assert subtotal == 38.28 assert discount == 9.39 assert total == 28.89
def test_calculate_percentage_discount_for_multiple_items(): # Discount tomato by 25% and cornflakes by 50% offers = { "BuyXGetY": { "": [] }, "PercentageDiscount": { "tomato": 0.25, "cornflakes": 0.50 }, } basket = Basket() basket.add_item("tomato", 1) basket.add_item("cornflakes", 2) subtotal, discount, total = checkout_basket(basket, catalogue, offers) assert subtotal == 5.30 assert discount == 2.50 assert total == 2.80
def test_calculate_buy_x_get_y_for_multiple_items(): # Buy 2 Get 1 Free on peaches && Buy 3 Get 2 Free on tomatos offers = { "BuyXGetY": { "peach": [2, 1], "tomato": [3, 2] }, "PercentageDiscount": { "cornflakes": 0.25 }, } basket = Basket() basket.add_item("peach", 3) basket.add_item("tomato", 15) subtotal, discount, total = checkout_basket(basket, catalogue, offers) assert subtotal == 13.59 assert discount == 5.13 assert total == 8.46
def test_both_offers_being_used_on_seperate_items(): # Buy 3 Get 1 Free on peaches && 25% off cornflakes offers = { "BuyXGetY": { "peach": [3, 1], "tomato": [3, 2] }, "PercentageDiscount": { "cornflakes": 0.25 }, } basket = Basket() basket.add_item("peach", 4) basket.add_item("cornflakes", 5) subtotal, discount, total = checkout_basket(basket, catalogue, offers) assert subtotal == 17.87 assert discount == 4.47 assert total == 13.40
def test_multiple_buy_x_get_y_offers_with_excess_and_inelegible_quantities(): # Buy 3 Get 1 Free on peaches && Buy 3 Get 2 Free on tomatos offers = { "BuyXGetY": { "peach": [3, 1], "tomato": [3, 2] }, "PercentageDiscount": { "cornflakes": 0.25 }, } basket = Basket() basket.add_item("peach", 23) basket.add_item("tomato", 4) basket.add_item("corn", 5) subtotal, discount, total = checkout_basket(basket, catalogue, offers) assert subtotal == 37.59 assert discount == 7.65 assert total == 29.94
def test_two_items_quantity_one(self): basket = Basket([Item(100.0, 1), Item(100.0, 1)]) self.assertEqual(basket.total(), 200)
def empty_basket(): """ Returns an empty basket """ return Basket()
#!/usr/bin/env python from shopping_basket import Basket, BasketPricer, BuyXGetYFree, DiscountPercent if __name__ == "__main__": basket = Basket({"baked_beans": 3, "biscuits": 1, "sardines": 1}) catalogue = {"baked_beans": 0.99, "biscuits": 1.20, "sardines": 1.89} offers = [BuyXGetYFree("baked_beans", x=2, y=1), DiscountPercent("sardines", 25)] basket_pricer = BasketPricer(basket=basket, catalogue=catalogue, offers=offers) subtotal, discount, total = basket_pricer.totals() assert subtotal == 6.06 assert discount == 0.99 + 0.47 assert total == 4.60 print(basket.items) print(basket_pricer.formatted_totals())
def test_new_basket_returns_empty_dict(): basket = Basket() assert basket.contents == {}
def test_add_basket(): basket = Basket({"baked_beans": 2, "biscuits": 1}) basket.add("sardines") assert "sardines" in basket.items assert basket.items["sardines"] == 1
def test_empty_basket(): basket_pricer = BasketPricer(basket=Basket({}), catalogue={}, offers=[]) subtotal, discount, total = basket_pricer.totals() assert subtotal == 0.0 assert discount == 0.0 assert total == 0.0
def empty_basket(): return Basket()
def test_single_item_quantity(self): basket = Basket([Item(100.0, 2)]) self.assertEqual(basket.total(), 200.0)
def test_empty_basket_total(self): basket = Basket([]) self.assertEqual(basket.total(), 0)
def test_default_basket(): basket = Basket() assert basket.sub_total == 0.0 assert basket.discount == 0.0 assert basket.total == 0.0 assert basket.items == []