Changeset - 2f77f5bb23ee
[Not reviewed]
0 6 0
Christopher Neugebauer - 8 years ago 2016-04-06 07:24:25
chrisjrn@gmail.com
Replaces .active = False; .save() pattern in tests with a test controller method
6 files changed with 55 insertions and 51 deletions:
0 comments (0 inline, 0 general)
registrasion/tests/cart_controller_helper.py
Show inline comments
...
 
@@ -21,6 +21,10 @@ class TestingCartController(CartController):
 
                cart=self.cart,
 
                product=product)
 
            old_quantity = product_item.quantity
 
        except ObjectDoesNotExist:
 
            old_quantity = 0
 
        self.set_quantity(product, old_quantity + quantity)
 

	
 
    def next_cart(self):
 
        self.cart.active = False
 
        self.cart.save()
registrasion/tests/test_cart.py
Show inline comments
...
 
@@ -71,18 +71,18 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase):
 

	
 
        cls.PROD_4.price = Decimal("5.00")
 
        cls.PROD_4.save()
 

	
 
        # 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):
 
        limit_ceiling = rego.TimeOrStockLimitEnablingCondition.objects.create(
 
            description=name,
 
            mandatory=True,
...
 
@@ -139,14 +139,14 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase):
 

	
 
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
 

	
 
        current_cart = TestingCartController.for_user(self.USER_1)
 
        self.assertNotEqual(old_cart.cart, current_cart.cart)
 

	
...
 
@@ -211,14 +211,14 @@ class BasicCartTests(RegistrationCartTestCase):
 

	
 
        # User should not be able to add 10 of PROD_1 to the current cart now,
 
        # because they have a limit of 10.
 
        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,
 
        # even though it's a new cart.
 
        with self.assertRaises(ValidationError):
 
            current_cart.add_to_cart(self.PROD_1, 10)
...
 
@@ -269,14 +269,14 @@ class BasicCartTests(RegistrationCartTestCase):
 
        current_cart.set_quantity(self.PROD_3, 5)
 
        current_cart.set_quantity(self.PROD_4, 5)
 
        # Cannot add to cart if category limit is filled by two products
 
        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
 
        with self.assertRaises(ValidationError):
 
            current_cart.add_to_cart(self.PROD_3, 10)
 

	
...
 
@@ -295,14 +295,14 @@ class BasicCartTests(RegistrationCartTestCase):
 
        current_cart.set_quantity(self.PROD_3, 0)
 
        with self.assertRaises(ValidationError):
 
            # There's only 6 allowed of PROD_4
 
            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)
 

	
 
        with self.assertRaises(ValidationError):
 
            current_cart.set_quantity(self.PROD_3, 5)
...
 
@@ -322,14 +322,14 @@ class BasicCartTests(RegistrationCartTestCase):
 
        current_cart = TestingCartController.for_user(self.USER_1)
 
        prods = get_prods()
 
        self.assertTrue(item in prods)
 
        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)
 

	
 
        prods = get_prods()
 
        self.assertTrue(item not in prods)
 

	
registrasion/tests/test_ceilings.py
Show inline comments
...
 
@@ -88,14 +88,14 @@ class CeilingsTestCases(RegistrationCartTestCase):
 
        # User 2 should be able to add item to their cart once the
 
        # reservation duration is elapsed
 
        self.add_timedelta(self.RESERVATION + datetime.timedelta(seconds=1))
 
        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
 
        # the ceiling, regardless of the reservation time.
 
        self.add_timedelta(self.RESERVATION * 20)
 
        with self.assertRaises(ValidationError):
...
 
@@ -125,33 +125,33 @@ class CeilingsTestCases(RegistrationCartTestCase):
 
        # Unpaid cart within reservation window
 
        second_cart.add_to_cart(self.PROD_1, 1)
 
        with self.assertRaises(ValidationError):
 
            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()
 

	
 
    def test_items_released_from_ceiling_by_refund(self):
 
        self.make_ceiling("Limit ceiling", limit=1)
 

	
 
        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)
 

	
 
    def test_discount_ceiling_only_counts_items_covered_by_ceiling(self):
 
        self.make_discount_ceiling("Limit ceiling", limit=1, percentage=50)
 
        voucher = self.new_voucher(code="VOUCHER")
...
 
