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.

shipping_benefit_tests.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from oscar.apps.offer import models, utils
  4. from oscar.apps.shipping.repository import Repository
  5. from oscar.apps.shipping.methods import FixedPrice
  6. from oscar.test.basket import add_product
  7. from oscar.test import factories
  8. def create_offer():
  9. range = models.Range.objects.create(
  10. name="All products", includes_all_products=True)
  11. condition = models.CountCondition.objects.create(
  12. range=range,
  13. type=models.Condition.COUNT,
  14. value=1)
  15. benefit = models.ShippingFixedPriceBenefit.objects.create(
  16. type=models.Benefit.SHIPPING_FIXED_PRICE,
  17. value=D('1.00'))
  18. return models.ConditionalOffer.objects.create(
  19. condition=condition,
  20. benefit=benefit,
  21. offer_type=models.ConditionalOffer.SITE)
  22. class StubRepository(Repository):
  23. """
  24. Stubbed shipped repository which overrides the get_shipping_methods method
  25. in order to use a non-free default shipping method. This allows the
  26. shipping discounts to be tested.
  27. """
  28. methods = [FixedPrice(D('10.00'), D('10.00'))]
  29. class TestAnOfferWithAShippingBenefit(TestCase):
  30. def setUp(self):
  31. self.basket = factories.create_basket(empty=True)
  32. create_offer()
  33. def test_applies_correctly_to_basket_which_matches_condition(self):
  34. add_product(self.basket, D('12.00'))
  35. utils.Applicator().apply(self.basket)
  36. self.assertEqual(1, len(self.basket.offer_applications))
  37. def test_applies_correctly_to_basket_which_exceeds_condition(self):
  38. add_product(self.basket, D('12.00'), 2)
  39. utils.Applicator().apply(self.basket)
  40. self.assertEqual(1, len(self.basket.offer_applications))
  41. def test_wraps_shipping_method_from_repository(self):
  42. add_product(self.basket, D('12.00'), 1)
  43. utils.Applicator().apply(self.basket)
  44. methods = StubRepository().get_shipping_methods(self.basket)
  45. method = methods[0]
  46. charge = method.calculate(self.basket)
  47. self.assertEqual(D('1.00'), charge.incl_tax)
  48. def test_has_discount_recorded_correctly_when_order_is_placed(self):
  49. add_product(self.basket, D('12.00'), 1)
  50. utils.Applicator().apply(self.basket)
  51. methods = StubRepository().get_shipping_methods(self.basket)
  52. method = methods[0]
  53. order = factories.create_order(basket=self.basket,
  54. shipping_method=method)
  55. discounts = order.discounts.all()
  56. self.assertEqual(1, len(discounts))
  57. discount = discounts[0]
  58. self.assertTrue(discount.is_shipping_discount)
  59. self.assertEqual(D('9.00'), discount.amount)