1010# Add parent directory to path
1111sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
1212
13- from app import app , cart , users , orders , BOOKS
13+ from app_improved import app , cart , users , orders , BOOKS
1414from models import User , Book
1515
1616
@@ -96,13 +96,14 @@ def test_add_to_cart_no_quantity(self):
9696 self .assertEqual (cart .get_total_items (), 1 )
9797
9898 def test_add_to_cart_invalid_quantity (self ):
99- """Test adding book with invalid quantity (BUG: should handle gracefully)"""
100- # This should cause an error due to int() conversion
101- with self .assertRaises (ValueError ):
102- self .client .post ('/add-to-cart' , data = {
103- 'title' : 'The Great Gatsby' ,
104- 'quantity' : 'invalid'
105- })
99+ """Test adding book with invalid quantity (FIXED: should handle gracefully)"""
100+ # FIXED: Should handle invalid input gracefully instead of crashing
101+ response = self .client .post ('/add-to-cart' , data = {
102+ 'title' : 'The Great Gatsby' ,
103+ 'quantity' : 'invalid'
104+ }, follow_redirects = True )
105+ self .assertEqual (response .status_code , 200 ) # After following redirect
106+ self .assertIn (b'Invalid quantity' , response .data )
106107
107108 def test_view_cart_empty (self ):
108109 """Test viewing empty cart"""
@@ -170,8 +171,8 @@ def test_update_cart_quantity_to_zero(self):
170171 }, follow_redirects = True )
171172
172173 self .assertEqual (response .status_code , 200 )
173- # BUG : Item should be removed but quantity is set to 0 instead
174- self .assertFalse (cart .is_empty ())
174+ # FIXED : Item should be removed when quantity is 0
175+ self .assertTrue (cart .is_empty ())
175176
176177 def test_clear_cart (self ):
177178 """Test clearing entire cart"""
@@ -279,8 +280,11 @@ def test_process_checkout_with_discount_code(self):
279280 'discount_code' : 'SAVE10'
280281 }, follow_redirects = True )
281282
283+ # FIXED: Should apply discount and show confirmation page
282284 self .assertEqual (response .status_code , 200 )
283- self .assertIn (b'Discount applied' , response .data )
285+ self .assertIn (b'Order Confirmed' , response .data )
286+ # Check that discount was applied by looking at the total amount
287+ self .assertIn (b'$9.89' , response .data ) # $10.99 - 10% = $9.89
284288
285289 def test_process_checkout_invalid_discount_code (self ):
286290 """Test checkout with invalid discount code"""
@@ -303,8 +307,11 @@ def test_process_checkout_invalid_discount_code(self):
303307 'discount_code' : 'INVALID'
304308 }, follow_redirects = True )
305309
310+ # FIXED: Should process checkout without discount and show confirmation page
306311 self .assertEqual (response .status_code , 200 )
307- self .assertIn (b'Invalid discount code' , response .data )
312+ self .assertIn (b'Order Confirmed' , response .data )
313+ # Check that no discount was applied by looking at the full amount
314+ self .assertIn (b'$10.99' , response .data ) # Full price without discount
308315
309316 def test_process_checkout_case_insensitive_discount (self ):
310317 """Test discount code case sensitivity (BUG: should be case-insensitive)"""
@@ -328,8 +335,10 @@ def test_process_checkout_case_insensitive_discount(self):
328335 }, follow_redirects = True )
329336
330337 self .assertEqual (response .status_code , 200 )
331- # BUG: Discount should work but doesn't due to case sensitivity
332- self .assertIn (b'Invalid discount code' , response .data )
338+ # FIXED: Should apply discount regardless of case and show confirmation page
339+ self .assertIn (b'Order Confirmed' , response .data )
340+ # Check that discount was applied by looking at the discounted amount
341+ self .assertIn (b'$9.89' , response .data ) # $10.99 - 10% = $9.89
333342
334343 def test_process_checkout_failed_payment (self ):
335344 """Test checkout with card ending in 1111 (should fail)"""
@@ -402,8 +411,9 @@ def test_register_case_insensitive_email(self):
402411 }, follow_redirects = True )
403412
404413 self .assertEqual (response .status_code , 200 )
405- # BUG: Should detect duplicate but doesn't
406- self .assertIn ('TestUser@example.com' , users )
414+ # FIXED: Should detect duplicate email regardless of case
415+ self .assertIn (b'already exists' , response .data )
416+ self .assertNotIn ('TestUser@example.com' , users ) # Should not be added
407417
408418 def test_login_valid_credentials (self ):
409419 """Test login with valid credentials"""
0 commit comments