3
3
4
4
class TestOnlineStore (unittest .TestCase ):
5
5
6
- # Tricks the system and walks away with 1 television, despite valid payment & reimbursement
7
- def test_4 (self ):
6
+ # Tricks the system and walks away with 1 television, despite valid payment & reimbursement.
7
+ def test_6 (self ):
8
8
tv_item = c .Item (type = 'product' , description = 'tv' , amount = 1000.00 , quantity = 1 )
9
9
payment = c .Item (type = 'payment' , description = 'invoice_4' , amount = 1e19 , quantity = 1 )
10
10
payback = c .Item (type = 'payment' , description = 'payback_4' , amount = - 1e19 , quantity = 1 )
11
11
order_4 = c .Order (id = '4' , items = [payment , tv_item , payback ])
12
12
self .assertEqual (c .validorder (order_4 ), 'Order ID: 4 - Payment imbalance: $-1000.00' )
13
13
14
- # Valid payments that should add up correctly, but do not
15
- def test_5 (self ):
14
+ # Valid payments that should add up correctly, but do not.
15
+ def test_7 (self ):
16
16
small_item = c .Item (type = 'product' , description = 'accessory' , amount = 3.3 , quantity = 1 )
17
17
payment_1 = c .Item (type = 'payment' , description = 'invoice_5_1' , amount = 1.1 , quantity = 1 )
18
18
payment_2 = c .Item (type = 'payment' , description = 'invoice_5_2' , amount = 2.2 , quantity = 1 )
19
19
order_5 = c .Order (id = '5' , items = [small_item , payment_1 , payment_2 ])
20
20
self .assertEqual (c .validorder (order_5 ), 'Order ID: 5 - Full payment received!' )
21
21
22
+ # The total amount of an order must be limited. Order validation shouldn't depend on ordering of items.
23
+ def test_8 (self ):
24
+ num_items = 12
25
+ items = [c .Item (type = 'product' , description = 'tv' , amount = 99999 , quantity = num_items )]
26
+ for i in range (num_items ):
27
+ items .append (c .Item (type = 'payment' , description = 'invoice_' + str (i ), amount = 99999 , quantity = 1 ))
28
+ order_1 = c .Order (id = '1' , items = items )
29
+ self .assertEqual (c .validorder (order_1 ), 'Total amount exceeded' )
30
+
31
+ # Put payments before products
32
+ items = items [1 :] + [items [0 ]]
33
+ order_2 = c .Order (id = '2' , items = items )
34
+ self .assertEqual (c .validorder (order_2 ), 'Total amount exceeded' )
35
+
22
36
if __name__ == '__main__' :
23
37
unittest .main ()
0 commit comments