@@ -173,14 +173,14 @@ class CeilingsTestCases(RegistrationCartTestCase):
 
        # the 100% discount from the voucher should apply to the first item
 
        # and not the ceiling discount.
 
        cart.apply_voucher("VOUCHER")
 
        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
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_1, 1)
 
        self.assertEqual(1, len(cart.cart.discountitem_set.all()))
registrasion/tests/test_discount.py
Show inline comments
...
 
@@ -165,23 +165,23 @@ class DiscountTestCase(RegistrationCartTestCase):
 
    def test_discount_applies_across_carts(self):
 
        self.add_discount_prod_1_includes_prod_2()
 

	
 
        # 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)
 
        cart.add_to_cart(self.PROD_2, 1)
 

	
 
        # 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.
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_2, 2)
 

	
...
 
@@ -194,14 +194,14 @@ class DiscountTestCase(RegistrationCartTestCase):
 
    def test_discount_applies_only_once_enabled(self):
 
        # Enable the discount during the first cart.
 
        cart = TestingCartController.for_user(self.USER_1)
 
        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)
 
        cart.add_to_cart(self.PROD_2, 2)
 

	
 
        discount_items = list(cart.cart.discountitem_set.all())
...
 
@@ -343,25 +343,25 @@ class DiscountTestCase(RegistrationCartTestCase):
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_1, 1)  # Enable the discount
 
        cart.add_to_cart(self.PROD_3, 1)  # Exhaust the quantity
 

	
 
        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()
 

	
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_3, 1)  # Exhaust the quantity
 

	
 
        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()
 
        discounts = discount.available_discounts(self.USER_1, [self.CAT_2], [])
 
        self.assertEqual(0, len(discounts))
 

	
...
 
@@ -385,28 +385,28 @@ class DiscountTestCase(RegistrationCartTestCase):
 
            [],
 
            [self.PROD_2],
 
        )
 
        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,
 
            [],
 
            [self.PROD_2],
 
        )
 
        self.assertEqual(0, len(discounts))
 

	
 
        cart.cart.released = True
 
        cart.cart.save()
 
        cart.next_cart()
 

	
 
        discounts = discount.available_discounts(
 
            self.USER_1,
 
            [],
 
            [self.PROD_2],
 
        )
registrasion/tests/test_enabling_condition.py
Show inline comments
...
 
@@ -65,14 +65,14 @@ class EnablingConditionTestCases(RegistrationCartTestCase):
 

	
 
    def test_product_enabled_by_product_in_previous_cart(self):
 
        self.add_product_enabling_condition()
 

	
 
        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)
 
        current_cart.add_to_cart(self.PROD_1, 1)
 

	
 
    def test_product_enabling_condition_enables_category(self):
...
 
@@ -100,14 +100,14 @@ class EnablingConditionTestCases(RegistrationCartTestCase):
 

	
 
    def test_product_enabled_by_category_in_previous_cart(self):
 
        self.add_category_enabling_condition()
 

	
 
        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)
 
        current_cart.add_to_cart(self.PROD_1, 1)
 

	
 
    def test_multiple_non_mandatory_conditions(self):
...
 
@@ -238,40 +238,40 @@ class EnablingConditionTestCases(RegistrationCartTestCase):
 
    def test_category_enabling_condition_fails_if_cart_refunded(self):
 
        self.add_category_enabling_condition(mandatory=False)
 

	
 
        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)
 

	
 
    def test_product_enabling_condition_fails_if_cart_refunded(self):
 
        self.add_product_enabling_condition(mandatory=False)
 

	
 
        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)
 

	
 
    def test_available_categories(self):
 
        self.add_product_enabling_condition_on_category(mandatory=False)
registrasion/tests/test_voucher.py
Show inline comments
...
 
@@ -31,14 +31,14 @@ class VoucherTestCases(RegistrationCartTestCase):
 
            cart_2.apply_voucher(voucher.code)
 

	
 
        # After the reservation duration
 
        # 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.
 
        self.add_timedelta(rego.Voucher.RESERVATION_DURATION * 2)
 
        with self.assertRaises(ValidationError):
 
            cart_1.validate_cart()
...
 
@@ -122,14 +122,14 @@ class VoucherTestCases(RegistrationCartTestCase):
 

	
 
    def test_voucher_can_only_be_applied_once_across_multiple_carts(self):
 
        voucher = self.new_voucher(limit=2)
 
        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)
 

	
 
        with self.assertRaises(ValidationError):
 
            current_cart.apply_voucher(voucher.code)
 

	
0 comments (0 inline, 0 general)