Ejemplo n.º 1
0
    def test_can_add_items_of_the_same_type_into_basket(self):
        baked_beans = Item("Baked Beans", 0.99)
        basket = Basket()
        basket.add_items(baked_beans, 4)
        basket.add_items(baked_beans, 1)

        self.assertEqual(basket.items,{baked_beans: 5})
Ejemplo n.º 2
0
    def setUp(self):
        """Define the catalogue of items and basket"""

        self.baked_beans = Item("Baked Beans", 0.99)
        self.biscuits  = Item("Biscuits", 1.20)
        self.sardines  = Item("Sardines", 1.89)
        self.shampoo_small  = Item("Shampoo (Small)", 2.00)
        self.shampoo_medium = Item("Shampoo (Medium)", 2.50)
        self.shampoo_large = Item("Shampoo (Large)", 3.50)
        self.basket = Basket()
        self.catalogue = CatalogueRepository()
Ejemplo n.º 3
0
    def setUp(self):
        """Define the catalogue of items, basket and current offers"""

        self.baked_beans = Item("Baked Beans", 0.99)
        self.biscuits  = Item("Biscuits", 1.20)
        self.sardines  = Item("Sardines", 1.89)
        self.shampoo_small  = Item("Shampoo (Small)", 2.00)
        self.shampoo_medium = Item("Shampoo (Medium)", 2.50)
        self.shampoo_large = Item("Shampoo (Large)", 3.50)
        self.basket = Basket()
        self.many_of_type_offer = {"Baked Beans" : 3}
        self.percentage_offer = {"Sardines" : 0.25}
        self.cheapest_item_offer = [["Shampoo (Small)", "Shampoo (Medium)", "Shampoo (Large)"]]
        self.users_offer = Offer(self.basket.items)
        self.cost = BasketCostCalculator(self.basket)
Ejemplo n.º 4
0
from shopping_basket_classes import Item, Basket, Offer, BasketCostCalculator, ItemOutOfStockException, CatalogueRepository

catalogue = CatalogueRepository()
catalogue.get_items()

#Products with discount
many_of_type_offer = catalogue.get_many_of_type_offer_products()
percentage_offer = catalogue.get_percentage_offer_products()
cheapest_item_offer = catalogue.get_cheapest_product_offer_products()

#Basket chosen by client
basket = Basket()
try:
    basket.add_items(catalogue.items_by_name["Baked Beans"], 4)
except ItemOutOfStockException:
    print('Sorry, the item is currently out of stock')

try:
    basket.add_items(catalogue.items_by_name["Biscuits"], 1)
except ItemOutOfStockException:
    print('Sorry, the item is currently out of stock')

users_offer = Offer(basket.items)
cost = BasketCostCalculator(basket)

subtotal = cost.calculate_subtotal()

# in a real project calculate_total_discount() would accept a list of OfferStrategies and iterate through all of them.
discount = cost.calculate_total_discount(users_offer, many_of_type_offer, percentage_offer, cheapest_item_offer)
total = cost.calculate_total(users_offer, many_of_type_offer, percentage_offer, cheapest_item_offer)
Ejemplo n.º 5
0
class TestOffer(unittest.TestCase):
    """Test for the class Offer"""

    def setUp(self):
        """Define the catalogue of items and basket"""

        self.baked_beans = Item("Baked Beans", 0.99)
        self.biscuits  = Item("Biscuits", 1.20)
        self.sardines  = Item("Sardines", 1.89)
        self.shampoo_small  = Item("Shampoo (Small)", 2.00)
        self.shampoo_medium = Item("Shampoo (Medium)", 2.50)
        self.shampoo_large = Item("Shampoo (Large)", 3.50)
        self.basket = Basket()
        self.catalogue = CatalogueRepository()

    def test_calculate_three_baked_beans_offer(self):
        self.basket.add_items(self.baked_beans, 4)
        many_of_type_offer = self.catalogue.get_many_of_type_offer_products()
        users_offer = Offer(self.basket.items)
        discount_amount = users_offer.calculate_many_of_a_type_offer(many_of_type_offer)

        self.assertEqual(discount_amount, 0.99)

    def test_calculate_baked_beans_and_biscuit_offer(self):
        self.basket.add_items(self.baked_beans, 4)
        self.basket.add_items(self.biscuits, 22)
        many_of_type_offer = {"Baked Beans" : 3, "Biscuits": 5} #Offer a baked bean for every 3 and a biscuit for every 5
        users_offer = Offer(self.basket.items)
        discount_amount = users_offer.calculate_many_of_a_type_offer(many_of_type_offer)

        self.assertEqual(discount_amount, 5.79) #(Biscuit: 22%5 = 4, 4*1.20 = 4.80) (4%3 = 1, 1*0.99 = 0.99)

    def test_calculate_sardines_percentage_offer(self):
        self.basket.add_items(self.sardines, 4)
        percentage_offer = self.catalogue.get_percentage_offer_products()
        users_offer = Offer(self.basket.items)
        discount_amount = users_offer.calculate_percentage_offer(percentage_offer)

        self.assertEqual(discount_amount, 1.89) #(0.25*1.89)*4

    def test_calculate_cheapest_item_offer(self):
        self.basket.add_items(self.shampoo_large, 3)
        self.basket.add_items(self.shampoo_medium, 1)
        self.basket.add_items(self.shampoo_small, 2)
        cheapest_item_offer = self.catalogue.get_cheapest_product_offer_products()
        users_offer = Offer(self.basket.items)
        discount_amount = users_offer.calculate_cheapest_item_offer(cheapest_item_offer)

        self.assertEqual(discount_amount, 5.5)

    def test_calculate_cheapest_item_offer_for_two_groups(self):
        self.basket.add_items(self.shampoo_large, 3)
        self.basket.add_items(self.shampoo_medium, 1)
        self.basket.add_items(self.shampoo_small, 2)
        self.basket.add_items(self.sardines, 4)
        self.basket.add_items(self.biscuits, 3)
        cheapest_item_offer = [["Shampoo (Small)", "Shampoo (Medium)", "Shampoo (Large)"], ["Sardines", "Biscuits"]]
        users_offer = Offer(self.basket.items)
        discount_amount = users_offer.calculate_cheapest_item_offer(cheapest_item_offer)

        self.assertEqual(discount_amount, 8.59)
