示例#1
0
    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")
示例#2
0
    def _map_item(self, item):

        product = Product(
            item['name'],
            price=item['price']
        )
        product.id = item['product_id']

        sale = Sale(
            product,
            quantity=item['quantity']
        )
        sale.id = item['id']
        return sale
def test_create_product():
    name = 'Сплит-система Comfee MSAFA-07HRN1-QC2'
    data = {'price': 12_990, 'quantity': 1}

    product = Product(name, **data)

    assert product.name == name
    for key, val in data.items():
        assert getattr(product, key) == val
示例#4
0
 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()
示例#5
0
def test_create_sale():
    product = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                      price=12_990,
                      quantity=100)
    quantity = 10

    sale = Sale(product, quantity)

    assert sale.product_id == product.id
    assert sale.name == product.name
    assert sale.price == product.price
    assert sale.quantity == quantity
def test_product_manager_remove_by_id():
    db = helpers.get_db()

    manager = ProductManager(db)
    product = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                      price=12_990,
                      quantity=1)

    manager.add(product)
    manager.remove_by_id(product.id)

    assert len(manager.get_all()) == 0
def test_db_update_with_object():
    db = helpers.get_db()

    manager = ProductManager(db)
    product = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                      price=12_990,
                      quantity=1)

    manager.add(product)

    product.name = 'test name'
    product.price = 10
    product.quantity = 100

    db.update(manager.table, product)

    updated_product = manager.search_by_id(product.id)

    assert updated_product.name == product.name
    assert updated_product.price == product.price
    assert updated_product.quantity == product.quantity
def test_db_delete():
    db = helpers.get_db()

    manager = ProductManager(db)
    product = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                      price=12_990,
                      quantity=1)

    manager.add(product)
    before_deletion = manager.get_all()
    db.delete(manager.table, product.id)
    after_deletion = manager.get_all()

    assert len(before_deletion) == 1
    assert len(after_deletion) == 0
示例#9
0
def test_sale_manager_add():
    product = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                      price=12_990,
                      quantity=100)
    quantity = 10

    sale = Sale(product, quantity)

    db = helpers.get_db()
    sale_manager = SaleManager(db)
    sale_manager.add(sale)

    results = helpers.extract_ids(sale_manager.get_all())

    assert len(results) == 1
    assert sale.id in results
def test_db_find_by_column():
    db = helpers.get_db()

    product_manager = ProductManager(db)

    comfee_msafa_07 = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                              price=12_990,
                              quantity=1)
    product_manager.add(comfee_msafa_07)

    haier_hsu_09 = Product('Сплит-система Haier HSU-09HTM03/R2',
                           price=22_490,
                           quantity=24)
    product_manager.add(haier_hsu_09)

    lg_p_07 = Product('Сплит-система (инвертор) LG P07EP2',
                      price=33_990,
                      quantity=1)
    product_manager.add(lg_p_07)

    haier_as_09 = Product('Сплит-система (инвертор) Haier AS09NA6HRA-S',
                          price=27_990,
                          quantity=24)
    product_manager.add(haier_as_09)

    comfee_msafa_09 = Product(
        'Сплит-система (инвертор) Comfee MSAFA-09HRDN1-QC2F',
        price=22_990,
        quantity=2)
    product_manager.add(comfee_msafa_09)

    lg_pm_09 = Product('Сплит-система (инвертор) LG PM09SP',
                       price=34_990,
                       quantity=6)
    product_manager.add(lg_pm_09)

    comfee_msafb_12 = Product('Сплит-система Comfee MSAFB-12HRN1-QC2',
                              price=20_990,
                              quantity=1)
    product_manager.add(comfee_msafb_12)

    expected = lg_p_07

    results = helpers.extract_ids(
        db.find_by_column(product_manager.table,
                          column='name',
                          value='Сплит-система (инвертор) LG P07EP2'))

    assert expected.id in results
示例#11
0
def test_product_manager_add():

    db = helpers.get_db()

    manager = ProductManager(db)
    product = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                      price=12_990,
                      quantity=1)

    manager.add(product)

    items = manager.get_all()
    assert len(items) == 1
    assert product.id == items[0].id
    assert product.name == items[0].name
    assert product.price == items[0].price
    assert product.quantity == items[0].quantity
