def test_item_constructor_should_raise_exception(product): obj = object() """This test will raise exception if product param is not an instance of product""" with pytest.raises(ValueError): Item(obj, 9) """This test will raise exception if quantity is not an instance of integer""" with pytest.raises(ValueError): Item(product, 9.0) """This test will raise exception if quantity is less than or equal to zero""" with pytest.raises(Exception): Item(product, 0)
def test_cart_total_cost_should_be_roundup_by_2_decimal(cart): product = Product('Test 1', 39.99) item = Item(product, 5) cart.add_item_in_cart(item) expected_no_of_items = 1 expected_total_cost_cart = 199.95 assert len(cart.get_all_items_in_cart()) == expected_no_of_items assert cart.get_subtotal_of_cart() == expected_total_cost_cart
def main(): try: print("Started") """Let create Dove product""" product = Product('Dove Soap', 39.99) """create second product""" product2 = Product('Axe Deo', 99.99) """Create empty Cart for the user""" cart = Cart() """Add items to the cart""" item = Item(product, 2) cart.add_item_in_cart(item) print("Added first item to the cart") """ Add second item to the cart""" item2 = Item(product2, 2) cart.add_item_in_cart(item2) print("Added first item to the cart") """Print the Bill""" print("===============================================") print("Item_no Product_name Quantity") for count, item in enumerate(cart.get_all_items_in_cart(), 1): print("{0:8d} {1:12} {2}".format(count, item.get_product().get_name(), item.get_quantity())) print("================================================") print("Subtotal of bill {0}".format(cart.get_subtotal_of_cart())) print("Sales tax on the bill {0}".format(cart.get_total_tax_of_cart())) print("Total bill {0}".format(cart.get_total_cart_cost())) except ValueError as err: print("Value Error : {0}".format(err)) except TypeError as err: print("Type Error : {0}".format(err)) except Exception as err: print("Exception : {0}".format(err)) finally: print("Finished")
def test_cart_decrease_quantity_item_to_zero(cart): """Create product""" product = Product('Dove Soap', 39.99) """create an item """ item = Item(product, 5) cart.add_item_in_cart(item) """Initial checks""" assert len(cart.get_all_items_in_cart()) == 1 cart_item = cart.get_all_items_in_cart()[0] assert cart_item.get_quantity() == 5 assert cart.get_subtotal_of_cart() == 199.95 assert cart.get_total_tax_of_cart() == 25.0 assert cart.get_total_cart_cost() == 224.95 """update the item quantity to zero""" quantity = cart.get_all_items_in_cart()[0].get_quantity() cart.decrease_quantity_of_item(item, quantity) assert len(cart.get_all_items_in_cart()) == 0 assert cart.get_subtotal_of_cart() == 0 assert cart.get_total_tax_of_cart() == 0 assert cart.get_total_cart_cost() == 0
def test_cart_increase_item_quantity(cart): """Create product""" product = Product('Dove Soap', 39.99) """create an item """ item = Item(product, 5) cart.add_item_in_cart(item) """Initial checks""" assert len(cart.get_all_items_in_cart()) == 1 cart_item = cart.get_all_items_in_cart()[0] assert cart_item.get_quantity() == 5 assert cart.get_subtotal_of_cart() == 199.95 assert cart.get_total_tax_of_cart() == 25.0 assert cart.get_total_cart_cost() == 224.95 """increase the item quantity by 5 """ cart.increase_quantity_of_item(item, 3) assert len(cart.get_all_items_in_cart()) == 1 cart_item = cart.get_all_items_in_cart()[0] assert cart_item.get_quantity() == 8 assert cart.get_subtotal_of_cart() == 319.92 assert cart.get_total_tax_of_cart() == 39.99 assert cart.get_total_cart_cost() == 359.91
def item(product): return Item(product, 3)
def test_item_update_quantity_of_item(product): item = Item(product, 4) assert item.get_quantity() == 4 assert item.get_sub_total() == 156 """Increase the quantity of the item""" new_quantity = 9 expected_sub_total = product.get_price() * 9 item.update_quantity_of_item(new_quantity) assert item.get_quantity() == new_quantity assert item.get_sub_total() == expected_sub_total """Decrease the quantity of the item""" new_quantity = 6 expected_sub_total = product.get_price() * new_quantity item.update_quantity_of_item(new_quantity) assert item.get_quantity() == new_quantity assert item.get_sub_total() == expected_sub_total
def test_item_sub_total_should_round_value_by_2_decimal(): product = Product('Test 1', 19.499) item = Item(product, 9) assert item.get_sub_total() == 175.5