From 2f77f5bb23ee7a98cb03cd9395aa728598ebc220 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Wed, 6 Apr 2016 17:24:25 +1000 Subject: [PATCH] Replaces .active = False; .save() pattern in tests with a test controller method --- registrasion/tests/cart_controller_helper.py | 4 +++ registrasion/tests/test_cart.py | 28 +++++++++---------- registrasion/tests/test_ceilings.py | 18 ++++++------ registrasion/tests/test_discount.py | 28 +++++++++---------- registrasion/tests/test_enabling_condition.py | 20 ++++++------- registrasion/tests/test_voucher.py | 8 +++--- 6 files changed, 55 insertions(+), 51 deletions(-) diff --git a/registrasion/tests/cart_controller_helper.py b/registrasion/tests/cart_controller_helper.py index 05782297..9e4191f0 100644 --- a/registrasion/tests/cart_controller_helper.py +++ b/registrasion/tests/cart_controller_helper.py @@ -24,3 +24,7 @@ class TestingCartController(CartController): except ObjectDoesNotExist: old_quantity = 0 self.set_quantity(product, old_quantity + quantity) + + def next_cart(self): + self.cart.active = False + self.cart.save() diff --git a/registrasion/tests/test_cart.py b/registrasion/tests/test_cart.py index 1de72bc9..df5e290f 100644 --- a/registrasion/tests/test_cart.py +++ b/registrasion/tests/test_cart.py @@ -74,12 +74,12 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase): # Burn through some carts -- this made some past EC tests fail current_cart = TestingCartController.for_user(cls.USER_1) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() current_cart = TestingCartController.for_user(cls.USER_2) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() @classmethod def make_ceiling(cls, name, limit=None, start_time=None, end_time=None): @@ -142,8 +142,8 @@ class BasicCartTests(RegistrationCartTestCase): def test_get_cart(self): current_cart = TestingCartController.for_user(self.USER_1) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() old_cart = current_cart @@ -214,8 +214,8 @@ class BasicCartTests(RegistrationCartTestCase): with self.assertRaises(ValidationError): current_cart.add_to_cart(self.PROD_1, 10) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() current_cart = TestingCartController.for_user(self.USER_1) # User should not be able to add 10 of PROD_1 to the current cart now, @@ -272,8 +272,8 @@ class BasicCartTests(RegistrationCartTestCase): with self.assertRaises(ValidationError): current_cart.add_to_cart(self.PROD_3, 1) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() current_cart = TestingCartController.for_user(self.USER_1) # The category limit should extend across carts @@ -298,8 +298,8 @@ class BasicCartTests(RegistrationCartTestCase): current_cart.add_to_cart(self.PROD_4, 1) # The limits should extend across carts... - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() current_cart = TestingCartController.for_user(self.USER_1) current_cart.set_quantity(self.PROD_3, 4) @@ -325,8 +325,8 @@ class BasicCartTests(RegistrationCartTestCase): current_cart.add_to_cart(item, quantity) self.assertTrue(item in prods) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() current_cart = TestingCartController.for_user(self.USER_1) diff --git a/registrasion/tests/test_ceilings.py b/registrasion/tests/test_ceilings.py index 94b1c311..cc333c29 100644 --- a/registrasion/tests/test_ceilings.py +++ b/registrasion/tests/test_ceilings.py @@ -91,8 +91,8 @@ class CeilingsTestCases(RegistrationCartTestCase): second_cart.add_to_cart(self.PROD_1, 1) # User 2 pays for their cart - second_cart.cart.active = False - second_cart.cart.save() + + second_cart.next_cart() # User 1 should not be able to add item to their cart # because user 2 has paid for their reserved item, exhausting @@ -128,8 +128,8 @@ class CeilingsTestCases(RegistrationCartTestCase): first_cart.validate_cart() # Paid cart outside the reservation window - second_cart.cart.active = False - second_cart.cart.save() + + second_cart.next_cart() self.add_timedelta(self.RESERVATION + datetime.timedelta(seconds=1)) with self.assertRaises(ValidationError): first_cart.validate_cart() @@ -140,15 +140,15 @@ class CeilingsTestCases(RegistrationCartTestCase): first_cart = TestingCartController.for_user(self.USER_1) first_cart.add_to_cart(self.PROD_1, 1) - first_cart.cart.active = False - first_cart.cart.save() + + first_cart.next_cart() second_cart = TestingCartController.for_user(self.USER_2) with self.assertRaises(ValidationError): second_cart.add_to_cart(self.PROD_1, 1) first_cart.cart.released = True - first_cart.cart.save() + first_cart.next_cart() second_cart.add_to_cart(self.PROD_1, 1) @@ -176,8 +176,8 @@ class CeilingsTestCases(RegistrationCartTestCase): cart.add_to_cart(self.PROD_1, 1) self.assertEqual(1, len(cart.cart.discountitem_set.all())) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() # The second cart has no voucher attached, so should apply the # ceiling discount diff --git a/registrasion/tests/test_discount.py b/registrasion/tests/test_discount.py index 860a90a3..c4211c0f 100644 --- a/registrasion/tests/test_discount.py +++ b/registrasion/tests/test_discount.py @@ -168,8 +168,8 @@ class DiscountTestCase(RegistrationCartTestCase): # Enable the discount during the first cart. cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_1, 1) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() # Use the discount in the second cart cart = TestingCartController.for_user(self.USER_1) @@ -177,8 +177,8 @@ class DiscountTestCase(RegistrationCartTestCase): # The discount should be applied. self.assertEqual(1, len(cart.cart.discountitem_set.all())) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() # The discount should respect the total quantity across all # of the user's carts. @@ -197,8 +197,8 @@ class DiscountTestCase(RegistrationCartTestCase): cart.add_to_cart(self.PROD_1, 1) # This would exhaust discount if present cart.add_to_cart(self.PROD_2, 2) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() self.add_discount_prod_1_includes_prod_2() cart = TestingCartController.for_user(self.USER_1) @@ -346,8 +346,8 @@ class DiscountTestCase(RegistrationCartTestCase): discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(2, discounts[0].quantity) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() def test_discount_quantity_is_correct_after_first_purchase(self): self.test_discount_quantity_is_correct_before_first_purchase() @@ -357,8 +357,8 @@ class DiscountTestCase(RegistrationCartTestCase): discounts = discount.available_discounts(self.USER_1, [self.CAT_2], []) self.assertEqual(1, discounts[0].quantity) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() def test_discount_is_gone_after_quantity_exhausted(self): self.test_discount_quantity_is_correct_after_first_purchase() @@ -388,12 +388,12 @@ class DiscountTestCase(RegistrationCartTestCase): self.assertEqual(1, len(discounts)) cart.cart.active = False # Keep discount enabled - cart.cart.save() + cart.next_cart() cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_2, 2) # The discount will be exhausted - cart.cart.active = False - cart.cart.save() + + cart.next_cart() discounts = discount.available_discounts( self.USER_1, @@ -403,7 +403,7 @@ class DiscountTestCase(RegistrationCartTestCase): self.assertEqual(0, len(discounts)) cart.cart.released = True - cart.cart.save() + cart.next_cart() discounts = discount.available_discounts( self.USER_1, diff --git a/registrasion/tests/test_enabling_condition.py b/registrasion/tests/test_enabling_condition.py index 6dd3d102..bedb6c86 100644 --- a/registrasion/tests/test_enabling_condition.py +++ b/registrasion/tests/test_enabling_condition.py @@ -68,8 +68,8 @@ class EnablingConditionTestCases(RegistrationCartTestCase): current_cart = TestingCartController.for_user(self.USER_1) current_cart.add_to_cart(self.PROD_2, 1) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() # Create new cart and try to add PROD_1 current_cart = TestingCartController.for_user(self.USER_1) @@ -103,8 +103,8 @@ class EnablingConditionTestCases(RegistrationCartTestCase): current_cart = TestingCartController.for_user(self.USER_1) current_cart.add_to_cart(self.PROD_3, 1) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() # Create new cart and try to add PROD_1 current_cart = TestingCartController.for_user(self.USER_1) @@ -241,15 +241,15 @@ class EnablingConditionTestCases(RegistrationCartTestCase): cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_3, 1) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() cart_2 = TestingCartController.for_user(self.USER_1) cart_2.add_to_cart(self.PROD_1, 1) cart_2.set_quantity(self.PROD_1, 0) cart.cart.released = True - cart.cart.save() + cart.next_cart() with self.assertRaises(ValidationError): cart_2.set_quantity(self.PROD_1, 1) @@ -260,15 +260,15 @@ class EnablingConditionTestCases(RegistrationCartTestCase): cart = TestingCartController.for_user(self.USER_1) cart.add_to_cart(self.PROD_2, 1) - cart.cart.active = False - cart.cart.save() + + cart.next_cart() cart_2 = TestingCartController.for_user(self.USER_1) cart_2.add_to_cart(self.PROD_1, 1) cart_2.set_quantity(self.PROD_1, 0) cart.cart.released = True - cart.cart.save() + cart.next_cart() with self.assertRaises(ValidationError): cart_2.set_quantity(self.PROD_1, 1) diff --git a/registrasion/tests/test_voucher.py b/registrasion/tests/test_voucher.py index 1502c2fc..a8686fb1 100644 --- a/registrasion/tests/test_voucher.py +++ b/registrasion/tests/test_voucher.py @@ -34,8 +34,8 @@ class VoucherTestCases(RegistrationCartTestCase): # user 2 should be able to apply voucher self.add_timedelta(rego.Voucher.RESERVATION_DURATION * 2) cart_2.apply_voucher(voucher.code) - cart_2.cart.active = False - cart_2.cart.save() + + cart_2.next_cart() # After the reservation duration, even though the voucher has applied, # it exceeds the number of vouchers available. @@ -125,8 +125,8 @@ class VoucherTestCases(RegistrationCartTestCase): current_cart = TestingCartController.for_user(self.USER_1) current_cart.apply_voucher(voucher.code) - current_cart.cart.active = False - current_cart.cart.save() + + current_cart.next_cart() current_cart = TestingCartController.for_user(self.USER_1)