Files
@ c41a9cadffb9
Branch filter:
Location: symposion_app/registrasion/tests/test_voucher.py
c41a9cadffb9
3.6 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 | import datetime
import pytz
from decimal import Decimal
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from registrasion import models as rego
from registrasion.controllers.cart import CartController
from test_cart import RegistrationCartTestCase
UTC = pytz.timezone('UTC')
class VoucherTestCases(RegistrationCartTestCase):
@classmethod
def new_voucher(self, code="VOUCHER"):
voucher = rego.Voucher.objects.create(
recipient="Voucher recipient",
code=code,
limit=1
)
voucher.save()
return voucher
def test_apply_voucher(self):
voucher = self.new_voucher()
self.set_time(datetime.datetime(2015, 01, 01, tzinfo=UTC))
cart_1 = CartController.for_user(self.USER_1)
cart_1.apply_voucher(voucher.code)
self.assertIn(voucher, cart_1.cart.vouchers.all())
# Second user should not be able to apply this voucher (it's exhausted)
cart_2 = CartController.for_user(self.USER_2)
with self.assertRaises(ValidationError):
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()
# After the reservation duration, user 1 should not be able to apply
# voucher, as user 2 has paid for their cart.
self.add_timedelta(rego.Voucher.RESERVATION_DURATION * 2)
with self.assertRaises(ValidationError):
cart_1.apply_voucher(voucher.code)
def test_voucher_enables_item(self):
voucher = self.new_voucher()
enabling_condition = rego.VoucherEnablingCondition.objects.create(
description="Voucher condition",
voucher=voucher,
mandatory=False,
)
enabling_condition.save()
enabling_condition.products.add(self.PROD_1)
enabling_condition.save()
# Adding the product without a voucher will not work
current_cart = CartController.for_user(self.USER_1)
with self.assertRaises(ValidationError):
current_cart.add_to_cart(self.PROD_1, 1)
# Apply the voucher
current_cart.apply_voucher(voucher.code)
current_cart.add_to_cart(self.PROD_1, 1)
def test_voucher_enables_discount(self):
voucher = self.new_voucher()
discount = rego.VoucherDiscount.objects.create(
description="VOUCHER RECIPIENT",
voucher=voucher,
)
discount.save()
rego.DiscountForProduct.objects.create(
discount=discount,
product=self.PROD_1,
percentage=Decimal(100),
quantity=1
).save()
# Having PROD_1 in place should add a discount
current_cart = CartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code)
current_cart.add_to_cart(self.PROD_1, 1)
self.assertEqual(1, len(current_cart.cart.discountitem_set.all()))
def test_voucher_codes_unique(self):
self.new_voucher(code="VOUCHER")
with self.assertRaises(IntegrityError):
self.new_voucher(code="VOUCHER")
def test_multiple_vouchers_work(self):
self.new_voucher(code="VOUCHER1")
self.new_voucher(code="VOUCHER2")
def test_vouchers_case_insensitive(self):
voucher = self.new_voucher(code="VOUCHeR")
current_cart = CartController.for_user(self.USER_1)
current_cart.apply_voucher(voucher.code.lower())
|