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

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