Skip to content

Commit 64f5546

Browse files
Fix tests to work with improved code - all 82 tests pass
1 parent e23860d commit 64f5546

3 files changed

Lines changed: 43 additions & 29 deletions

File tree

tests/test_app.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Add parent directory to path
1111
sys.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
1414
from 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"""

tests/test_models.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Add parent directory to path to import models
1313
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1414

15-
from models import Book, Cart, CartItem, User, Order, PaymentGateway, EmailService
15+
from models_improved import Book, Cart, CartItem, User, Order, PaymentGateway, EmailService
1616

1717

1818
class TestBook(unittest.TestCase):
@@ -125,18 +125,20 @@ def test_update_quantity(self):
125125
self.assertEqual(self.cart.items[self.book1.title].quantity, 5)
126126

127127
def test_update_quantity_to_zero(self):
128-
"""Test updating quantity to zero (BUG: should remove item)"""
128+
"""Test updating quantity to zero (FIXED: should remove item)"""
129129
self.cart.add_book(self.book1, 2)
130130
self.cart.update_quantity(self.book1.title, 0)
131-
# BUG: Item should be removed but isn't
132-
self.assertIn(self.book1.title, self.cart.items)
133-
self.assertEqual(self.cart.items[self.book1.title].quantity, 0)
131+
# FIXED: Item should be removed
132+
self.assertNotIn(self.book1.title, self.cart.items)
133+
self.assertTrue(self.cart.is_empty())
134134

135135
def test_update_quantity_negative(self):
136-
"""Test updating quantity to negative value (edge case)"""
136+
"""Test updating quantity to negative value (FIXED: should remove item)"""
137137
self.cart.add_book(self.book1, 2)
138138
self.cart.update_quantity(self.book1.title, -1)
139-
self.assertEqual(self.cart.items[self.book1.title].quantity, -1)
139+
# FIXED: Item should be removed when quantity is negative
140+
self.assertNotIn(self.book1.title, self.cart.items)
141+
self.assertTrue(self.cart.is_empty())
140142

141143
def test_get_total_price_empty_cart(self):
142144
"""Test total price of empty cart"""
@@ -367,8 +369,9 @@ def test_paypal_payment(self):
367369

368370
result = PaymentGateway.process_payment(payment_info)
369371

370-
# BUG: PayPal should be validated but currently passes without validation
371-
self.assertTrue(result['success'])
372+
# FIXED: PayPal should be validated and should fail
373+
self.assertFalse(result['success'])
374+
self.assertIn('message', result)
372375

373376
def test_empty_card_number(self):
374377
"""Test payment with empty card number"""
@@ -381,8 +384,9 @@ def test_empty_card_number(self):
381384

382385
result = PaymentGateway.process_payment(payment_info)
383386

384-
# BUG: Should fail but currently succeeds
385-
self.assertTrue(result['success'])
387+
# FIXED: Should fail with empty card number
388+
self.assertFalse(result['success'])
389+
self.assertIn('message', result)
386390

387391

388392
class TestEmailService(unittest.TestCase):

tests/test_performance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
# Add parent directory to path
1515
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1616

17-
from models import Book, Cart, CartItem, User, Order
18-
from app import get_book_by_title, BOOKS
17+
from models_improved import Book, Cart, CartItem, User, Order
18+
from app_improved import get_book_by_title, BOOKS
1919

2020

2121
class TestCartPerformance(unittest.TestCase):

0 commit comments

Comments
 (0)