Files
@ e9ebf5da03ea
Branch filter:
Location: symposion_app/registrasion/tests/test_flag.py
e9ebf5da03ea
12.4 KiB
text/x-python
Writes inventory and overview documentation, and updates docstrings on a LOT of modules.
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | import pytz
from django.core.exceptions import ValidationError
from registrasion.models import commerce
from registrasion.models import conditions
from registrasion.controllers.category import CategoryController
from controller_helpers import TestingCartController
from registrasion.controllers.product import ProductController
from test_cart import RegistrationCartTestCase
UTC = pytz.timezone('UTC')
class FlagTestCases(RegistrationCartTestCase):
@classmethod
def add_product_flag(cls, condition=conditions.FlagBase.ENABLE_IF_TRUE):
''' Adds a product flag condition: adding PROD_1 to a cart is
predicated on adding PROD_2 beforehand. '''
flag = conditions.ProductFlag.objects.create(
description="Product condition",
condition=condition,
)
flag.save()
flag.products.add(cls.PROD_1)
flag.enabling_products.add(cls.PROD_2)
flag.save()
@classmethod
def add_product_flag_on_category(
cls,
condition=conditions.FlagBase.ENABLE_IF_TRUE,
):
''' Adds a product flag condition that operates on a category:
adding an item from CAT_1 is predicated on adding PROD_3 beforehand '''
flag = conditions.ProductFlag.objects.create(
description="Product condition",
condition=condition,
)
flag.save()
flag.categories.add(cls.CAT_1)
flag.enabling_products.add(cls.PROD_3)
flag.save()
def add_category_flag(cls, condition=conditions.FlagBase.ENABLE_IF_TRUE):
''' Adds a category flag condition: adding PROD_1 to a cart is
predicated on adding an item from CAT_2 beforehand.'''
flag = conditions.CategoryFlag.objects.create(
description="Category condition",
condition=condition,
enabling_category=cls.CAT_2,
)
flag.save()
flag.products.add(cls.PROD_1)
flag.save()
def test_product_flag_enables_product(self):
self.add_product_flag()
# Cannot buy PROD_1 without buying PROD_2
current_cart = TestingCartController.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_flag()
current_cart = TestingCartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_2, 1)
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_flag_enables_category(self):
self.add_product_flag_on_category()
# Cannot buy PROD_1 without buying item from CAT_2
current_cart = TestingCartController.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_flag_enables_product(self):
self.add_category_flag()
# Cannot buy PROD_1 without buying PROD_2
current_cart = TestingCartController.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_flag()
current_cart = TestingCartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_3, 1)
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_eit_conditions(self):
self.add_product_flag()
self.add_category_flag()
# User 1 is testing the product flag condition
cart_1 = TestingCartController.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 flag condition
cart_2 = TestingCartController.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_dif_conditions(self):
self.add_product_flag(condition=conditions.FlagBase.DISABLE_IF_FALSE)
self.add_category_flag(condition=conditions.FlagBase.DISABLE_IF_FALSE)
cart_1 = TestingCartController.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_eit_and_dif_conditions_work_together(self):
self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
self.add_category_flag(condition=conditions.FlagBase.DISABLE_IF_FALSE)
cart_1 = TestingCartController.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 EIT condition
# Need to meet both conditions before you can add
with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1)
cart_1.set_quantity(self.PROD_2, 0) # Un-meets the EIT condition
cart_1.add_to_cart(self.PROD_3, 1) # Meets the DIF condition
# Need to meet both conditions before you can add
with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1)
cart_1.add_to_cart(self.PROD_2, 1) # Meets the EIT condition
# Now that both conditions are met, we can add the product
cart_1.add_to_cart(self.PROD_1, 1)
def test_available_products_works_with_no_conditions_set(self):
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_1,
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_2,
)
self.assertTrue(self.PROD_3 in prods)
self.assertTrue(self.PROD_4 in prods)
prods = ProductController.available_products(
self.USER_1,
products=[self.PROD_1, self.PROD_2, self.PROD_3, self.PROD_4],
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)
self.assertTrue(self.PROD_3 in prods)
self.assertTrue(self.PROD_4 in prods)
def test_available_products_on_category_works_when_condition_not_met(self):
self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_1,
)
self.assertTrue(self.PROD_1 not in prods)
self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_category_works_when_condition_is_met(self):
self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
cart_1 = TestingCartController.for_user(self.USER_1)
cart_1.add_to_cart(self.PROD_2, 1)
prods = ProductController.available_products(
self.USER_1,
category=self.CAT_1,
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_products_works_when_condition_not_met(self):
self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
prods = ProductController.available_products(
self.USER_1,
products=[self.PROD_1, self.PROD_2],
)
self.assertTrue(self.PROD_1 not in prods)
self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_products_works_when_condition_is_met(self):
self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
cart_1 = TestingCartController.for_user(self.USER_1)
cart_1.add_to_cart(self.PROD_2, 1)
prods = ProductController.available_products(
self.USER_1,
products=[self.PROD_1, self.PROD_2],
)
self.assertTrue(self.PROD_1 in prods)
self.assertTrue(self.PROD_2 in prods)
def test_category_flag_fails_if_cart_refunded(self):
self.add_category_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_3, 1)
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.next_cart()
with self.assertRaises(ValidationError):
cart_2.set_quantity(self.PROD_1, 1)
def test_product_flag_fails_if_cart_refunded(self):
self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_2, 1)
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.next_cart()
with self.assertRaises(ValidationError):
cart_2.set_quantity(self.PROD_1, 1)
def test_available_categories(self):
self.add_product_flag_on_category(
condition=conditions.FlagBase.ENABLE_IF_TRUE,
)
cart_1 = TestingCartController.for_user(self.USER_1)
cats = CategoryController.available_categories(
self.USER_1,
)
self.assertFalse(self.CAT_1 in cats)
self.assertTrue(self.CAT_2 in cats)
cart_1.add_to_cart(self.PROD_3, 1)
cats = CategoryController.available_categories(
self.USER_1,
)
self.assertTrue(self.CAT_1 in cats)
self.assertTrue(self.CAT_2 in cats)
def test_validate_cart_when_flags_become_unmet(self):
self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_2, 1)
cart.add_to_cart(self.PROD_1, 1)
# Should pass
cart.validate_cart()
cart.set_quantity(self.PROD_2, 0)
# Should fail
with self.assertRaises(ValidationError):
cart.validate_cart()
def test_fix_simple_errors_resolves_unavailable_products(self):
self.test_validate_cart_when_flags_become_unmet()
cart = TestingCartController.for_user(self.USER_1)
# Should just remove all of the unavailable products
cart.fix_simple_errors()
# Should now succeed
cart.validate_cart()
# Should keep PROD_2 in the cart
items = commerce.ProductItem.objects.filter(cart=cart.cart)
self.assertFalse([i for i in items if i.product == self.PROD_1])
def test_fix_simple_errors_does_not_remove_limited_items(self):
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_2, 1)
cart.add_to_cart(self.PROD_1, 10)
# Should just remove all of the unavailable products
cart.fix_simple_errors()
# Should now succeed
cart.validate_cart()
# Should keep PROD_2 in the cart
# and also PROD_1, which is now exhausted for user.
items = commerce.ProductItem.objects.filter(cart=cart.cart)
self.assertTrue([i for i in items if i.product == self.PROD_1])
|