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.

model_tests.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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(
  20. code='lower', start_date=END_DATE, end_date=START_DATE)
  21. voucher.clean()
  22. class TestAVoucher(TestCase):
  23. def setUp(self):
  24. self.voucher = Voucher(start_date=START_DATE, end_date=END_DATE)
  25. def test_is_active_between_start_and_end_dates(self):
  26. test = datetime.date(2011, 06, 10)
  27. self.assertTrue(self.voucher.is_active(test))
  28. def test_is_active_on_end_date(self):
  29. self.assertTrue(self.voucher.is_active(END_DATE))
  30. def test_is_active_on_start_date(self):
  31. self.assertTrue(self.voucher.is_active(START_DATE))
  32. def test_is_inactive_outside_of_start_and_end_dates(self):
  33. test = datetime.date(2012, 03, 10)
  34. self.assertFalse(self.voucher.is_active(test))
  35. def test_increments_total_discount_when_recording_usage(self):
  36. voucher = G(Voucher)
  37. voucher.record_discount({'discount': D('10.00')})
  38. self.assertEqual(voucher.total_discount, D('10.00'))
  39. voucher.record_discount({'discount': D('10.00')})
  40. self.assertEqual(voucher.total_discount, D('20.00'))
  41. class TestMultiuseVoucher(TestCase):
  42. def setUp(self):
  43. self.voucher = G(Voucher, usage=Voucher.MULTI_USE)
  44. def test_is_available_to_same_user_multiple_times(self):
  45. user, order = G(User), G(Order)
  46. for i in xrange(10):
  47. self.voucher.record_usage(order, user)
  48. self.assertTrue(self.voucher.is_available_to_user(user)[0])
  49. class TestOncePerCustomerVoucher(TestCase):
  50. def setUp(self):
  51. self.voucher = G(Voucher, usage=Voucher.ONCE_PER_CUSTOMER)
  52. def test_is_available_to_a_user_once(self):
  53. user, order = G(User), G(Order)
  54. self.assertTrue(self.voucher.is_available_to_user(user)[0])
  55. self.voucher.record_usage(order, user)
  56. self.assertFalse(self.voucher.is_available_to_user(user)[0])
  57. def test_is_available_to_different_users(self):
  58. users, order = [G(User), G(User)], G(Order)
  59. for user in users:
  60. self.assertTrue(self.voucher.is_available_to_user(user)[0])
  61. self.voucher.record_usage(order, user)
  62. self.assertFalse(self.voucher.is_available_to_user(user)[0])