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.

availability_tests.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from decimal import Decimal as D
  2. import datetime
  3. from django.contrib.auth.models import User
  4. from django.test import TestCase
  5. from django_dynamic_fixture import G
  6. from oscar.apps.offer import models
  7. from oscar.apps.order.models import OrderDiscount
  8. from oscar_testsupport.factories import create_order
  9. class TestADateBasedConditionalOffer(TestCase):
  10. def setUp(self):
  11. self.start = datetime.date(2011, 01, 01)
  12. self.end = datetime.date(2011, 02, 01)
  13. self.offer = models.ConditionalOffer(start_date=self.start,
  14. end_date=self.end)
  15. def test_is_available_during_date_range(self):
  16. test = datetime.date(2011, 01, 10)
  17. self.assertTrue(self.offer.is_available(test_date=test))
  18. def test_is_inactive_before_date_range(self):
  19. test = datetime.date(2010, 03, 10)
  20. self.assertFalse(self.offer.is_available(test_date=test))
  21. def test_is_inactive_after_date_range(self):
  22. test = datetime.date(2011, 03, 10)
  23. self.assertFalse(self.offer.is_available(test_date=test))
  24. def test_is_active_on_end_date(self):
  25. self.assertTrue(self.offer.is_available(test_date=self.end))
  26. class TestAConsumptionFrequencyBasedConditionalOffer(TestCase):
  27. def setUp(self):
  28. self.offer = models.ConditionalOffer(max_global_applications=4)
  29. def test_is_available_with_no_applications(self):
  30. self.assertTrue(self.offer.is_available())
  31. def test_is_available_with_fewer_applications_than_max(self):
  32. self.offer.num_applications = 3
  33. self.assertTrue(self.offer.is_available())
  34. def test_is_inactive_with_equal_applications_to_max(self):
  35. self.offer.num_applications = 4
  36. self.assertFalse(self.offer.is_available())
  37. def test_is_inactive_with_more_applications_than_max(self):
  38. self.offer.num_applications = 4
  39. self.assertFalse(self.offer.is_available())
  40. def test_restricts_number_of_applications_correctly_with_no_applications(self):
  41. self.assertEqual(4, self.offer.get_max_applications())
  42. def test_restricts_number_of_applications_correctly_with_fewer_applications_than_max(self):
  43. self.offer.num_applications = 3
  44. self.assertEqual(1, self.offer.get_max_applications())
  45. def test_restricts_number_of_applications_correctly_with_more_applications_than_max(self):
  46. self.offer.num_applications = 5
  47. self.assertEqual(0, self.offer.get_max_applications())
  48. class TestAPerUserConditionalOffer(TestCase):
  49. def setUp(self):
  50. self.offer = models.ConditionalOffer(max_user_applications=1)
  51. self.user = G(User)
  52. def test_is_available_with_no_applications(self):
  53. self.assertTrue(self.offer.is_available())
  54. def test_max_applications_is_correct_when_no_applications(self):
  55. self.assertEqual(1, self.offer.get_max_applications(self.user))
  56. def test_max_applications_is_correct_when_equal_applications(self):
  57. order = create_order(user=self.user)
  58. G(OrderDiscount, order=order, offer_id=self.offer.id, frequency=1)
  59. self.assertEqual(0, self.offer.get_max_applications(self.user))
  60. def test_max_applications_is_correct_when_more_applications(self):
  61. order = create_order(user=self.user)
  62. G(OrderDiscount, order=order, offer_id=self.offer.id, frequency=5)
  63. self.assertEqual(0, self.offer.get_max_applications(self.user))
  64. class TestCappedDiscountConditionalOffer(TestCase):
  65. def setUp(self):
  66. self.offer = models.ConditionalOffer(
  67. max_discount=D('100.00'),
  68. total_discount=D('0.00'))
  69. def test_is_available_when_below_threshold(self):
  70. self.assertTrue(self.offer.is_available())
  71. def test_is_inactive_when_on_threshold(self):
  72. self.offer.total_discount = self.offer.max_discount
  73. self.assertFalse(self.offer.is_available())
  74. def test_is_inactive_when_above_threshold(self):
  75. self.offer.total_discount = self.offer.max_discount + D('10.00')
  76. self.assertFalse(self.offer.is_available())
  77. class TestASuspendedOffer(TestCase):
  78. def setUp(self):
  79. self.offer = models.ConditionalOffer(
  80. status=models.ConditionalOffer.SUSPENDED)
  81. def test_is_unavailable(self):
  82. self.assertFalse(self.offer.is_available())
  83. def test_lists_suspension_as_an_availability_restriction(self):
  84. restrictions = self.offer.availability_restrictions()
  85. self.assertEqual(1, len(restrictions))