示例#1
0
class DiscountTestCase(unittest.TestCase):
    def setUp(self):
        self.discount = Discount()
        self.product = Product()
        self.product.add_product(name="Grape", price=1.0, package_type=PackageTypes.TIN.value)
        self.product.add_product(name="Milk", price=2.0, package_type=PackageTypes.BOTTLE.value)

    def tearDown(self):
        self.discount = list()
        self.product = list()

    def test_add_discount(self):
        self.assertListEqual(self.discount.discount, list())
        self.discount.add(10, self.product.get_product('Grape'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="Grape 10 % off")
        self.assertListEqual(self.discount.discount, [{'product': 'Grape', 'value': 10, '_when_field': None,
                                                       '_when_op': None, '_when_value': None, '_to': None,
                                                       'discount_type': 'PERCENTAGE', 'description': 'Grape 10 % off'}])

    def test_remove_all_discount(self):
        self.assertListEqual(self.discount.discount, list())
        self.discount.add(10, self.product.get_product('Grape'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="Grape 10 % off")
        self.assertListEqual(self.discount.discount, [{'product': 'Grape', 'value': 10, '_when_field': None,
                                                       '_when_op': None, '_when_value': None, '_to': None,
                                                       'discount_type': 'PERCENTAGE', 'description': 'Grape 10 % off'}])
        self.discount.remove_all_discount()
        self.assertListEqual(self.discount.discount, list())

    def test_get_discount_by_product_name_existent(self):
        self.assertListEqual(self.discount.discount, list())
        self.discount.add(10, self.product.get_product('Grape'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="Grape 10 % off")
        self.discount.add(20, self.product.get_product('Milk'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="milk 20 % off")
        self.assertListEqual(self.discount.discount, [{'product': 'Grape', 'value': 10, '_when_field': None,
                                                       '_when_op': None, '_when_value': None, '_to': None,
                                                       'discount_type': 'PERCENTAGE', 'description': 'Grape 10 % off'},
                                                      {'product': 'Milk', 'value': 20, '_when_field': None,
                                                       '_when_op': None, '_when_value': None, '_to': None,
                                                       'discount_type': 'PERCENTAGE', 'description': 'milk 20 % off'}])
        self.assertDictEqual(self.discount.get_by_product('Milk'), {'product': 'Milk', 'value': 20, '_when_field': None,
                                                                    '_when_op': None, '_when_value': None, '_to': None,
                                                                    'discount_type': 'PERCENTAGE',
                                                                    'description': 'milk 20 % off'})

    def test_get_discount_by_product_name_not_existent(self):
        self.assertListEqual(self.discount.discount, list())
        self.discount.add(10, self.product.get_product('Grape'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="Grape 10 % off")
        self.discount.add(20, self.product.get_product('Milk'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="milk 20 % off")
        self.assertListEqual(self.discount.discount, [{'product': 'Grape', 'value': 10, '_when_field': None,
                                                       '_when_op': None, '_when_value': None, '_to': None,
                                                       'discount_type': 'PERCENTAGE',
                                                       'description': 'Grape 10 % off'},
                                                      {'product': 'Milk', 'value': 20, '_when_field': None,
                                                       '_when_op': None, '_when_value': None, '_to': None,
                                                       'discount_type': 'PERCENTAGE',
                                                       'description': 'milk 20 % off'}])
        self.assertFalse(self.discount.get_by_product('Banana'))

    def test_get_all_discount(self):
        self.assertListEqual(self.discount.discount, list())
        self.discount.add(10, self.product.get_product('Grape'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="Grape 10 % off")
        self.discount.add(20, self.product.get_product('Milk'), _when_field=None, _when_op=None, _when_value=None,
                          _to=None, discount_type=DiscountTypes.PERCENTAGE.name, description="milk 20 % off")
        self.assertListEqual(self.discount.get_all(), [{'product': 'Grape', 'value': 10, '_when_field': None,
                                                        '_when_op': None, '_when_value': None, '_to': None,
                                                        'discount_type': 'PERCENTAGE',
                                                        'description': 'Grape 10 % off'},
                                                       {'product': 'Milk', 'value': 20, '_when_field': None,
                                                        '_when_op': None, '_when_value': None, '_to': None,
                                                        'discount_type': 'PERCENTAGE',
                                                        'description': 'milk 20 % off'}])
示例#2
0
    items.add_product(name="Bread",
                      price=0.80,
                      package_type=PackageTypes.LOAF.value)
    items.add_product(name="Milk",
                      price=1.3,
                      package_type=PackageTypes.BOTTLE.value)
    items.add_product(name="Apple",
                      price=1.0,
                      package_type=PackageTypes.BAG.value)

    print("---- Products list")
    print(items.get_list_items())

    discount = Discount()
    discount.add(
        product=items.get_product('Apple'),
        value=10,
        description="Apples have 10% off their normal price this week")

    discount.add(
        product=items.get_product('Soup'),
        value=50,
        _to=items.get_product('Bread'),
        _when_field="qty",
        _when_op="ge",
        _when_value="2",
        description="Buy 2 tins of soup and get a loaf of bread for half price"
    )

    cart = Cart()
示例#3
0
class Application:
    """This class manage the program display.
    This class manages the different menus of the program.

    """
    def __init__(self):
        self.category = ""
        self.products = []
        self.products_sub = []
        self.product_selected = []
        self.prod_sub = []
        self.leave_main_menu = 1
        self.leave_category_choice = 1
        self.leave_choice_product = 1
        self.leave_choice_sub = 1
        self.leave_save_sub = 1
        self.first_number = 0
        self.choice_menu = ""
        self.input_product = ""
        self.product_sub = ""
        self.input_product_sub = ""
        self.base = Database()
        self.category_table = Category()
        self.product_table = Product()
        self.substitution_table = Substitution()

    def initialise_bdd(self):
        """Method to reset database and save categories"""
        print(fr.FR[1])
        self.base.create_database("sql/p5.sql")
        print(fr.FR[2])
        self.category_table.save_category()
        print(fr.FR[3])

    def save_product_bdd(self):
        """Method to retrieve the products and save them in the database"""
        for element in config.CATEGORIES:
            category = ClearData(element)
            category.get_data_api()
            category.generate_products_list()
            self.product_table.save_product(
                category.products,
                config.CATEGORIES.index(element) + 1)

    def main(self):
        """Method to display the different choices of the main menu"""
        while self.leave_main_menu:
            print(fr.FR[4], fr.FR[5], fr.FR[6], fr.FR[7])
            self.choice_menu = input(fr.FR[8])
            self.main_menu_input()

    def main_menu_input(self):
        """Method to manage entries in the main menu"""
        if self.choice_menu == "1":
            self.category_choice()
        elif self.choice_menu == "2":
            print(fr.FR[9])
            for element in self.substitution_table.get_substitution():
                for substitution in element:
                    sub_prod = self.product_table.get_product(substitution)
                    print(sub_prod[0][1] + " - " + sub_prod[0][2] + " - " +
                          sub_prod[0][3] + " - " + sub_prod[0][4])
                print("\n")
        elif self.choice_menu == "3":
            self.initialise_bdd()
            self.save_product_bdd()
        elif self.choice_menu == "4":
            self.leave_main_menu -= 1

    def category_choice(self):
        """Method to display the different choices in the categories menu"""
        self.leave_category_choice = 1
        while self.leave_category_choice:
            print(fr.FR[15])
            for element in config.CATEGORIES:
                print(
                    str(config.CATEGORIES.index(element) + 1) + " : " +
                    element)
            self.category_choice_input()

    def category_choice_input(self):
        """Method to manage entries in the categories menu"""
        self.category = input(fr.FR[8])
        try:
            if self.category == "q":
                self.leave_category_choice -= 1
            elif 1 <= int(self.category) <= len(config.CATEGORIES):
                print(self.category)
                self.products = self.product_table.get_list_product(
                    self.category)
                self.products_sub = self.product_table.get_list_product(
                    self.category)
                self.choice_product()
                self.leave_category_choice -= 1
        except ValueError:
            print(fr.FR[10])

    def display_product(self, list_products):
        """Method to display products"""
        for element in list_products[self.first_number:self.first_number +
                                     config.NUMBER_PRODUCT_DISPLAY]:
            print(
                str(list_products.index(element) + 1) + " - " + element[1] +
                " - " + element[4].upper() + " - " + element[2] + " - " +
                element[3])

    def choice_product(self):
        """Method to display the different choices in the products menu"""
        self.first_number = 0
        self.leave_choice_product = 1
        while self.leave_choice_product:
            print(fr.FR[11])
            self.display_product(self.products)
            self.input_product = input(fr.FR[12])
            self.choice_product_input()

    def choice_product_input(self):
        """Method to manage entries in the products menu"""
        try:
            if self.input_product == "s" and (
                    self.first_number + config.NUMBER_PRODUCT_DISPLAY)\
                    < len(self.products):
                self.first_number += config.NUMBER_PRODUCT_DISPLAY
            elif self.input_product == "p" and self.first_number > 0:
                self.first_number -= config.NUMBER_PRODUCT_DISPLAY
            elif self.input_product == "q":
                self.leave_choice_product -= 1
            elif 1 <= int(self.input_product) <= len(self.products):
                self.product_selected = self.products[int(self.input_product) -
                                                      1][0]
                del self.products_sub[int(self.input_product) - 1]
                self.choice_sub()
                self.leave_choice_product -= 1
        except ValueError:
            print(fr.FR[10])

    def choice_sub(self):
        """Method to display the different choices in the substitute menu"""
        self.first_number = 0
        self.leave_choice_sub = 1
        while self.leave_choice_sub:
            print(fr.FR[13])
            self.display_product(self.products_sub)
            self.input_product_sub = input(fr.FR[12])
            self.choice_sub_input()

    def choice_sub_input(self):
        """Method to manage entries in the substitute products menu"""
        try:
            if self.input_product_sub == "s" and (
                self.first_number + config.NUMBER_PRODUCT_DISPLAY) \
                    < len(self.products_sub):
                self.first_number += config.NUMBER_PRODUCT_DISPLAY
            elif self.input_product_sub == "p" and self.first_number > 0:
                self.first_number -= config.NUMBER_PRODUCT_DISPLAY
            elif self.input_product_sub == "q":
                self.leave_choice_sub -= 1
            elif 1 <= int(self.input_product_sub) <= len(self.products_sub):
                self.prod_sub = self.products_sub[int(self.input_product_sub) -
                                                  1][0]
                self.save_sub()
                self.leave_choice_sub -= 1
        except ValueError:
            print(fr.FR[10])

    def save_sub(self):
        """Method to ask to the user if he wants to register the substitute"""
        self.leave_save_sub = 1
        while self.leave_save_sub:
            print(fr.FR[14])
            self.product_sub = input("y / n : ")
            self.save_sub_input()

    def save_sub_input(self):
        """Method to manage entries for saved or not the substitute"""
        if self.product_sub == "y":
            self.substitution_table.save_product_substitute(
                self.product_selected, self.prod_sub)
            self.leave_save_sub -= 1
        elif self.product_sub == "n":
            self.leave_save_sub -= 1
        elif self.product_sub == "q":
            self.leave_save_sub -= 1
示例#4
0
class CartTestCase(unittest.TestCase):
    def setUp(self):
        self.cart = Cart()

        self.product = Product()
        self.product.add_product(name="Soup",
                                 price=0.65,
                                 package_type=PackageTypes.TIN.value)
        self.product.add_product(name="Bread",
                                 price=0.80,
                                 package_type=PackageTypes.LOAF.value)
        self.product.add_product(name="Milk",
                                 price=1.3,
                                 package_type=PackageTypes.BOTTLE.value)
        self.product.add_product(name="Apple", price=1.0, package_type="bag")

        self.discount = Discount()
        self.discount.add(
            product=self.product.get_product('Apple'),
            value=10,
            description="Apples have 10% off their normal price this week")

        self.discount.add(
            product=self.product.get_product('Soup'),
            value=50,
            _to=self.product.get_product('Bread'),
            _when_field="qty",
            _when_op="ge",
            _when_value="2",
            description=
            "Buy 2 tins of soup and get a loaf of bread for half price")

    def tearDown(self):
        self.cart = list()
        self.discount = list()
        self.product = list()

    def test_add_cart(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.assertListEqual(self.cart.items, [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }])
        self.assertEqual(self.cart.subtotal, 0.8)
        self.cart.add_item(self.product.get_product('Apple'))
        self.assertListEqual(self.cart.items, [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }, {
            'name': 'Apple',
            'price': 1.0,
            'package_type': 'bag',
            'qty': 1
        }])
        self.assertEqual(self.cart.subtotal, 1.8)

    def test_add_cart_already_existent(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.assertEqual(self.cart.subtotal, 0.8)
        self.assertListEqual(self.cart.items, [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }])
        self.cart.add_item(self.product.get_product('Bread'))
        self.assertEqual(self.cart.subtotal, 1.6)
        self.assertListEqual(self.cart.items, [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 2
        }])
        self.cart.add_item(self.product.get_product('Bread'))
        self.assertEqual(round(self.cart.subtotal, 2), 2.4)
        self.assertListEqual(self.cart.items, [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 3
        }])

    def test_get_items(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }])

    def test_find_item_by_product(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Apple'))
        self.assertDictEqual(
            self.cart.find_item(self.product.get_product('Bread')), {
                'name': 'Bread',
                'price': 0.8,
                'package_type': 'loaf',
                'qty': 1
            })

    def test_find_item_by_name(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Apple'))
        self.assertTupleEqual(self.cart.find_item_by_name('Bread'),
                              ({
                                  'name': 'Bread',
                                  'price': 0.8,
                                  'package_type': 'loaf',
                                  'qty': 1
                              }, 0))

    def test_find_item_by_name(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Apple'))
        self.assertTupleEqual(self.cart.find_item_by_name('Bread'),
                              ({
                                  'name': 'Bread',
                                  'price': 0.8,
                                  'package_type': 'loaf',
                                  'qty': 1
                              }, 0))

    def test_remove_item(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Apple'))
        self.cart.remove_item('Apple')
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }])

    def test_remove_item_not_existent(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Apple'))
        self.cart.remove_item('Banana')
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }, {
            'name': 'Apple',
            'price': 1.0,
            'package_type': 'bag',
            'qty': 1
        }])

    def test_remove_all(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Apple'))
        self.cart.remove_all()
        self.assertListEqual(self.cart.get_items(), list())

    def test_add_total_without_discount(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }])
        self.assertEqual(round(self.cart.total, 2), 0.8)

    def test_add_total_with_10_percent_discount(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Apple'))
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'discount_value': 0.1,
            'name': 'Apple',
            'new_price': 0.9,
            'price': 1.0,
            'package_type': 'bag',
            'qty': 1
        }])
        self.assertEqual(round(self.cart.total, 2), 0.9)

    def test_add_total_with_10_percent_discount_and_product_without(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Apple'))
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }, {
            'name': 'Apple',
            'price': 1.0,
            'package_type': 'bag',
            'qty': 1,
            'discount_value': 0.1,
            'new_price': 0.9
        }])
        self.assertEqual(len(self.cart.get_items()), 2)
        self.assertEqual(round(self.cart.total, 2), 1.7)

    def test_add_total_with_50_percent_discount_and_product_without(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Soup'), 2)
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1,
            'discount_value': 0.4,
            'new_price': 0.4
        }, {
            'name': 'Soup',
            'price': 0.65,
            'package_type': 'tin',
            'qty': 2
        }])
        self.assertEqual(len(self.cart.get_items()), 2)
        self.assertEqual(round(self.cart.total, 2), 1.7)

    def test_add_all_products_with_discount_and_without_discount(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Soup'), 2)
        self.cart.add_item(self.product.get_product('Apple'), 2)
        self.cart.add_item(self.product.get_product('Milk'), 1)
        self.cart.add_item(self.product.get_product('Milk'), 1)
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1,
            'discount_value': 0.4,
            'new_price': 0.4
        }, {
            'name': 'Soup',
            'price': 0.65,
            'package_type': 'tin',
            'qty': 2
        }, {
            'name': 'Apple',
            'price': 1.0,
            'package_type': 'bag',
            'qty': 2,
            'discount_value': 0.2,
            'new_price': 1.8
        }, {
            'name': 'Milk',
            'price': 1.3,
            'package_type': 'bottle',
            'qty': 2
        }])

        self.assertEqual(len(self.cart.get_items()), 4)
        self.assertEqual(round(self.cart.subtotal, 2), 6.7)
        self.assertEqual(round(self.cart.total, 2), 6.1)

    def test_result_with_discount(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Apple'))
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Apple',
            'price': 1.0,
            'package_type': 'bag',
            'qty': 1,
            'discount_value': 0.1,
            'new_price': 0.9
        }])
        self.assertEqual(round(self.cart.total, 2), 0.9)
        self.assertDictEqual(
            self.cart.result(), {
                'subtotal': '£: 1.0',
                'discount': ['Apple 10 % off: -10p'],
                'total': '£: 0.9'
            })

    def test_result_with_more_than_one_discount(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Apple'))
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.add_item(self.product.get_product('Soup'), 2)
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Apple',
            'price': 1.0,
            'package_type': 'bag',
            'qty': 1,
            'discount_value': 0.1,
            'new_price': 0.9
        }, {
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1,
            'discount_value': 0.4,
            'new_price': 0.4
        }, {
            'name': 'Soup',
            'price': 0.65,
            'package_type': 'tin',
            'qty': 2
        }])
        self.assertEqual(round(self.cart.total, 2), 2.6)
        print(self.cart.result())
        self.assertDictEqual(
            self.cart.result(), {
                'subtotal': '£: 3.1',
                'discount': ['Apple 10 % off: -10p', 'Bread 50 % off: -50p'],
                'total': '£: 2.6'
            })

    def test_result_without_discount(self):
        self.assertListEqual(self.cart.items, list())
        self.assertEqual(self.cart.total, 0.0)
        self.assertEqual(self.cart.subtotal, 0.0)
        self.cart.add_item(self.product.get_product('Bread'))
        self.cart.apply_discount(self.discount)
        self.assertListEqual(self.cart.get_items(), [{
            'name': 'Bread',
            'price': 0.8,
            'package_type': 'loaf',
            'qty': 1
        }])
        self.assertEqual(round(self.cart.total, 2), 0.8)
        self.assertDictEqual(
            self.cart.result(), {
                'subtotal': '£: 0.8',
                'discount': ['no offers available'],
                'total': '£: 0.8'
            })