示例#12
0
def test_product_manager_search_by_name():
    db = helpers.get_db()

    product_manager = ProductManager(db)

    comfee_msafa_07 = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                              price=12_990,
                              quantity=1)
    product_manager.add(comfee_msafa_07)

    haier_hsu_09 = Product('Сплит-система Haier HSU-09HTM03/R2',
                           price=22_490,
                           quantity=24)
    product_manager.add(haier_hsu_09)

    lg_p_07 = Product('Сплит-система (инвертор) LG P07EP2',
                      price=33_990,
                      quantity=1)
    product_manager.add(lg_p_07)

    haier_as_09 = Product('Сплит-система (инвертор) Haier AS09NA6HRA-S',
                          price=27_990,
                          quantity=24)
    product_manager.add(haier_as_09)

    comfee_msafa_09 = Product(
        'Сплит-система (инвертор) Comfee MSAFA-09HRDN1-QC2F',
        price=22_990,
        quantity=2)
    product_manager.add(comfee_msafa_09)

    lg_pm_09 = Product('Сплит-система (инвертор) LG PM09SP',
                       price=34_990,
                       quantity=6)
    product_manager.add(lg_pm_09)

    comfee_msafb_12 = Product('Сплит-система Comfee MSAFB-12HRN1-QC2',
                              price=20_990,
                              quantity=1)
    product_manager.add(comfee_msafb_12)

    expected = [lg_p_07.id, lg_pm_09.id]
    expected.sort()

    results = helpers.extract_ids(product_manager.search_by_name('lg'))
    results.sort()

    assert expected == results
示例#13
0
def test_product_manager_update():
    db = helpers.get_db()

    manager = ProductManager(db)
    product = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                      price=12_990,
                      quantity=1)

    data = {'name': 'test name', 'price': 10, 'quantity': 100}

    manager.add(product)
    manager.update(product.id, **data)

    updated_product = manager.search_by_id(product.id)

    assert updated_product.name == data['name']
    assert updated_product.price == data['price']
    assert updated_product.quantity == data['quantity']
示例#14
0
    def products_import():
        nonlocal product_manager
        if 'import-file' not in request.files:
            redirect(url_for('index'))

        importFile = request.files['import-file']
        content = io.StringIO(importFile.read().decode("utf8"))
        reader = csv.reader(content, delimiter=';')
        for line in reader:
            if line[0] != '':
                product_manager.update(line[0],
                                       name=line[1],
                                       price=line[2],
                                       quantity=line[3])
            else:
                product_manager.add(
                    Product(line[1], price=line[2], quantity=line[3]))
        return redirect(url_for('index'))
示例#15
0
def test_product_manager_search_by_id():
    db = helpers.get_db()

    product_manager = ProductManager(db)

    comfee_msafa_07 = Product('Сплит-система Comfee MSAFA-07HRN1-QC2',
                              price=12_990,
                              quantity=1)
    product_manager.add(comfee_msafa_07)

    haier_hsu_09 = Product('Сплит-система Haier HSU-09HTM03/R2',
                           price=22_490,
                           quantity=24)
    product_manager.add(haier_hsu_09)

    lg_p_07 = Product('Сплит-система (инвертор) LG P07EP2',
                      price=33_990,
                      quantity=1)
    product_manager.add(lg_p_07)

    haier_as_09 = Product('Сплит-система (инвертор) Haier AS09NA6HRA-S',
                          price=27_990,
                          quantity=24)
    product_manager.add(haier_as_09)

    comfee_msafa_09 = Product(
        'Сплит-система (инвертор) Comfee MSAFA-09HRDN1-QC2F',
        price=22_990,
        quantity=2)
    product_manager.add(comfee_msafa_09)

    lg_pm_09 = Product('Сплит-система (инвертор) LG PM09SP',
                       price=34_990,
                       quantity=6)
    product_manager.add(lg_pm_09)

    comfee_msafb_12 = Product('Сплит-система Comfee MSAFB-12HRN1-QC2',
                              price=20_990,
                              quantity=1)
    product_manager.add(comfee_msafb_12)

    result = product_manager.search_by_id(lg_pm_09.id)

    assert result.id == lg_pm_09.id
