Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

absolute_benefit_tests.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from django_dynamic_fixture import G
  4. from oscar.apps.offer import models
  5. from oscar.apps.basket.models import Basket
  6. from oscar.test.helpers import create_product
  7. class TestAnAbsoluteDiscountAppliedWithCountCondition(TestCase):
  8. def setUp(self):
  9. range = models.Range.objects.create(
  10. name="All products", includes_all_products=True)
  11. self.condition = models.CountCondition.objects.create(
  12. range=range,
  13. type=models.Condition.COUNT,
  14. value=2)
  15. self.benefit = models.AbsoluteDiscountBenefit.objects.create(
  16. range=range,
  17. type=models.Benefit.FIXED,
  18. value=D('3.00'))
  19. self.basket = G(Basket)
  20. def test_applies_correctly_to_empty_basket(self):
  21. discount = self.benefit.apply(self.basket, self.condition)
  22. self.assertEqual(D('0.00'), discount)
  23. self.assertEqual(0, self.basket.num_items_with_discount)
  24. self.assertEqual(0, self.basket.num_items_without_discount)
  25. def test_applies_correctly_to_basket_which_matches_condition(self):
  26. for product in [create_product(price=D('12.00'))]:
  27. self.basket.add_product(product, 2)
  28. discount = self.benefit.apply(self.basket, self.condition)
  29. self.assertEqual(D('3.00'), discount)
  30. self.assertEqual(2, self.basket.num_items_with_discount)
  31. self.assertEqual(0, self.basket.num_items_without_discount)
  32. def test_applies_correctly_to_basket_which_exceeds_condition(self):
  33. for product in [create_product(price=D('12.00')),
  34. create_product(price=D('10.00'))]:
  35. self.basket.add_product(product, 2)
  36. discount = self.benefit.apply(self.basket, self.condition)
  37. self.assertEqual(D('3.00'), discount)
  38. self.assertEqual(2, self.basket.num_items_with_discount)
  39. self.assertEqual(2, self.basket.num_items_without_discount)
  40. def test_applies_correctly_to_basket_which_exceeds_condition_with_smaller_prices_than_discount(self):
  41. for product in [create_product(price=D('2.00')),
  42. create_product(price=D('4.00'))]:
  43. self.basket.add_product(product, 2)
  44. discount = self.benefit.apply(self.basket, self.condition)
  45. self.assertEqual(D('3.00'), discount)
  46. self.assertEqual(2, self.basket.num_items_with_discount)
  47. self.assertEqual(2, self.basket.num_items_without_discount)
  48. def test_applies_correctly_to_basket_which_exceeds_condition_with_smaller_prices_than_discount_and_higher_prices_first(self):
  49. for product in [create_product(price=D('4.00')),
  50. create_product(price=D('2.00'))]:
  51. self.basket.add_product(product, 2)
  52. discount = self.benefit.apply(self.basket, self.condition)
  53. self.assertEqual(D('3.00'), discount)
  54. self.assertEqual(2, self.basket.num_items_with_discount)
  55. self.assertEqual(2, self.basket.num_items_without_discount)
  56. class TestAnAbsoluteDiscountWithMaxItemsSetAppliedWithCountCondition(TestCase):
  57. def setUp(self):
  58. range = models.Range.objects.create(
  59. name="All products", includes_all_products=True)
  60. self.condition = models.CountCondition.objects.create(
  61. range=range,
  62. type=models.Condition.COUNT,
  63. value=2)
  64. self.benefit = models.AbsoluteDiscountBenefit.objects.create(
  65. range=range,
  66. type=models.Benefit.FIXED,
  67. value=D('3.00'),
  68. max_affected_items=1)
  69. self.basket = G(Basket)
  70. def test_applies_correctly_to_empty_basket(self):
  71. discount = self.benefit.apply(self.basket, self.condition)
  72. self.assertEqual(D('0.00'), discount)
  73. self.assertEqual(0, self.basket.num_items_with_discount)
  74. self.assertEqual(0, self.basket.num_items_without_discount)
  75. def test_applies_correctly_to_basket_which_matches_condition(self):
  76. for product in [create_product(price=D('12.00'))]:
  77. self.basket.add_product(product, 2)
  78. discount = self.benefit.apply(self.basket, self.condition)
  79. self.assertEqual(D('3.00'), discount)
  80. self.assertEqual(2, self.basket.num_items_with_discount)
  81. self.assertEqual(0, self.basket.num_items_without_discount)
  82. def test_applies_correctly_to_basket_which_exceeds_condition(self):
  83. for product in [create_product(price=D('12.00')),
  84. create_product(price=D('10.00'))]:
  85. self.basket.add_product(product, 2)
  86. discount = self.benefit.apply(self.basket, self.condition)
  87. self.assertEqual(D('3.00'), discount)
  88. self.assertEqual(2, self.basket.num_items_with_discount)
  89. self.assertEqual(2, self.basket.num_items_without_discount)
  90. def test_applies_correctly_to_basket_which_exceeds_condition_but_with_smaller_prices_than_discount(self):
  91. for product in [create_product(price=D('2.00')),
  92. create_product(price=D('1.00'))]:
  93. self.basket.add_product(product, 2)
  94. discount = self.benefit.apply(self.basket, self.condition)
  95. self.assertEqual(D('1.00'), discount)
  96. self.assertEqual(2, self.basket.num_items_with_discount)
  97. self.assertEqual(2, self.basket.num_items_without_discount)
  98. class TestAnAbsoluteDiscountAppliedWithValueCondition(TestCase):
  99. def setUp(self):
  100. range = models.Range.objects.create(
  101. name="All products", includes_all_products=True)
  102. self.condition = models.ValueCondition.objects.create(
  103. range=range,
  104. type=models.Condition.VALUE,
  105. value=D('10.00'))
  106. self.benefit = models.AbsoluteDiscountBenefit.objects.create(
  107. range=range,
  108. type=models.Benefit.FIXED,
  109. value=D('3.00'))
  110. self.basket = G(Basket)
  111. def test_applies_correctly_to_empty_basket(self):
  112. discount = self.benefit.apply(self.basket, self.condition)
  113. self.assertEqual(D('0.00'), discount)
  114. self.assertEqual(0, self.basket.num_items_with_discount)
  115. self.assertEqual(0, self.basket.num_items_without_discount)
  116. def test_applies_correctly_to_single_item_basket_which_matches_condition(self):
  117. for product in [create_product(price=D('10.00'))]:
  118. self.basket.add_product(product, 1)
  119. discount = self.benefit.apply(self.basket, self.condition)
  120. self.assertEqual(D('3.00'), discount)
  121. self.assertEqual(1, self.basket.num_items_with_discount)
  122. self.assertEqual(0, self.basket.num_items_without_discount)
  123. def test_applies_correctly_to_multi_item_basket_which_matches_condition(self):
  124. for product in [create_product(price=D('5.00'))]:
  125. self.basket.add_product(product, 2)
  126. discount = self.benefit.apply(self.basket, self.condition)
  127. self.assertEqual(D('3.00'), discount)
  128. self.assertEqual(2, self.basket.num_items_with_discount)
  129. self.assertEqual(0, self.basket.num_items_without_discount)
  130. def test_applies_correctly_to_multi_item_basket_which_exceeds_condition(self):
  131. for product in [create_product(price=D('4.00'))]:
  132. self.basket.add_product(product, 3)
  133. discount = self.benefit.apply(self.basket, self.condition)
  134. self.assertEqual(D('3.00'), discount)
  135. self.assertEqual(3, self.basket.num_items_with_discount)
  136. self.assertEqual(0, self.basket.num_items_without_discount)
  137. def test_applies_correctly_to_multi_item_basket_which_exceeds_condition_but_matches_boundary(self):
  138. for product in [create_product(price=D('5.00'))]:
  139. self.basket.add_product(product, 3)
  140. discount = self.benefit.apply(self.basket, self.condition)
  141. self.assertEqual(D('3.00'), discount)
  142. self.assertEqual(2, self.basket.num_items_with_discount)
  143. self.assertEqual(1, self.basket.num_items_without_discount)
  144. class TestAnAbsoluteDiscountWithMaxItemsSetAppliedWithValueCondition(TestCase):
  145. def setUp(self):
  146. range = models.Range.objects.create(
  147. name="All products", includes_all_products=True)
  148. self.condition = models.ValueCondition.objects.create(
  149. range=range,
  150. type=models.Condition.VALUE,
  151. value=D('10.00'))
  152. self.benefit = models.AbsoluteDiscountBenefit.objects.create(
  153. range=range,
  154. type=models.Benefit.FIXED,
  155. value=D('3.00'),
  156. max_affected_items=1)
  157. self.basket = G(Basket)
  158. def test_applies_correctly_to_empty_basket(self):
  159. discount = self.benefit.apply(self.basket, self.condition)
  160. self.assertEqual(D('0.00'), discount)
  161. self.assertEqual(0, self.basket.num_items_with_discount)
  162. self.assertEqual(0, self.basket.num_items_without_discount)
  163. def test_applies_correctly_to_single_item_basket_which_matches_condition(self):
  164. for product in [create_product(price=D('10.00'))]:
  165. self.basket.add_product(product, 1)
  166. discount = self.benefit.apply(self.basket, self.condition)
  167. self.assertEqual(D('3.00'), discount)
  168. self.assertEqual(1, self.basket.num_items_with_discount)
  169. self.assertEqual(0, self.basket.num_items_without_discount)
  170. def test_applies_correctly_to_multi_item_basket_which_matches_condition(self):
  171. for product in [create_product(price=D('5.00'))]:
  172. self.basket.add_product(product, 2)
  173. discount = self.benefit.apply(self.basket, self.condition)
  174. self.assertEqual(D('3.00'), discount)
  175. self.assertEqual(2, self.basket.num_items_with_discount)
  176. self.assertEqual(0, self.basket.num_items_without_discount)
  177. def test_applies_correctly_to_multi_item_basket_which_exceeds_condition(self):
  178. for product in [create_product(price=D('4.00'))]:
  179. self.basket.add_product(product, 3)
  180. discount = self.benefit.apply(self.basket, self.condition)
  181. self.assertEqual(D('3.00'), discount)
  182. self.assertEqual(3, self.basket.num_items_with_discount)
  183. self.assertEqual(0, self.basket.num_items_without_discount)
  184. def test_applies_correctly_to_multi_item_basket_which_exceeds_condition_but_matches_boundary(self):
  185. for product in [create_product(price=D('5.00'))]:
  186. self.basket.add_product(product, 3)
  187. discount = self.benefit.apply(self.basket, self.condition)
  188. self.assertEqual(D('3.00'), discount)
  189. self.assertEqual(2, self.basket.num_items_with_discount)
  190. self.assertEqual(1, self.basket.num_items_without_discount)
  191. def test_applies_correctly_to_multi_item_basket_which_matches_condition_but_with_lower_prices_than_discount(self):
  192. for product in [create_product(price=D('2.00'))]:
  193. self.basket.add_product(product, 6)
  194. discount = self.benefit.apply(self.basket, self.condition)
  195. self.assertEqual(D('2.00'), discount)
  196. self.assertEqual(5, self.basket.num_items_with_discount)
  197. self.assertEqual(1, self.basket.num_items_without_discount)