You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

voucher_tests.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import datetime
  2. from decimal import Decimal as D
  3. from django.test import TestCase
  4. from django.core import exceptions
  5. from django.contrib.auth.models import User
  6. from django_dynamic_fixture import G
  7. from oscar.apps.voucher.models import Voucher
  8. from oscar.apps.order.models import Order
  9. START_DATE = datetime.date(2011, 01, 01)
  10. END_DATE = datetime.date(2012, 01, 01)
  11. class TestSavingAVoucher(TestCase):
  12. def test_saves_code_as_uppercase(self):
  13. voucher = Voucher(code='lower', start_date=START_DATE,
  14. end_date=END_DATE)
  15. voucher.save()
  16. self.assertEqual('LOWER', voucher.code)
  17. def test_verifies_dates_are_sensible(self):
  18. with self.assertRaises(exceptions.ValidationError):
  19. voucher = Voucher.objects.create(code='lower',
  20. start_date=END_DATE,
  21. end_date=START_DATE)
  22. voucher.clean()
  23. class TestAVoucher(TestCase):
  24. def setUp(self):
  25. self.voucher = Voucher(start_date=START_DATE, end_date=END_DATE)
  26. def test_is_active_between_start_and_end_dates(self):
  27. test = datetime.date(2011, 06, 10)
  28. self.assertTrue(self.voucher.is_active(test))
  29. def test_is_active_on_end_date(self):
  30. self.assertTrue(self.voucher.is_active(END_DATE))
  31. def test_is_active_on_start_date(self):
  32. self.assertTrue(self.voucher.is_active(START_DATE))
  33. def test_is_inactive_outside_of_start_and_end_dates(self):
  34. test = datetime.date(2012, 03, 10)
  35. self.assertFalse(self.voucher.is_active(test))
  36. def test_increments_total_discount_when_recording_usage(self):
  37. voucher = G(Voucher)
  38. voucher.record_discount({'discount': D('10.00')})
  39. self.assertEqual(voucher.total_discount, D('10.00'))
  40. voucher.record_discount({'discount': D('10.00')})
  41. self.assertEqual(voucher.total_discount, D('20.00'))
  42. class TestMultiuseVoucher(TestCase):
  43. def setUp(self):
  44. self.voucher = G(Voucher, usage=Voucher.MULTI_USE)
  45. def test_is_available_to_same_user_multiple_times(self):
  46. user, order = G(User), G(Order)
  47. for i in xrange(10):
  48. self.voucher.record_usage(order, user)
  49. self.assertTrue(self.voucher.is_available_to_user(user)[0])
  50. class TestOncePerCustomerVoucher(TestCase):
  51. def setUp(self):
  52. self.voucher = G(Voucher, usage=Voucher.ONCE_PER_CUSTOMER)
  53. def test_is_available_to_a_user_once(self):
  54. user, order = G(User), G(Order)
  55. self.assertTrue(self.voucher.is_available_to_user(user)[0])
  56. self.voucher.record_usage(order, user)
  57. self.assertFalse(self.voucher.is_available_to_user(user)[0])
  58. def test_is_available_to_different_users(self):
  59. users, order = [G(User), G(User)], G(Order)
  60. for user in users:
  61. self.assertTrue(self.voucher.is_available_to_user(user)[0])
  62. self.voucher.record_usage(order, user)
  63. self.assertFalse(self.voucher.is_available_to_user(user)[0])