def test_cart(): c = Cart() p = Product("widget", 123) c.add_product(p, 100) item2 = Product("gadget", 45) c.add_product(item2, 1) assert c.get_total() == 12345 # 100x123=12300 + 1x45=45 assert len(c.line_items) == 2 assert c.line_items[0].get_extended_price() == 12300 assert c.line_items[1].get_extended_price() == 45 content = str(c) assert content.find("widget") != -1 assert content.find("gadget") != -1 assert content.find("123") != -1 assert content.find("12300") != -1 assert content.find("1") != -1 assert content.find("45") != -1 assert content.find("total") != -1 assert content.find("12345") != -1 display_cart(c) c.remove(2) c.update_quantity(1, 200) # line number and quantity content = str(c) assert content.find("24600") != -1 # 123x200=24600 display_cart(c)
def write(cls, fl_cart): """Data downloading""" with open(fl_cart, 'r') as file_in: cart = file_in.readlines() new_cart = Cart() for prod in cart[1:]: prod = Product(prod.strip().split(';')) new_cart.add_product(prod) return new_cart
class Customer: __orders = [] __bank = None __amount = 0 __cart = None def __init__(self, name): self.__name = name self.__cart = Cart(name) def get_name(self): return self.__name def add_product(self, product): self.__cart.add_product(product) def get_orders(self): return self.__orders def add_order(self, order): self.__orders.append(order) def remove_order(self, order): self.__orders.remove(order) def get_bank(self): self.__bank def set_bank(self, bank): self.__bank = bank def get_amount(self): return self.__amount def set_amount(self, amount): self.__amount = amount def add_cart(self, cart): self.__cart = cart def get_cart(self): return self.__cart
class TestCart(unittest.TestCase): def setUp(self): self.session = {} self.product_one = NonCallableMock(product_id=1, price=1.99) self.product_two = NonCallableMock(product_id=2, price=2.49) self.cart = Cart(self.session) def test_can_add_product(self): self.cart.add_product(self.product_one, 10) self.assertEqual( self.cart.get_cart_item(self.product_one).quantity, 10) def test_cannot_add_product_twice(self): self.cart.add_product(self.product_one, 13) self.assertRaises( CartError, lambda: self.cart.add_product(self.product_one, 10)) def test_remove_product(self): self.cart.add_product(self.product_one, 10) self.cart.remove_product(self.product_one) self.assertEqual(self.cart.num_items, 0) self.assertRaises( CartError, lambda: self.cart.get_cart_item(self.product_one)) def test_cannot_remove_product_not_in_cart(self): self.assertRaises( CartError, lambda: self.cart.remove_product(self.product_one)) def test_can_calculate_quantity_of_cart(self): self.cart.add_product(self.product_one, 10) self.assertEqual(self.cart.num_items, 10) self.cart.add_product(self.product_two, 15) self.assertEqual(self.cart.num_items, 25) def test_quantity_of_empty_cart_is_zero(self): self.assertEqual(self.cart.num_items, 0) def test_can_calculate_subtotal_of_cart(self): self.cart.add_product(self.product_one, 10) self.assertEqual(self.cart.sub_total, 19.9) self.cart.add_product(self.product_two, 15) self.assertEqual(self.cart.sub_total, 57.25) def test_subtotal_of_empty_cart_is_zero(self): self.assertEqual(self.cart.sub_total, 0) def test_can_update_quanity(self): self.cart.add_product(self.product_one, 10) self.cart.update_quantity(self.product_one, 15) self.assertEqual( self.cart.get_cart_item(self.product_one).quantity, 15) def test_cannot_update_quantity_if_not_in_cart(self): self.assertRaises(CartError, lambda: self.cart.update_quantity(self.product_one, 10)) def test_can_save_cart(self): self.cart.add_product(self.product_one) self.cart.save(self.session) self.new_cart = Cart(self.session) self.assertEqual(self.cart.cart_items, self.new_cart.cart_items) def test_can_reset_cart(self): self.cart.add_product(self.product_one, 10) self.cart.reset() self.assertEqual(self.cart.cart_items, [])
elif choice == '3': prod_info = [] print('Штрих-код продукта:', end=' ') s = input() prod_info.append(s) print('Наименование:', end=' ') s = input() prod_info.append(s) print('Количество:', end=' ') s = input() prod_info.append(s) print('Цена:', end=' ') s = input() prod_info.append(s) cart.add_product(Product(prod_info)) print('Товар добавлен в корзину.') break elif choice == '4': print('Штрих-код продукта:', end=' ') s = input() if cart.del_product(s): print('Товар не найден.') else: print('Товар удален.') break print('Выбор некорректен.') print('-' * 100) print(POST_MENU)
class CartTestCase(unittest.TestCase): def setUp(self): self.session = {} self.product_one = NonCallableMock(product_id=1, price=1.99) self.product_two = NonCallableMock(product_id=2, price=2.49) self.cart = Cart(self.session) def test_can_add_product(self): self.cart.add_product(self.product_one, 10) self.assertEqual( self.cart.get_cart_item(self.product_one).quantity, 10) def test_cannot_add_product_twice(self): self.cart.add_product(self.product_one, 13) self.assertRaises(CartError, lambda: self.cart.add_product(self.product_one, 10)) def test_remove_product(self): self.cart.add_product(self.product_one, 10) self.cart.remove_product(self.product_one) self.assertEqual(self.cart.num_items, 0) self.assertRaises(CartError, lambda: self.cart.get_cart_item(self.product_one)) def test_cannot_remove_product_not_in_cart(self): self.assertRaises(CartError, lambda: self.cart.remove_product(self.product_one)) def test_can_calculate_quantity_of_cart(self): self.cart.add_product(self.product_one, 10) self.assertEqual(self.cart.num_items, 10) self.cart.add_product(self.product_two, 15) self.assertEqual(self.cart.num_items, 25) def test_quantity_of_empty_cart_is_zero(self): self.assertEqual(self.cart.num_items, 0) def test_can_calculate_subtotal_of_cart(self): self.cart.add_product(self.product_one, 10) self.assertEqual(self.cart.sub_total, 19.9) self.cart.add_product(self.product_two, 15) self.assertEqual(self.cart.sub_total, 57.25) def test_subtotal_of_empty_cart_is_zero(self): self.assertEqual(self.cart.sub_total, 0) def test_can_update_quanity(self): self.cart.add_product(self.product_one, 10) self.cart.update_quantity(self.product_one, 15) self.assertEqual( self.cart.get_cart_item(self.product_one).quantity, 15) def test_cannot_update_quantity_if_not_in_cart(self): self.assertRaises( CartError, lambda: self.cart.update_quantity(self.product_one, 10)) def test_can_save_cart(self): self.cart.add_product(self.product_one) self.cart.save(self.session) self.new_cart = Cart(self.session) self.assertEqual(self.cart.cart_items, self.new_cart.cart_items) def test_can_reset_cart(self): self.cart.add_product(self.product_one, 10) self.cart.reset() self.assertEqual(self.cart.cart_items, [])