2016-01-22 05:01:30 +00:00
|
|
|
import datetime
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
from django.contrib.auth.models import User
|
2016-03-04 02:18:58 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2016-01-22 05:01:30 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-04-25 05:32:59 +00:00
|
|
|
from django.core.management import call_command
|
2016-01-22 05:01:30 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import conditions
|
|
|
|
from registrasion.models import inventory
|
|
|
|
from registrasion.models import people
|
2016-05-01 01:55:14 +00:00
|
|
|
from registrasion.controllers.batch import BatchController
|
2016-04-02 02:14:34 +00:00
|
|
|
from registrasion.controllers.product import ProductController
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2017-04-22 08:39:07 +00:00
|
|
|
from registrasion.tests.controller_helpers import TestingCartController
|
|
|
|
from registrasion.tests.patches import MixInPatches
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
UTC = pytz.timezone('UTC')
|
|
|
|
|
2016-01-22 06:02:07 +00:00
|
|
|
|
2016-08-21 06:39:57 +00:00
|
|
|
class RegistrationCartTestCase(MixInPatches, TestCase):
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(RegistrationCartTestCase, self).setUp()
|
|
|
|
|
2016-04-25 05:32:59 +00:00
|
|
|
def tearDown(self):
|
2016-04-28 02:39:20 +00:00
|
|
|
if True:
|
2016-04-25 05:32:59 +00:00
|
|
|
# If you're seeing segfaults in tests, enable this.
|
2016-04-25 07:37:33 +00:00
|
|
|
call_command(
|
|
|
|
'flush',
|
|
|
|
verbosity=0,
|
|
|
|
interactive=False,
|
|
|
|
reset_sequences=False,
|
|
|
|
allow_cascade=False,
|
|
|
|
inhibit_post_migrate=False
|
|
|
|
)
|
2016-04-25 05:32:59 +00:00
|
|
|
|
|
|
|
super(RegistrationCartTestCase, self).tearDown()
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-04-07 06:59:03 +00:00
|
|
|
|
|
|
|
super(RegistrationCartTestCase, cls).setUpTestData()
|
|
|
|
|
2016-01-22 06:02:07 +00:00
|
|
|
cls.USER_1 = User.objects.create_user(
|
|
|
|
username='testuser',
|
|
|
|
email='test@example.com',
|
|
|
|
password='top_secret')
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-01-22 06:02:07 +00:00
|
|
|
cls.USER_2 = User.objects.create_user(
|
|
|
|
username='testuser2',
|
|
|
|
email='test2@example.com',
|
|
|
|
password='top_secret')
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
attendee1 = people.Attendee.get_instance(cls.USER_1)
|
2016-04-25 07:37:33 +00:00
|
|
|
people.AttendeeProfileBase.objects.create(
|
2016-04-22 05:06:24 +00:00
|
|
|
attendee=attendee1,
|
|
|
|
)
|
|
|
|
attendee2 = people.Attendee.get_instance(cls.USER_2)
|
2016-04-25 07:37:33 +00:00
|
|
|
people.AttendeeProfileBase.objects.create(
|
2016-04-22 05:06:24 +00:00
|
|
|
attendee=attendee2,
|
|
|
|
)
|
2016-04-07 06:59:03 +00:00
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
cls.RESERVATION = datetime.timedelta(hours=1)
|
|
|
|
|
2016-03-27 01:08:17 +00:00
|
|
|
cls.categories = []
|
2017-04-22 08:39:07 +00:00
|
|
|
for i in range(2):
|
2016-04-22 05:06:24 +00:00
|
|
|
cat = inventory.Category.objects.create(
|
2016-03-27 01:08:17 +00:00
|
|
|
name="Category " + str(i + 1),
|
|
|
|
description="This is a test category",
|
|
|
|
order=i,
|
2016-04-22 05:06:24 +00:00
|
|
|
render_type=inventory.Category.RENDER_TYPE_RADIO,
|
2016-03-27 01:08:17 +00:00
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
cls.categories.append(cat)
|
|
|
|
|
|
|
|
cls.CAT_1 = cls.categories[0]
|
|
|
|
cls.CAT_2 = cls.categories[1]
|
|
|
|
|
|
|
|
cls.products = []
|
2017-04-22 08:39:07 +00:00
|
|
|
for i in range(4):
|
2016-04-22 05:06:24 +00:00
|
|
|
prod = inventory.Product.objects.create(
|
2016-03-27 08:25:24 +00:00
|
|
|
name="Product " + str(i + 1),
|
2016-03-27 01:24:48 +00:00
|
|
|
description="This is a test product.",
|
2017-04-22 08:43:50 +00:00
|
|
|
category=cls.categories[int(i / 2)], # 2 products per category
|
2016-03-27 01:08:17 +00:00
|
|
|
price=Decimal("10.00"),
|
|
|
|
reservation_duration=cls.RESERVATION,
|
|
|
|
limit_per_user=10,
|
|
|
|
order=1,
|
|
|
|
)
|
|
|
|
cls.products.append(prod)
|
|
|
|
|
|
|
|
cls.PROD_1 = cls.products[0]
|
|
|
|
cls.PROD_2 = cls.products[1]
|
|
|
|
cls.PROD_3 = cls.products[2]
|
|
|
|
cls.PROD_4 = cls.products[3]
|
|
|
|
|
|
|
|
cls.PROD_4.price = Decimal("5.00")
|
2016-03-04 21:07:45 +00:00
|
|
|
cls.PROD_4.save()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-25 04:31:25 +00:00
|
|
|
# Burn through some carts -- this made some past flag tests fail
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(cls.USER_1)
|
2016-04-06 07:24:25 +00:00
|
|
|
|
|
|
|
current_cart.next_cart()
|
2016-04-02 09:11:31 +00:00
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(cls.USER_2)
|
2016-04-06 07:24:25 +00:00
|
|
|
|
|
|
|
current_cart.next_cart()
|
2016-04-02 09:11:31 +00:00
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
@classmethod
|
|
|
|
def make_ceiling(cls, name, limit=None, start_time=None, end_time=None):
|
2016-04-22 05:06:24 +00:00
|
|
|
limit_ceiling = conditions.TimeOrStockLimitFlag.objects.create(
|
2016-01-22 05:01:30 +00:00
|
|
|
description=name,
|
2016-04-22 05:06:24 +00:00
|
|
|
condition=conditions.FlagBase.DISABLE_IF_FALSE,
|
2016-01-22 05:01:30 +00:00
|
|
|
limit=limit,
|
|
|
|
start_time=start_time,
|
|
|
|
end_time=end_time
|
|
|
|
)
|
|
|
|
limit_ceiling.products.add(cls.PROD_1, cls.PROD_2)
|
|
|
|
|
|
|
|
@classmethod
|
2016-01-22 06:02:07 +00:00
|
|
|
def make_category_ceiling(
|
|
|
|
cls, name, limit=None, start_time=None, end_time=None):
|
2016-04-22 05:06:24 +00:00
|
|
|
limit_ceiling = conditions.TimeOrStockLimitFlag.objects.create(
|
2016-01-22 05:01:30 +00:00
|
|
|
description=name,
|
2016-04-22 05:06:24 +00:00
|
|
|
condition=conditions.FlagBase.DISABLE_IF_FALSE,
|
2016-01-22 05:01:30 +00:00
|
|
|
limit=limit,
|
|
|
|
start_time=start_time,
|
|
|
|
end_time=end_time
|
|
|
|
)
|
|
|
|
limit_ceiling.categories.add(cls.CAT_1)
|
|
|
|
|
|
|
|
@classmethod
|
2016-01-22 06:02:07 +00:00
|
|
|
def make_discount_ceiling(
|
2016-04-03 02:53:36 +00:00
|
|
|
cls, name, limit=None, start_time=None, end_time=None,
|
|
|
|
percentage=100):
|
2016-04-22 05:06:24 +00:00
|
|
|
limit_ceiling = conditions.TimeOrStockLimitDiscount.objects.create(
|
2016-01-22 05:01:30 +00:00
|
|
|
description=name,
|
|
|
|
start_time=start_time,
|
|
|
|
end_time=end_time,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2016-04-22 05:06:24 +00:00
|
|
|
conditions.DiscountForProduct.objects.create(
|
2016-01-22 05:01:30 +00:00
|
|
|
discount=limit_ceiling,
|
|
|
|
product=cls.PROD_1,
|
2016-04-03 02:53:36 +00:00
|
|
|
percentage=percentage,
|
2016-01-22 05:01:30 +00:00
|
|
|
quantity=10,
|
2016-04-25 04:31:25 +00:00
|
|
|
)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-03 02:53:36 +00:00
|
|
|
@classmethod
|
|
|
|
def new_voucher(self, code="VOUCHER", limit=1):
|
2016-04-22 05:06:24 +00:00
|
|
|
voucher = inventory.Voucher.objects.create(
|
2016-04-03 02:53:36 +00:00
|
|
|
recipient="Voucher recipient",
|
|
|
|
code=code,
|
|
|
|
limit=limit,
|
|
|
|
)
|
|
|
|
return voucher
|
|
|
|
|
2016-04-07 08:26:31 +00:00
|
|
|
@classmethod
|
|
|
|
def reget(cls, object):
|
|
|
|
return type(object).objects.get(id=object.id)
|
|
|
|
|
2016-04-03 02:53:36 +00:00
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
class BasicCartTests(RegistrationCartTestCase):
|
|
|
|
|
|
|
|
def test_get_cart(self):
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-06 07:24:25 +00:00
|
|
|
current_cart.next_cart()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
old_cart = current_cart
|
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-01-22 05:01:30 +00:00
|
|
|
self.assertNotEqual(old_cart.cart, current_cart.cart)
|
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart2 = TestingCartController.for_user(self.USER_1)
|
2016-01-22 05:01:30 +00:00
|
|
|
self.assertEqual(current_cart.cart, current_cart2.cart)
|
|
|
|
|
|
|
|
def test_add_to_cart_collapses_product_items(self):
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
# Add a product twice
|
|
|
|
current_cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
current_cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
|
2016-01-22 06:02:07 +00:00
|
|
|
# Count of products for a given user should be collapsed.
|
2016-04-22 05:06:24 +00:00
|
|
|
items = commerce.ProductItem.objects.filter(
|
2016-01-22 06:02:07 +00:00
|
|
|
cart=current_cart.cart,
|
2016-01-22 05:01:30 +00:00
|
|
|
product=self.PROD_1)
|
|
|
|
self.assertEqual(1, len(items))
|
|
|
|
item = items[0]
|
|
|
|
self.assertEquals(2, item.quantity)
|
|
|
|
|
2016-03-04 02:18:58 +00:00
|
|
|
def test_set_quantity(self):
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-03-04 02:18:58 +00:00
|
|
|
|
|
|
|
def get_item():
|
2016-04-22 05:06:24 +00:00
|
|
|
return commerce.ProductItem.objects.get(
|
2016-03-04 02:18:58 +00:00
|
|
|
cart=current_cart.cart,
|
|
|
|
product=self.PROD_1)
|
|
|
|
|
|
|
|
current_cart.set_quantity(self.PROD_1, 1)
|
|
|
|
self.assertEqual(1, get_item().quantity)
|
|
|
|
|
|
|
|
# Setting the quantity to zero should remove the entry from the cart.
|
|
|
|
current_cart.set_quantity(self.PROD_1, 0)
|
|
|
|
with self.assertRaises(ObjectDoesNotExist):
|
|
|
|
get_item()
|
|
|
|
|
|
|
|
current_cart.set_quantity(self.PROD_1, 9)
|
|
|
|
self.assertEqual(9, get_item().quantity)
|
|
|
|
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
current_cart.set_quantity(self.PROD_1, 11)
|
|
|
|
|
|
|
|
self.assertEqual(9, get_item().quantity)
|
|
|
|
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
current_cart.set_quantity(self.PROD_1, -1)
|
|
|
|
|
|
|
|
self.assertEqual(9, get_item().quantity)
|
|
|
|
|
|
|
|
current_cart.set_quantity(self.PROD_1, 2)
|
|
|
|
self.assertEqual(2, get_item().quantity)
|
|
|
|
|
2016-03-27 01:08:17 +00:00
|
|
|
def test_add_to_cart_product_per_user_limit(self):
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
# User should be able to add 1 of PROD_1 to the current cart.
|
|
|
|
current_cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
|
|
|
|
# User should be able to add 1 of PROD_1 to the current cart.
|
|
|
|
current_cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2016-04-06 07:24:25 +00:00
|
|
|
current_cart.next_cart()
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-01-22 05:01:30 +00:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
# Second user should not be affected by first user's limits
|
2016-04-03 00:06:35 +00:00
|
|
|
second_user_cart = TestingCartController.for_user(self.USER_2)
|
2016-01-22 05:01:30 +00:00
|
|
|
second_user_cart.add_to_cart(self.PROD_1, 10)
|
2016-03-27 01:24:48 +00:00
|
|
|
|
|
|
|
def set_limits(self):
|
|
|
|
self.CAT_2.limit_per_user = 10
|
|
|
|
self.PROD_2.limit_per_user = None
|
|
|
|
self.PROD_3.limit_per_user = None
|
|
|
|
self.PROD_4.limit_per_user = 6
|
|
|
|
|
|
|
|
self.CAT_2.save()
|
|
|
|
self.PROD_2.save()
|
|
|
|
self.PROD_3.save()
|
|
|
|
self.PROD_4.save()
|
|
|
|
|
|
|
|
def test_per_user_product_limit_ignored_if_blank(self):
|
|
|
|
self.set_limits()
|
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-03-27 01:24:48 +00:00
|
|
|
# There is no product limit on PROD_2, and there is no cat limit
|
|
|
|
current_cart.add_to_cart(self.PROD_2, 1)
|
|
|
|
# There is no product limit on PROD_3, but there is a cat limit
|
|
|
|
current_cart.add_to_cart(self.PROD_3, 1)
|
|
|
|
|
|
|
|
def test_per_user_category_limit_ignored_if_blank(self):
|
|
|
|
self.set_limits()
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-03-27 01:24:48 +00:00
|
|
|
# There is no product limit on PROD_2, and there is no cat limit
|
|
|
|
current_cart.add_to_cart(self.PROD_2, 1)
|
|
|
|
# There is no cat limit on PROD_1, but there is a prod limit
|
|
|
|
current_cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
|
|
|
|
def test_per_user_category_limit_only(self):
|
|
|
|
self.set_limits()
|
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-03-27 01:24:48 +00:00
|
|
|
|
|
|
|
# Cannot add to cart if category limit is filled by one product.
|
|
|
|
current_cart.set_quantity(self.PROD_3, 10)
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
current_cart.set_quantity(self.PROD_4, 1)
|
|
|
|
|
|
|
|
# Can add to cart if category limit is not filled by one product
|
|
|
|
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)
|
|
|
|
|
2016-04-06 07:24:25 +00:00
|
|
|
current_cart.next_cart()
|
2016-03-27 01:24:48 +00:00
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-03-27 01:24:48 +00:00
|
|
|
# The category limit should extend across carts
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
current_cart.add_to_cart(self.PROD_3, 10)
|
|
|
|
|
|
|
|
def test_per_user_category_and_product_limits(self):
|
|
|
|
self.set_limits()
|
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-03-27 01:24:48 +00:00
|
|
|
|
|
|
|
# Hit both the product and category edges:
|
|
|
|
current_cart.set_quantity(self.PROD_3, 4)
|
|
|
|
current_cart.set_quantity(self.PROD_4, 6)
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
# There's unlimited PROD_3, but limited in the category
|
|
|
|
current_cart.add_to_cart(self.PROD_3, 1)
|
|
|
|
|
|
|
|
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...
|
2016-04-06 07:24:25 +00:00
|
|
|
|
|
|
|
current_cart.next_cart()
|
2016-03-27 01:24:48 +00:00
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-03-27 01:24:48 +00:00
|
|
|
current_cart.set_quantity(self.PROD_3, 4)
|
|
|
|
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
current_cart.set_quantity(self.PROD_3, 5)
|
|
|
|
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
current_cart.set_quantity(self.PROD_4, 1)
|
2016-04-02 02:14:34 +00:00
|
|
|
|
|
|
|
def __available_products_test(self, item, quantity):
|
|
|
|
self.set_limits()
|
|
|
|
|
2016-04-02 02:29:53 +00:00
|
|
|
def get_prods():
|
|
|
|
return ProductController.available_products(
|
|
|
|
self.USER_1,
|
|
|
|
products=[self.PROD_2, self.PROD_3, self.PROD_4],
|
|
|
|
)
|
2016-04-02 02:14:34 +00:00
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-04-02 02:14:34 +00:00
|
|
|
prods = get_prods()
|
|
|
|
self.assertTrue(item in prods)
|
|
|
|
current_cart.add_to_cart(item, quantity)
|
|
|
|
self.assertTrue(item in prods)
|
|
|
|
|
2016-04-06 07:24:25 +00:00
|
|
|
current_cart.next_cart()
|
2016-04-02 02:14:34 +00:00
|
|
|
|
2016-04-03 00:06:35 +00:00
|
|
|
current_cart = TestingCartController.for_user(self.USER_1)
|
2016-04-02 02:14:34 +00:00
|
|
|
|
|
|
|
prods = get_prods()
|
|
|
|
self.assertTrue(item not in prods)
|
|
|
|
|
|
|
|
def test_available_products_respects_category_limits(self):
|
|
|
|
self.__available_products_test(self.PROD_3, 10)
|
|
|
|
|
|
|
|
def test_available_products_respects_product_limits(self):
|
|
|
|
self.__available_products_test(self.PROD_4, 6)
|
2016-05-01 01:55:14 +00:00
|
|
|
|
2016-05-01 02:05:51 +00:00
|
|
|
def test_cart_controller_for_user_is_memoised(self):
|
2016-05-01 01:55:14 +00:00
|
|
|
# - that for_user is memoised
|
|
|
|
with BatchController.batch(self.USER_1):
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
cart_2 = TestingCartController.for_user(self.USER_1)
|
|
|
|
self.assertIs(cart, cart_2)
|
|
|
|
|
2016-05-01 02:05:51 +00:00
|
|
|
def test_cart_revision_does_not_increment_if_not_modified(self):
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
2016-05-01 01:55:14 +00:00
|
|
|
rev_0 = cart.cart.revision
|
2016-05-01 02:05:51 +00:00
|
|
|
|
2016-05-01 01:55:14 +00:00
|
|
|
with BatchController.batch(self.USER_1):
|
|
|
|
# Memoise the cart
|
2016-09-02 01:43:27 +00:00
|
|
|
TestingCartController.for_user(self.USER_1)
|
2016-05-01 01:55:14 +00:00
|
|
|
# Do nothing on exit
|
2016-08-21 06:39:57 +00:00
|
|
|
|
2016-05-01 01:55:14 +00:00
|
|
|
rev_1 = self.reget(cart.cart).revision
|
|
|
|
self.assertEqual(rev_0, rev_1)
|
|
|
|
|
2016-05-01 02:05:51 +00:00
|
|
|
def test_cart_revision_only_increments_at_end_of_batches(self):
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
2016-05-01 01:55:14 +00:00
|
|
|
rev_0 = cart.cart.revision
|
2016-05-01 02:05:51 +00:00
|
|
|
|
2016-05-01 01:55:14 +00:00
|
|
|
with BatchController.batch(self.USER_1):
|
|
|
|
# Memoise the cart
|
|
|
|
same_cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
same_cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
rev_1 = self.reget(same_cart.cart).revision
|
|
|
|
|
|
|
|
rev_2 = self.reget(cart.cart).revision
|
|
|
|
|
|
|
|
self.assertEqual(rev_0, rev_1)
|
|
|
|
self.assertNotEqual(rev_0, rev_2)
|
|
|
|
|
2016-05-01 02:05:51 +00:00
|
|
|
def test_cart_discounts_only_calculated_at_end_of_batches(self):
|
2016-05-01 01:55:14 +00:00
|
|
|
def count_discounts(cart):
|
|
|
|
return cart.cart.discountitem_set.count()
|
|
|
|
|
2016-05-01 02:05:51 +00:00
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
2016-05-01 01:55:14 +00:00
|
|
|
self.make_discount_ceiling("FLOOZLE")
|
2016-05-01 02:19:20 +00:00
|
|
|
count_0 = count_discounts(cart)
|
2016-05-01 02:05:51 +00:00
|
|
|
|
2016-05-01 01:55:14 +00:00
|
|
|
with BatchController.batch(self.USER_1):
|
|
|
|
# Memoise the cart
|
|
|
|
same_cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
|
|
|
|
with BatchController.batch(self.USER_1):
|
|
|
|
# Memoise the cart
|
|
|
|
same_cart_2 = TestingCartController.for_user(self.USER_1)
|
|
|
|
|
|
|
|
same_cart_2.add_to_cart(self.PROD_1, 1)
|
2016-05-01 02:19:20 +00:00
|
|
|
count_1 = count_discounts(same_cart_2)
|
2016-05-01 01:55:14 +00:00
|
|
|
|
2016-05-01 02:19:20 +00:00
|
|
|
count_2 = count_discounts(same_cart)
|
|
|
|
|
|
|
|
count_3 = count_discounts(cart)
|
2016-05-01 01:55:14 +00:00
|
|
|
|
|
|
|
self.assertEqual(0, count_0)
|
|
|
|
self.assertEqual(0, count_1)
|
|
|
|
self.assertEqual(0, count_2)
|
2016-05-01 02:19:20 +00:00
|
|
|
self.assertEqual(1, count_3)
|
2016-10-06 18:52:46 +00:00
|
|
|
|
|
|
|
def test_reservation_duration_forwards(self):
|
|
|
|
''' Reservation duration should be the maximum of the durations (small)
|
|
|
|
'''
|
|
|
|
|
|
|
|
new_res = self.RESERVATION * 2
|
|
|
|
self.PROD_2.reservation_duration = new_res
|
|
|
|
self.PROD_2.save()
|
|
|
|
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
|
|
|
|
|
|
|
|
cart.add_to_cart(self.PROD_2, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, new_res)
|
|
|
|
|
|
|
|
def test_reservation_duration_backwards(self):
|
|
|
|
''' Reservation duration should be the maximum of the durations (big)
|
|
|
|
'''
|
|
|
|
|
|
|
|
new_res = self.RESERVATION * 2
|
|
|
|
self.PROD_2.reservation_duration = new_res
|
|
|
|
self.PROD_2.save()
|
|
|
|
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
|
|
|
|
cart.add_to_cart(self.PROD_2, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, new_res)
|
|
|
|
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, new_res)
|
2016-10-06 19:12:50 +00:00
|
|
|
|
|
|
|
def test_reservation_duration_removals(self):
|
|
|
|
''' Reservation duration should update with removals
|
|
|
|
'''
|
|
|
|
|
|
|
|
new_res = self.RESERVATION * 2
|
|
|
|
self.PROD_2.reservation_duration = new_res
|
|
|
|
self.PROD_2.save()
|
|
|
|
|
|
|
|
self.set_time(datetime.datetime(2015, 1, 1, tzinfo=UTC))
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
|
|
|
|
one_third = new_res / 3
|
|
|
|
|
|
|
|
cart.add_to_cart(self.PROD_2, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, new_res)
|
|
|
|
|
|
|
|
# Reservation duration should not decrease if time hasn't decreased
|
|
|
|
cart.set_quantity(self.PROD_2, 0)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, new_res)
|
|
|
|
|
|
|
|
# Adding a new product should not reset the reservation duration below
|
|
|
|
# the old one
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, new_res)
|
|
|
|
|
|
|
|
self.add_timedelta(one_third)
|
|
|
|
|
|
|
|
# The old reservation duration is still longer than PROD_1's
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, new_res - one_third)
|
|
|
|
|
|
|
|
self.add_timedelta(one_third)
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
|
2016-10-06 19:33:53 +00:00
|
|
|
|
|
|
|
def test_reservation_extension_less_than_current(self):
|
|
|
|
''' Reservation extension should have no effect if it's too small
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.set_time(datetime.datetime(2015, 1, 1, tzinfo=UTC))
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
|
|
|
|
|
|
|
|
cart.extend_reservation(datetime.timedelta(minutes=30))
|
|
|
|
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
|
|
|
|
|
|
|
|
def test_reservation_extension(self):
|
|
|
|
''' Test various reservation extension bits.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.set_time(datetime.datetime(2015, 1, 1, tzinfo=UTC))
|
|
|
|
cart = TestingCartController.for_user(self.USER_1)
|
|
|
|
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
|
|
|
|
|
|
|
|
hours = datetime.timedelta(hours=1)
|
|
|
|
cart.extend_reservation(24 * hours)
|
|
|
|
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, 24 * hours)
|
|
|
|
|
|
|
|
self.add_timedelta(1 * hours)
|
|
|
|
|
|
|
|
# PROD_1's reservation is less than what we've added to the cart
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(cart.cart.reservation_duration, 23 * hours)
|
|
|
|
|
|
|
|
# Now the extension should only have 59 minutes remaining
|
|
|
|
# so the autoextend behaviour should kick in
|
|
|
|
self.add_timedelta(datetime.timedelta(hours=22, minutes=1))
|
|
|
|
cart.add_to_cart(self.PROD_1, 1)
|
|
|
|
cart.cart.refresh_from_db()
|
|
|
|
self.assertEqual(
|
|
|
|
cart.cart.reservation_duration,
|
|
|
|
self.PROD_1.reservation_duration,
|
|
|
|
)
|