class TestShoppingCart(unittest.TestCase): def setUp(self): self.bed = Product("Bed", 25.2) self.juice = Product("Orange", 23.3) self.shopping_cart = ShoppingCart() #before test self.shopping_cart.add_product(self.bed) def tearDown(self): pass #after test def test_five_plus_five_iqual_ten(self): assert 5 + 5 == 10 def test_product_name_iqual_bed(self): self.assertEqual(self.bed.name, "Bed") # == def test_product_name_diferent_apple(self): self.assertNotEqual(self.juice.name, "Apple") def test_contains_products(self): self.assertTrue(self.shopping_cart.contains_products()) def test_not_contains_products(self): self.shopping_cart.remove_products(self.bed) self.assertFalse(self.shopping_cart.contains_products()) def test_obteins_product_bed(self): product = self.shopping_cart.get_product(self.bed) self.assertIs(product, self.bed) #is self.assertIsNot(product, self.juice) def test_execption_obtain_juice(self): with self.assertRaises(NotExistsProductError): self.shopping_cart.get_product(self.juice) def test_total_with_one_product(self): total = self.shopping_cart.total() self.assertGreater(total, 0) self.assertLess(total, self.bed.price + 1.0) self.assertEqual(total, self.bed.price) def test_code_bed(self): self.assertRegex(self.bed.code(), self.bed.name) def test_fail(self): if 2 > 3: self.fail("Two is not greater than three!") #@unittest.skip("Motives") #@unittest.skipIf(API_VERSION < 18, "Motives") @unittest.skipUnless(3 > 5, "Motives") def test_skip(self): pass
class Checkout(): def __init__(self, discounts: Discounts) -> None: self.shopping_cart = ShoppingCart(discounts) def add_product(self, product_code: str) -> None: self.shopping_cart.add_product(product_code) def empty(self) -> None: self.shopping_cart.empty() @property def total(self) -> str: return "{:.2f}€".format(self.shopping_cart.total_amount / 100) @property def contents(self) -> str: return "\n".join([ "{} x{}".format(product_name, quantity) for product_name, quantity in self.shopping_cart.contents.items() ])
from product import Product from shopping_cart import ShoppingCart from payment_strategies import BlikStrategy, CreditCardStrategy if __name__ == '__main__': product1 = Product('Banana', 4.99) product2 = Product('Apple', 2.99) cart = ShoppingCart() cart.add_product(product1) cart.add_product(product2) cart.pay(BlikStrategy('*****@*****.**', '123456')) cart.pay( CreditCardStrategy('4111 1111 4555 1142', 'Jon Snow', '456', '09/27'))