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.

test_offer_forms.py 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. from decimal import Decimal as D
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from oscar.apps.dashboard.offers import forms
  5. from oscar.apps.offer.custom import create_benefit, create_condition
  6. from oscar.apps.offer.models import Benefit, Range
  7. from oscar.test.factories import create_product
  8. from tests._site.model_tests_app.models import CustomBenefitModel, CustomConditionModel
  9. class TestBenefitForm(TestCase):
  10. def setUp(self):
  11. self.range = Range.objects.create(
  12. name="All products", includes_all_products=True
  13. )
  14. self.prod = create_product()
  15. def test_init_without_custom_benefit(self):
  16. """
  17. If no custom benefit exists, the type field should be required.
  18. """
  19. form = forms.BenefitForm()
  20. self.assertTrue(form.fields["type"].required)
  21. def test_init_with_custom_benefit(self):
  22. """
  23. If a custom benefit exists, the type field should not be required.
  24. """
  25. create_benefit(CustomBenefitModel)
  26. form = forms.BenefitForm()
  27. self.assertFalse(form.fields["type"].required)
  28. self.assertEqual(form.fields["custom_benefit"].initial, None)
  29. def test_init_with_custom_benefit_with_instance(self):
  30. """
  31. If a custom benefit exists and the kwargs instance is passed to init, the initial value for the custom_benefit
  32. should be the instance.
  33. """
  34. benefit = create_benefit(CustomBenefitModel)
  35. form = forms.BenefitForm(instance=benefit)
  36. self.assertFalse(form.fields["type"].required)
  37. self.assertEqual(form.fields["custom_benefit"].initial, benefit.id)
  38. def test_is_valid_no_data(self):
  39. """
  40. If not data is supplied, is_valid should evaluate to false
  41. """
  42. form = forms.BenefitForm()
  43. self.assertFalse(form.is_valid())
  44. def test_clean_no_value_data(self):
  45. """
  46. If data is supplied without any values, the form should evaluate to not valid +
  47. and the clean method should throw a ValidationError
  48. """
  49. form = forms.BenefitForm(
  50. data={"range": "", "type": "", "value": "", "custom_benefit": ""}
  51. )
  52. self.assertFalse(form.is_valid())
  53. self.assertRaises(ValidationError, form.clean)
  54. def test_clean_new_incentive(self):
  55. """
  56. If a range, type and value is supplied, the clean method should return the cleaned data without errors.
  57. """
  58. form = forms.BenefitForm(
  59. data={
  60. "range": self.range.id,
  61. "type": Benefit.FIXED,
  62. "value": 5,
  63. "custom_benefit": "",
  64. }
  65. )
  66. self.assertTrue(form.is_valid())
  67. self.assertEqual(
  68. {
  69. "range": self.range,
  70. "type": Benefit.FIXED,
  71. "value": D("5"),
  72. "custom_benefit": "",
  73. "max_affected_items": None,
  74. },
  75. form.clean(),
  76. )
  77. def test_clean_new_incentive_only_range(self):
  78. """
  79. If only a range is supplied, the clean method should throw a ValidationError.
  80. """
  81. form = forms.BenefitForm(
  82. data={"range": self.range.id, "type": "", "value": "", "custom_benefit": ""}
  83. )
  84. self.assertFalse(form.is_valid())
  85. self.assertRaises(ValidationError, form.clean)
  86. def test_clean_validation_with_custom_benefit(self):
  87. """
  88. If a custom benefit is selected, the form should be valid.
  89. """
  90. benefit = create_benefit(CustomBenefitModel)
  91. form = forms.BenefitForm(
  92. data={"range": "", "type": "", "value": "", "custom_benefit": benefit.id}
  93. )
  94. self.assertTrue(form.is_valid())
  95. self.assertEqual(
  96. {
  97. "range": None,
  98. "type": "",
  99. "value": None,
  100. "custom_benefit": str(benefit.id),
  101. "max_affected_items": None,
  102. },
  103. form.clean(),
  104. )
  105. def test_clean_only_range_custom_exists(self):
  106. """
  107. If a custom benefit exists, the type field is not required. Still, the clean method should throw a
  108. ValidationError, if only the range is supplied.
  109. """
  110. create_benefit(CustomBenefitModel)
  111. form = forms.BenefitForm(
  112. data={"range": self.range, "type": "", "value": "", "custom_benefit": ""}
  113. )
  114. self.assertFalse(form.is_valid())
  115. self.assertRaises(ValidationError, form.clean)
  116. def test_clean_validation_custom_exists(self):
  117. """
  118. If a custom benefit exists, and the data for range, type and value is supplied, the form should validate.
  119. Clean should return the cleaned data.
  120. """
  121. create_benefit(CustomBenefitModel)
  122. form = forms.BenefitForm(
  123. data={
  124. "range": self.range.id,
  125. "type": Benefit.FIXED,
  126. "value": 5,
  127. "custom_benefit": "",
  128. }
  129. )
  130. self.assertTrue(form.is_valid())
  131. self.assertEqual(
  132. {
  133. "range": self.range,
  134. "type": Benefit.FIXED,
  135. "value": D("5"),
  136. "custom_benefit": "",
  137. "max_affected_items": None,
  138. },
  139. form.clean(),
  140. )
  141. class TestConditionForm(TestCase):
  142. def setUp(self):
  143. self.range = Range.objects.create(
  144. name="All products", includes_all_products=True
  145. )
  146. def test_clean_all_data(self):
  147. """
  148. If a custom condition exists, and the data for range, type, value is supplied,
  149. the form should be valid.
  150. """
  151. create_condition(CustomConditionModel)
  152. form = forms.ConditionForm(
  153. data={
  154. "range": self.range.id,
  155. "type": "Count",
  156. "value": 1,
  157. "custom_condition": "",
  158. }
  159. )
  160. self.assertTrue(form.is_valid())
  161. def test_clean_no_value(self):
  162. """
  163. If a custom condition exists, and the data for one of range, type, value is supplied,
  164. the form should be invalid.
  165. """
  166. create_condition(CustomConditionModel)
  167. test_data = [("range", self.range), ("type", "Count"), ("value", 1)]
  168. for field, value in test_data:
  169. data = {"range": "", "type": "", "value": "", "custom_condition": ""}
  170. data[field] = value
  171. form = forms.ConditionForm(data=data)
  172. self.assertFalse(form.is_valid())
  173. def test_clean_custom_condition(self):
  174. """
  175. If a custom condition is selected, the form should be valid.
  176. """
  177. custom_condition = create_condition(CustomConditionModel)
  178. form = forms.ConditionForm(
  179. data={
  180. "range": "",
  181. "type": "",
  182. "value": "",
  183. "custom_condition": custom_condition.id,
  184. }
  185. )
  186. self.assertTrue(form.is_valid())
  187. self.assertEqual(
  188. {
  189. "range": None,
  190. "type": "",
  191. "value": None,
  192. "custom_condition": str(custom_condition.id),
  193. },
  194. form.clean(),
  195. )
  196. def test_clean_custom_condition_with_range_type_and_value(self):
  197. """
  198. If a custom condition is selected, but a range, type and value is selected as well,
  199. it should throw a ValidationError as you may only have a custom condition.
  200. """
  201. custom_condition = create_condition(CustomConditionModel)
  202. form = forms.ConditionForm(
  203. data={
  204. "range": self.range.id,
  205. "type": "Count",
  206. "value": "5",
  207. "custom_condition": custom_condition.id,
  208. }
  209. )
  210. self.assertFalse(form.is_valid())
  211. self.assertRaises(ValidationError, form.clean)