1+ import unittest
2+ from parameterized import parameterized
3+ from Lib .Purchase import Purchase
4+ from Lib .Product import Product
5+
6+
7+ class TestProductPurchase (unittest .TestCase ):
8+ # Method for setting test data
9+ def setUp (self ):
10+ # Create products for each category
11+ self .laptops = [Product ("Laptop1" , 1000 , "laptops" ),
12+ Product ("Laptop2" , 1500 , "laptops" ),
13+ Product ("Laptop3" , 1200 , "laptops" )]
14+
15+ self .phones = [Product ("Phone1" , 800 , "phones" ),
16+ Product ("Phone2" , 900 , "phones" ),
17+ Product ("Phone3" , 1000 , "phones" )]
18+
19+ # Method for testing checkout
20+ def test_place_order (self ):
21+ # Create a product object
22+ product = Product ("Laptop1" , 1000 , "laptops" )
23+ # Call the method under test
24+ total_price = Purchase .place_order (product , 2 , 'cash' )
25+ # Check result
26+ self .assertEqual (total_price , 2200 ) # Changed expected result to 2200
27+ # Checking that the product discount has not been applied
28+ self .assertFalse (product .discount_applied )
29+
30+ # Method for testing the calculation of the amount of goods, taking into account the quantity
31+ def test_calculate_total_price (self ):
32+ # Create a product object
33+ product = Product ("Laptop1" , 1000 , "laptops" )
34+ # Call the method under test
35+ total_price = product .calculate_total_price (2 )
36+ # Check result
37+ self .assertEqual (total_price , 2000 )
38+
39+ # Method for testing the calculation of the amount of goods, taking into account a 10% discount
40+ def test_calculate_total_price_with_discount (self ):
41+ # Checking the calculation of the amount, taking into account the discount for each category of goods
42+ for product in self .laptops :
43+ total_price = product .calculate_total_price_with_discount (2 )
44+ self .assertEqual (total_price , 1800 )
45+
46+ for product in self .phones :
47+ total_price = product .calculate_total_price_with_discount (3 )
48+ self .assertEqual (total_price , 1680 )
49+
50+ # Method for testing the application of a promo code discount
51+ def test_apply_discount_by_promocode (self ):
52+ # Create a product object
53+ product = Product ("Laptop1" , 1000 , "laptops" )
54+ # Call the method under test
55+ total_price = Purchase .apply_discount_by_promocode (product , 2 , 'DISCOUNT10' )
56+ # Check result
57+ self .assertEqual (total_price , 1800 )
58+
59+ # Check that the promo code discount cannot be applied again if there is already a discount on the product
60+ product_with_discount = Product ("Laptop2" , 1500 , "laptops" )
61+ Purchase .apply_discount_by_promocode (product_with_discount , 1 , 'DISCOUNT10' )
62+ with self .assertRaises (ValueError ):
63+ Purchase .apply_discount_by_promocode (product_with_discount , 1 , 'DISCOUNT10' )
64+
65+ # Method for testing the calculation of the amount of goods, taking into account a 10% discount
66+ def test_calculate_total_price_with_discount (self ):
67+ # Create a product object
68+ product = Product ("Laptop1" , 1000 , "laptops" )
69+ # Call the method under test, passing the quantity argument
70+ total_price = product .calculate_total_price_with_discount (2 )
71+ # Check result
72+ self .assertEqual (total_price , 1800 )
73+
74+ # Method to test place_order method with wrong payment method
75+ def test_place_order_with_wrong_payment_method (self ):
76+ # Create a product object
77+ product = Product ("Laptop1" , 1000 , "laptops" )
78+ # Check for an error with an incorrect payment method
79+ with self .assertRaises (ValueError ):
80+ Purchase .place_order (product , 2 , 'paypal' )
81+
82+ # Method for testing the calculation of the amount of the goods, taking into account the tax of 20%
83+ def test_calculate_total_price_with_vat (self ):
84+ # Checking the calculation of the amount including tax for each category of goods
85+ for product in self .laptops + self .phones :
86+ # Call the method under test
87+ total_price_with_vat = product .calculate_total_price_with_vat (2 )
88+ # Expected Result
89+ expected_total_price_with_vat = product .price * 2 * 1.2
90+ # Check result
91+ self .assertEqual (total_price_with_vat , expected_total_price_with_vat )
92+
93+ # Testing the place_order method using parameterization
94+ @parameterized .expand ([
95+ # Test data: product, quantity, payment_method, expected_total_price
96+ (Product ("Phone1" , 1000 , "phones" ), 2 , "cash" , 2200 ),
97+ (Product ("Laptop1" , 2000 , "laptops" ), 3 , "credit_card" , 6000 ),
98+ (Product ("Phone2" , 1000 , "phones" ), 1 , "cash" , 1100 )
99+ ])
100+ def test_place_order_with_parameterization (self , product , quantity , payment_method , expected_total_price ):
101+ # Call the method under test
102+ total_price = Purchase .place_order (product , quantity , payment_method )
103+
104+ # Round expected and actual amount to 2 decimal places
105+ expected_total_price = round (expected_total_price , 2 )
106+ total_price = round (total_price , 2 )
107+
108+ # Check for equality of expected and actual amount
109+ self .assertEqual (total_price , expected_total_price )
110+
111+ # Testing the apply_discount_by_promocode method using parameterization
112+ @parameterized .expand ([
113+ # Test data: product, quantity, promocode, expected_total_price
114+ (Product ("Phone1" , 1000 , "phones" ), 2 , "DISCOUNT10" , 1800 ),
115+ (Product ("Laptop1" , 2000 , "laptops" ), 3 , "DISCOUNT10" , 5400 ),
116+ (Product ("Phone2" , 1500 , "phones" ), 1 , "DISCOUNT10" , 1350 )
117+ ])
118+ def test_apply_discount_by_promocode_with_parameterization (self , product , quantity , promocode , expected_total_price ):
119+ # Call the method under test
120+ total_price = Purchase .apply_discount_by_promocode (product , quantity , promocode )
121+ # Checking if the expected value matches
122+ self .assertEqual (total_price , expected_total_price )
123+
124+ # Create a test suite
125+ # def suite():
126+ # suite = unittest.TestSuite()
127+ # suite.addTest(TestProductPurchase("test_place_order"))
128+ # suite.addTest(TestProductPurchase("test_apply_discount_by_promocode"))
129+ # suite.addTest(TestProductPurchase("test_calculate_total_price_with_discount"))
130+ # suite.addTest(TestProductPurchase("test_calculate_total_price_with_vat"))
131+ # suite.addTest(TestProductPurchase("test_calculate_total_price_with_quantity"))
132+ # suite.addTest(TestProductPurchase("test_place_order_with_wrong_payment_method"))
133+ # return suite
134+
135+
136+ if __name__ == '__main__' :
137+ unittest .main ()
138+ # runner = unittest.TextTestRunner()
139+ # runner.run(suite())
0 commit comments