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_shipping_benefit.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from decimal import Decimal as D
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from oscar.apps.offer import models, utils
  5. from oscar.apps.shipping.repository import Repository
  6. from oscar.apps.shipping.methods import FixedPrice
  7. from oscar.test.basket import add_product
  8. from oscar.test import factories
  9. class StubRepository(Repository):
  10. """
  11. Stubbed shipped repository which overrides the get_shipping_methods method
  12. in order to use a non-free default shipping method. This allows the
  13. shipping discounts to be tested.
  14. """
  15. methods = [FixedPrice(D('10.00'), D('10.00'))]
  16. class TestAnOfferWithAShippingBenefit(TestCase):
  17. def setUp(self):
  18. self.basket = factories.create_basket(empty=True)
  19. self.range = models.Range.objects.create(
  20. name="All products", includes_all_products=True)
  21. self.condition = models.CountCondition.objects.create(
  22. range=self.range,
  23. type=models.Condition.COUNT,
  24. value=1)
  25. self.benefit = models.ShippingFixedPriceBenefit.objects.create(
  26. type=models.Benefit.SHIPPING_FIXED_PRICE,
  27. value=D('1.00'))
  28. self.offer = models.ConditionalOffer.objects.create(
  29. condition=self.condition,
  30. benefit=self.benefit,
  31. offer_type=models.ConditionalOffer.SITE)
  32. def test_applies_correctly_to_basket_which_matches_condition(self):
  33. add_product(self.basket, D('12.00'))
  34. utils.Applicator().apply(self.basket)
  35. self.assertEqual(1, len(self.basket.offer_applications))
  36. def test_applies_correctly_to_basket_which_exceeds_condition(self):
  37. add_product(self.basket, D('12.00'), 2)
  38. utils.Applicator().apply(self.basket)
  39. self.assertEqual(1, len(self.basket.offer_applications))
  40. def test_wraps_shipping_method_from_repository(self):
  41. add_product(self.basket, D('12.00'), 1)
  42. utils.Applicator().apply(self.basket)
  43. methods = StubRepository().get_shipping_methods(self.basket)
  44. method = methods[0]
  45. charge = method.calculate(self.basket)
  46. self.assertEqual(D('1.00'), charge.incl_tax)
  47. def test_has_discount_recorded_correctly_when_order_is_placed(self):
  48. add_product(self.basket, D('12.00'), 1)
  49. utils.Applicator().apply(self.basket)
  50. methods = StubRepository().get_shipping_methods(self.basket)
  51. method = methods[0]
  52. order = factories.create_order(basket=self.basket,
  53. shipping_method=method)
  54. discounts = order.discounts.all()
  55. self.assertEqual(1, len(discounts))
  56. discount = discounts[0]
  57. self.assertTrue(discount.is_shipping_discount)
  58. self.assertEqual(D('9.00'), discount.amount)
  59. def test_fixed_range_must_not_be_set(self):
  60. benefit = models.Benefit(
  61. type=models.Benefit.SHIPPING_FIXED_PRICE,
  62. value=10,
  63. range=self.range,
  64. )
  65. with self.assertRaises(ValidationError):
  66. benefit.clean()
  67. def test_fixed_max_affected_items_must_not_be_set(self):
  68. benefit = models.Benefit(
  69. type=models.Benefit.SHIPPING_FIXED_PRICE,
  70. value=10,
  71. max_affected_items=5,
  72. )
  73. with self.assertRaises(ValidationError):
  74. benefit.clean()
  75. def test_absolute_requires_value(self):
  76. benefit = models.Benefit(
  77. type=models.Benefit.SHIPPING_ABSOLUTE)
  78. with self.assertRaises(ValidationError):
  79. benefit.clean()
  80. def test_absolute_range_must_not_be_set(self):
  81. benefit = models.Benefit(
  82. type=models.Benefit.SHIPPING_ABSOLUTE,
  83. value=10,
  84. range=self.range,
  85. )
  86. with self.assertRaises(ValidationError):
  87. benefit.clean()
  88. def test_absolute_max_affected_items_must_not_be_set(self):
  89. benefit = models.Benefit(
  90. type=models.Benefit.SHIPPING_ABSOLUTE,
  91. value=10,
  92. max_affected_items=5,
  93. )
  94. with self.assertRaises(ValidationError):
  95. benefit.clean()