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.

offer_tests.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import datetime
  2. from django.contrib.auth.models import User
  3. from django.test import TestCase
  4. from django_dynamic_fixture import G
  5. from oscar.apps.offer import models
  6. from oscar.apps.order.models import OrderDiscount
  7. from oscar_testsupport.factories import create_order
  8. class TestADateBasedConditionalOffer(TestCase):
  9. def setUp(self):
  10. self.start = datetime.date(2011, 01, 01)
  11. self.end = datetime.date(2011, 02, 01)
  12. self.offer = models.ConditionalOffer(start_date=self.start,
  13. end_date=self.end)
  14. def test_is_active_during_date_range(self):
  15. test = datetime.date(2011, 01, 10)
  16. self.assertTrue(self.offer.is_active(test))
  17. def test_is_inactive_before_date_range(self):
  18. test = datetime.date(2010, 03, 10)
  19. self.assertFalse(self.offer.is_active(test))
  20. def test_is_inactive_after_date_range(self):
  21. test = datetime.date(2011, 03, 10)
  22. self.assertFalse(self.offer.is_active(test))
  23. def test_is_inactive_on_end_date(self):
  24. self.assertFalse(self.offer.is_active(self.end))
  25. class TestAConsumptionFrequencyBasedConditionalOffer(TestCase):
  26. def setUp(self):
  27. self.offer = models.ConditionalOffer(max_global_applications=4)
  28. def test_is_active_with_no_applications(self):
  29. self.assertTrue(self.offer.is_active())
  30. def test_is_active_with_fewer_applications_than_max(self):
  31. self.offer.num_applications = 3
  32. self.assertTrue(self.offer.is_active())
  33. def test_is_inactive_with_equal_applications_to_max(self):
  34. self.offer.num_applications = 4
  35. self.assertFalse(self.offer.is_active())
  36. def test_is_inactive_with_more_applications_than_max(self):
  37. self.offer.num_applications = 4
  38. self.assertFalse(self.offer.is_active())
  39. def test_restricts_number_of_applications_correctly_with_no_applications(self):
  40. self.assertEqual(4, self.offer.get_max_applications())
  41. def test_restricts_number_of_applications_correctly_with_fewer_applications_than_max(self):
  42. self.offer.num_applications = 3
  43. self.assertEqual(1, self.offer.get_max_applications())
  44. def test_restricts_number_of_applications_correctly_with_more_applications_than_max(self):
  45. self.offer.num_applications = 5
  46. self.assertEqual(0, self.offer.get_max_applications())
  47. class TestAPerUserConditionalOffer(TestCase):
  48. def setUp(self):
  49. self.offer = models.ConditionalOffer(max_user_applications=1)
  50. self.user = G(User)
  51. def test_is_active_with_no_applications(self):
  52. self.assertTrue(self.offer.is_active())
  53. def test_max_applications_is_correct_when_no_applications(self):
  54. self.assertEqual(1, self.offer.get_max_applications(self.user))
  55. def test_max_applications_is_correct_when_equal_applications(self):
  56. order = create_order(user=self.user)
  57. G(OrderDiscount, order=order, offer_id=self.offer.id, frequency=1)
  58. self.assertEqual(0, self.offer.get_max_applications(self.user))
  59. def test_max_applications_is_correct_when_more_applications(self):
  60. order = create_order(user=self.user)
  61. G(OrderDiscount, order=order, offer_id=self.offer.id, frequency=5)
  62. self.assertEqual(0, self.offer.get_max_applications(self.user))