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

test_availability.py 4.4KB

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