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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 (
  9. CustomBenefitModel, CustomConditionModel)
  10. class TestBenefitForm(TestCase):
  11. def setUp(self):
  12. self.range = Range.objects.create(
  13. name="All products", includes_all_products=True)
  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(data={
  50. 'range': '',
  51. 'type': '',
  52. 'value': '',
  53. 'custom_benefit': ''
  54. })
  55. self.assertFalse(form.is_valid())
  56. self.assertRaises(ValidationError, form.clean)
  57. def test_clean_new_incentive(self):
  58. """
  59. If a range, type and value is supplied, the clean method should return the cleaned data without errors.
  60. """
  61. form = forms.BenefitForm(data={
  62. 'range': self.range.id,
  63. 'type': Benefit.FIXED,
  64. 'value': 5,
  65. 'custom_benefit': ''
  66. })
  67. self.assertTrue(form.is_valid())
  68. self.assertEqual({
  69. 'range': self.range,
  70. 'type': Benefit.FIXED,
  71. 'value': D('5'),
  72. 'custom_benefit': '',
  73. 'max_affected_items': None
  74. }, form.clean())
  75. def test_clean_new_incentive_only_range(self):
  76. """
  77. If only a range is supplied, the clean method should throw a ValidationError.
  78. """
  79. form = forms.BenefitForm(data={
  80. 'range': self.range.id,
  81. 'type': '',
  82. 'value': '',
  83. 'custom_benefit': ''
  84. })
  85. self.assertFalse(form.is_valid())
  86. self.assertRaises(ValidationError, form.clean)
  87. def test_clean_validation_with_custom_benefit(self):
  88. """
  89. If a custom benefit is selected, the form should be valid.
  90. """
  91. benefit = create_benefit(CustomBenefitModel)
  92. form = forms.BenefitForm(data={
  93. 'range': '',
  94. 'type': '',
  95. 'value': '',
  96. 'custom_benefit': benefit.id
  97. })
  98. self.assertTrue(form.is_valid())
  99. self.assertEqual({
  100. 'range': None,
  101. 'type': '',
  102. 'value': None,
  103. 'custom_benefit': str(benefit.id),
  104. 'max_affected_items': None
  105. }, form.clean())
  106. def test_clean_only_range_custom_exists(self):
  107. """
  108. If a custom benefit exists, the type field is not required. Still, the clean method should throw a
  109. ValidationError, if only the range is supplied.
  110. """
  111. create_benefit(CustomBenefitModel)
  112. form = forms.BenefitForm(data={
  113. 'range': self.range,
  114. 'type': '',
  115. 'value': '',
  116. 'custom_benefit': ''
  117. })
  118. self.assertFalse(form.is_valid())
  119. self.assertRaises(ValidationError, form.clean)
  120. def test_clean_validation_custom_exists(self):
  121. """
  122. If a custom benefit exists, and the data for range, type and value is supplied, the form should validate.
  123. Clean should return the cleaned data.
  124. """
  125. create_benefit(CustomBenefitModel)
  126. form = forms.BenefitForm(data={
  127. 'range': self.range.id,
  128. 'type': Benefit.FIXED,
  129. 'value': 5,
  130. 'custom_benefit': ''
  131. })
  132. self.assertTrue(form.is_valid())
  133. self.assertEqual({
  134. 'range': self.range,
  135. 'type': Benefit.FIXED,
  136. 'value': D('5'),
  137. 'custom_benefit': '',
  138. 'max_affected_items': None
  139. }, form.clean())
  140. class TestConditionForm(TestCase):
  141. def setUp(self):
  142. self.range = Range.objects.create(
  143. name="All products", includes_all_products=True)
  144. def test_clean_all_data(self):
  145. """
  146. If a custom condition exists, and the data for range, type, value is supplied,
  147. the form should be valid.
  148. """
  149. create_condition(CustomConditionModel)
  150. form = forms.ConditionForm(data={
  151. 'range': self.range.id,
  152. 'type': 'Count',
  153. 'value': 1,
  154. 'custom_condition': ''
  155. })
  156. self.assertTrue(form.is_valid())
  157. def test_clean_no_value(self):
  158. """
  159. If a custom condition exists, and the data for one of range, type, value is supplied,
  160. the form should be invalid.
  161. """
  162. create_condition(CustomConditionModel)
  163. test_data = [('range', self.range), ('type', 'Count'), ('value', 1)]
  164. for field, value in test_data:
  165. data = {
  166. 'range': '',
  167. 'type': '',
  168. 'value': '',
  169. 'custom_condition': ''
  170. }
  171. data[field] = value
  172. form = forms.ConditionForm(data=data)
  173. self.assertFalse(form.is_valid())
  174. def test_clean_custom_condition(self):
  175. """
  176. If a custom condition is selected, the form should be valid.
  177. """
  178. custom_condition = create_condition(CustomConditionModel)
  179. form = forms.ConditionForm(data={
  180. 'range': '',
  181. 'type': '',
  182. 'value': '',
  183. 'custom_condition': custom_condition.id
  184. })
  185. self.assertTrue(form.is_valid())
  186. self.assertEqual({
  187. 'range': None,
  188. 'type': '',
  189. 'value': None,
  190. 'custom_condition': str(custom_condition.id)
  191. }, form.clean())
  192. def test_clean_custom_condition_with_range_type_and_value(self):
  193. """
  194. If a custom condition is selected, but a range, type and value is selected as well,
  195. it should throw a ValidationError as you may only have a custom condition.
  196. """
  197. custom_condition = create_condition(CustomConditionModel)
  198. form = forms.ConditionForm(data={
  199. 'range': self.range.id,
  200. 'type': 'Count',
  201. 'value': '5',
  202. 'custom_condition': custom_condition.id
  203. })
  204. self.assertFalse(form.is_valid())
  205. self.assertRaises(ValidationError, form.clean)