示例#16
0
    def product_save(product_id):
        nonlocal product_saved

        name = request.form['name']
        price = request.form['price']
        quantity = request.form['quantity']

        empty_id = str(uuid.UUID(int=0))
        if product_id == empty_id:
            product = Product(name, price=price, quantity=quantity)
            product_manager.add(product)
            product_id = product.id
        else:
            product_manager.update(product_id,
                                   name=name,
                                   price=price,
                                   quantity=quantity)

        product_saved = True
        return redirect(url_for('product_edit', product_id=product_id))
示例#17
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'
                }
            })
示例#18
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'
            })
示例#19
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
示例#20
0
# coding=utf-8

from utils import PackageTypes
from app.product import Product
from app.discount import Discount
from app.cart import Cart

if __name__ == '__main__':
    items = Product()
    items.add_product(name="Soup",
                      price=0.65,
                      package_type=PackageTypes.TIN.value)
    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")
示例#21
0
 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)
示例#22
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'}])
示例#23
0
 def setUp(self):
     self.product = Product()
示例#24
0
def test_product_manager_sorting():
    db = helpers.get_db()

    product_manager = ProductManager(db)

    product_a = Product('АА', price=10, quantity=1)

    product_b = Product('БА', price=1, quantity=100)

    product_c = Product('ВА', price=100, quantity=10)

    product_manager.add(product_b)
    product_manager.add(product_a)
    product_manager.add(product_c)

    sorting_name_asc = {'column': 'name', 'order': 'ASC'}
    sorting_name_desc = {'column': 'name', 'order': 'DESC'}
    sorting_price_asc = {'column': 'price', 'order': 'ASC'}
    sorting_price_desc = {'column': 'price', 'order': 'DESC'}
    sorting_quantity_asc = {'column': 'quantity', 'order': 'ASC'}
    sorting_quantity_desc = {'column': 'quantity', 'order': 'DESC'}

    expected_name_asc = [product_a.id, product_b.id, product_c.id]
    expected_name_desc = [product_c.id, product_b.id, product_a.id]
    expected_price_asc = [product_b.id, product_a.id, product_c.id]
    expected_price_desc = [product_c.id, product_a.id, product_b.id]
    expected_quantity_asc = [product_a.id, product_c.id, product_b.id]
    expected_quantity_desc = [product_b.id, product_c.id, product_a.id]

    items = helpers.extract_ids(product_manager.get_all(sorting_name_asc))
    assert expected_name_asc == items
    items = helpers.extract_ids(
        product_manager.search_by_name('А', sorting_name_asc))
    assert expected_name_asc == items

    items = helpers.extract_ids(product_manager.get_all(sorting_name_desc))
    assert expected_name_desc == items
    items = helpers.extract_ids(
        product_manager.search_by_name('А', sorting_name_desc))
    assert expected_name_desc == items

    items = helpers.extract_ids(product_manager.get_all(sorting_price_asc))
    assert expected_price_asc == items
    items = helpers.extract_ids(
        product_manager.search_by_name('А', sorting_price_asc))
    assert expected_price_asc == items

    items = helpers.extract_ids(product_manager.get_all(sorting_price_desc))
    assert expected_price_desc == items
    items = helpers.extract_ids(
        product_manager.search_by_name('А', sorting_price_desc))
    assert expected_price_desc == items

    items = helpers.extract_ids(product_manager.get_all(sorting_quantity_asc))
    assert expected_quantity_asc == items
    items = helpers.extract_ids(
        product_manager.search_by_name('А', sorting_quantity_asc))
    assert expected_quantity_asc == items

    items = helpers.extract_ids(product_manager.get_all(sorting_quantity_desc))
    assert expected_quantity_desc == items
    items = helpers.extract_ids(
        product_manager.search_by_name('А', sorting_quantity_desc))
    assert expected_quantity_desc == items