Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

availability_tests.py 4.4KB

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