Ejemplo n.º 6
0
    def test_cannot_add_unavailable_items_into_basket(self):
        baked_beans = Item("Baked Beans", 0.99)
        baked_beans.set_out_of_stock()
        basket = Basket()

        self.assertRaises(ItemOutOfStockException, basket.add_items, baked_beans, 1)
Ejemplo n.º 7
0
    def test_can_insert_items_into_empty_basket(self):
        baked_beans = Item("Baked Beans", 0.99)
        basket = Basket()
        basket.add_items(baked_beans, 4)

        self.assertEqual(basket.items,{baked_beans: 4})
Ejemplo n.º 8
0
class TestBasketCostCalculator(unittest.TestCase):
    """Test for the class BasketCostCalculator"""

    def setUp(self):
        """Define the catalogue of items, basket and current offers"""

        self.baked_beans = Item("Baked Beans", 0.99)
        self.biscuits  = Item("Biscuits", 1.20)
        self.sardines  = Item("Sardines", 1.89)
        self.shampoo_small  = Item("Shampoo (Small)", 2.00)
        self.shampoo_medium = Item("Shampoo (Medium)", 2.50)
        self.shampoo_large = Item("Shampoo (Large)", 3.50)
        self.basket = Basket()
        self.many_of_type_offer = {"Baked Beans" : 3}
        self.percentage_offer = {"Sardines" : 0.25}
        self.cheapest_item_offer = [["Shampoo (Small)", "Shampoo (Medium)", "Shampoo (Large)"]]
        self.users_offer = Offer(self.basket.items)
        self.cost = BasketCostCalculator(self.basket)

    def test_costs_baked_beans_sardines(self):
        self.basket.add_items(self.baked_beans, 4)
        self.basket.add_items(self.biscuits, 1)
        subtotal = self.cost.calculate_subtotal()
        discount = self.cost.calculate_total_discount(self.users_offer, self.many_of_type_offer, self.percentage_offer, self.cheapest_item_offer)
        total = self.cost.calculate_total(self.users_offer, self.many_of_type_offer, self.percentage_offer, self.cheapest_item_offer)

        self.assertEqual(subtotal, 5.16)
        self.assertEqual(discount, 0.99)
        self.assertEqual(total, 4.17)

    def test_costs_baked_beans_biscuits_sardines(self):
        self.basket.add_items(self.baked_beans, 2)
        self.basket.add_items(self.biscuits, 1)
        self.basket.add_items(self.sardines, 2)
        subtotal = self.cost.calculate_subtotal()
        discount = self.cost.calculate_total_discount(self.users_offer, self.many_of_type_offer, self.percentage_offer, self.cheapest_item_offer)
        total = self.cost.calculate_total(self.users_offer, self.many_of_type_offer, self.percentage_offer, self.cheapest_item_offer)

        self.assertEqual(subtotal, 6.96)
        self.assertEqual(discount, 0.945)
        self.assertEqual(total, 6.01)

    def test_costs_of_shampoos(self):
        self.basket.add_items(self.shampoo_large, 3)
        self.basket.add_items(self.shampoo_medium, 1)
        self.basket.add_items(self.shampoo_small, 2)
        subtotal = self.cost.calculate_subtotal()
        discount = self.cost.calculate_total_discount(self.users_offer, self.many_of_type_offer, self.percentage_offer, self.cheapest_item_offer)
        total = self.cost.calculate_total(self.users_offer, self.many_of_type_offer, self.percentage_offer, self.cheapest_item_offer)

        self.assertEqual(subtotal, 17.0)
        self.assertEqual(discount, 5.5)
        self.assertEqual(total, 11.5)