示例#5
0
class ProductTestCase(unittest.TestCase):
    def setUp(self):
        self.product = Product()

    def tearDown(self):
        self.product = list()

    def test_add_product_complete(self):
        self.assertEqual(self.product.products, dict())
        self.product.add_product(name="Soup",
                                 price=0.65,
                                 package_type=PackageTypes.TIN.value)
        self.product.add_product(name="Bread",
                                 price=0.8,
                                 package_type=PackageTypes.LOAF.value)
        self.assertDictEqual(
            self.product.products, {
                'Soup': {
                    'price': 0.65,
                    'package_type': 'tin'
                },
                'Bread': {
                    'price': 0.8,
                    'package_type': 'loaf'
                }
            })

    def test_get_list_items(self):
        self.assertEqual(self.product.products, dict())
        self.product.add_product(name="Bread",
                                 price=0.8,
                                 package_type=PackageTypes.LOAF.value)
        self.assertDictEqual(self.product.products,
                             {'Bread': {
                                 'price': 0.8,
                                 'package_type': 'loaf'
                             }})

    def test_get_product_existent(self):
        self.product.add_product(name="Soup",
                                 price=0.65,
                                 package_type=PackageTypes.TIN.value)
        self.product.add_product(name="Bread",
                                 price=0.8,
                                 package_type=PackageTypes.LOAF.value)
        self.product.add_product(name="Milk",
                                 price=0.8,
                                 package_type=PackageTypes.BOTTLE.value)
        self.assertDictEqual(self.product.get_product('Milk'), {
            'name': 'Milk',
            'price': 0.8,
            'package_type': 'bottle'
        })

    def test_get_product_not_existent(self):
        self.product.add_product(name="Soup",
                                 price=0.65,
                                 package_type=PackageTypes.TIN.value)
        self.product.add_product(name="Bread",
                                 price=0.8,
                                 package_type=PackageTypes.LOAF.value)
        self.product.add_product(name="Milk",
                                 price=0.8,
                                 package_type=PackageTypes.BOTTLE.value)
        self.assertFalse(self.product.get_product('Banana'))

    def test_remove_product(self):
        self.product.add_product(name="Soup",
                                 price=0.65,
                                 package_type=PackageTypes.TIN.value)
        self.product.add_product(name="Bread",
                                 price=0.8,
                                 package_type=PackageTypes.LOAF.value)
        self.product.add_product(name="Milk",
                                 price=1.8,
                                 package_type=PackageTypes.BOTTLE.value)
        self.product.remove_product('Milk')
        self.assertDictEqual(
            self.product.products, {
                'Soup': {
                    'price': 0.65,
                    'package_type': 'tin'
                },
                'Bread': {
                    'price': 0.8,
                    'package_type': 'loaf'
                }
            })