Files
@ c41a9cadffb9
Branch filter:
Location: symposion_app/registrasion/tests/test_enabling_condition.py - annotation
c41a9cadffb9
6.2 KiB
text/x-python
recalculate_discounts now uses the available_discounts function from controllers.discount.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 c2400c4695ae d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 224878a10ce3 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 224878a10ce3 d9e433659d01 d9e433659d01 224878a10ce3 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 224878a10ce3 d9e433659d01 d9e433659d01 224878a10ce3 d9e433659d01 | import pytz
from django.core.exceptions import ValidationError
from registrasion import models as rego
from registrasion.controllers.cart import CartController
from test_cart import RegistrationCartTestCase
UTC = pytz.timezone('UTC')
class EnablingConditionTestCases(RegistrationCartTestCase):
@classmethod
def add_product_enabling_condition(cls, mandatory=False):
''' Adds a product enabling condition: adding PROD_1 to a cart is
predicated on adding PROD_2 beforehand. '''
enabling_condition = rego.ProductEnablingCondition.objects.create(
description="Product condition",
mandatory=mandatory,
)
enabling_condition.save()
enabling_condition.products.add(cls.PROD_1)
enabling_condition.enabling_products.add(cls.PROD_2)
enabling_condition.save()
@classmethod
def add_product_enabling_condition_on_category(cls, mandatory=False):
''' Adds a product enabling condition that operates on a category:
adding an item from CAT_1 is predicated on adding PROD_3 beforehand '''
enabling_condition = rego.ProductEnablingCondition.objects.create(
description="Product condition",
mandatory=mandatory,
)
enabling_condition.save()
enabling_condition.categories.add(cls.CAT_1)
enabling_condition.enabling_products.add(cls.PROD_3)
enabling_condition.save()
def add_category_enabling_condition(cls, mandatory=False):
''' Adds a category enabling condition: adding PROD_1 to a cart is
predicated on adding an item from CAT_2 beforehand.'''
enabling_condition = rego.CategoryEnablingCondition.objects.create(
description="Category condition",
mandatory=mandatory,
enabling_category=cls.CAT_2,
)
enabling_condition.save()
enabling_condition.products.add(cls.PROD_1)
enabling_condition.save()
def test_product_enabling_condition_enables_product(self):
self.add_product_enabling_condition()
# Cannot buy PROD_1 without buying PROD_2
current_cart = CartController.for_user(self.USER_1)
with self.assertRaises(ValidationError):
current_cart.add_to_cart(self.PROD_1, 1)
current_cart.add_to_cart(self.PROD_2, 1)
current_cart.add_to_cart(self.PROD_1, 1)
def test_product_enabled_by_product_in_previous_cart(self):
self.add_product_enabling_condition()
current_cart = CartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_2, 1)
current_cart.cart.active = False
current_cart.cart.save()
# Create new cart and try to add PROD_1
current_cart = CartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_1, 1)
def test_product_enabling_condition_enables_category(self):
self.add_product_enabling_condition_on_category()
# Cannot buy PROD_1 without buying item from CAT_2
current_cart = CartController.for_user(self.USER_1)
with self.assertRaises(ValidationError):
current_cart.add_to_cart(self.PROD_1, 1)
current_cart.add_to_cart(self.PROD_3, 1)
current_cart.add_to_cart(self.PROD_1, 1)
def test_category_enabling_condition_enables_product(self):
self.add_category_enabling_condition()
# Cannot buy PROD_1 without buying PROD_2
current_cart = CartController.for_user(self.USER_1)
with self.assertRaises(ValidationError):
current_cart.add_to_cart(self.PROD_1, 1)
# PROD_3 is in CAT_2
current_cart.add_to_cart(self.PROD_3, 1)
current_cart.add_to_cart(self.PROD_1, 1)
def test_product_enabled_by_category_in_previous_cart(self):
self.add_category_enabling_condition()
current_cart = CartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_3, 1)
current_cart.cart.active = False
current_cart.cart.save()
# Create new cart and try to add PROD_1
current_cart = CartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_1, 1)
def test_multiple_non_mandatory_conditions(self):
self.add_product_enabling_condition()
self.add_category_enabling_condition()
# User 1 is testing the product enabling condition
cart_1 = CartController.for_user(self.USER_1)
# Cannot add PROD_1 until a condition is met
with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1)
cart_1.add_to_cart(self.PROD_2, 1)
cart_1.add_to_cart(self.PROD_1, 1)
# User 2 is testing the category enabling condition
cart_2 = CartController.for_user(self.USER_2)
# Cannot add PROD_1 until a condition is met
with self.assertRaises(ValidationError):
cart_2.add_to_cart(self.PROD_1, 1)
cart_2.add_to_cart(self.PROD_3, 1)
cart_2.add_to_cart(self.PROD_1, 1)
def test_multiple_mandatory_conditions(self):
self.add_product_enabling_condition(mandatory=True)
self.add_category_enabling_condition(mandatory=True)
cart_1 = CartController.for_user(self.USER_1)
# Cannot add PROD_1 until both conditions are met
with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1)
cart_1.add_to_cart(self.PROD_2, 1) # Meets the product condition
with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1)
cart_1.add_to_cart(self.PROD_3, 1) # Meets the category condition
cart_1.add_to_cart(self.PROD_1, 1)
def test_mandatory_conditions_are_mandatory(self):
self.add_product_enabling_condition(mandatory=False)
self.add_category_enabling_condition(mandatory=True)
cart_1 = CartController.for_user(self.USER_1)
# Cannot add PROD_1 until both conditions are met
with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1)
cart_1.add_to_cart(self.PROD_2, 1) # Meets the product condition
with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1)
cart_1.add_to_cart(self.PROD_3, 1) # Meets the category condition
cart_1.add_to_cart(self.PROD_1, 1)
|