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.

post_order_action_tests.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from django.test.client import RequestFactory
  4. from django_dynamic_fixture import G
  5. import mock
  6. from oscar.apps.offer import models, utils, custom
  7. from oscar.apps.basket.models import Basket
  8. from oscar.apps.order.utils import OrderCreator
  9. from oscar.apps.shipping.repository import Repository
  10. from oscar.apps.shipping.methods import FixedPrice
  11. from oscar_testsupport.factories import create_product
  12. class CustomAction(models.Benefit):
  13. class Meta:
  14. proxy = True
  15. def apply(self, basket, condition, offer=None):
  16. condition.consume_items(basket, ())
  17. return models.PostOrderAction(
  18. "Something will happen")
  19. def apply_deferred(self, basket):
  20. return "Something happened"
  21. @property
  22. def description(self):
  23. return "Will do something"
  24. def create_offer():
  25. range = models.Range.objects.create(
  26. name="All products", includes_all_products=True)
  27. condition = models.CountCondition.objects.create(
  28. range=range,
  29. type=models.Condition.COUNT,
  30. value=1)
  31. benefit = custom.create_benefit(CustomAction)
  32. return models.ConditionalOffer.objects.create(
  33. condition=condition,
  34. benefit=benefit,
  35. offer_type=models.ConditionalOffer.SITE)
  36. def apply_offers(basket):
  37. req = RequestFactory().get('/')
  38. req.user = mock.Mock()
  39. utils.Applicator().apply(req, basket)
  40. class TestAnOfferWithAPostOrderAction(TestCase):
  41. def setUp(self):
  42. self.basket = G(Basket)
  43. for product in [create_product(price=D('12.00'))]:
  44. self.basket.add_product(product, 1)
  45. create_offer()
  46. apply_offers(self.basket)
  47. def test_applies_correctly_to_basket_which_meets_condition(self):
  48. self.assertEqual(1, len(self.basket.offer_applications))
  49. self.assertEqual(
  50. 1, len(self.basket.offer_applications.post_order_actions))
  51. action = self.basket.offer_applications.post_order_actions[0]
  52. self.assertEqual('Something will happen', action['description'])
  53. def test_has_discount_recorded_correctly_when_order_is_placed(self):
  54. order = OrderCreator().place_order(self.basket)
  55. discounts = order.discounts.all()
  56. self.assertEqual(1, len(discounts))
  57. self.assertEqual(1, len(order.post_order_actions))
  58. discount = discounts[0]
  59. self.assertTrue(discount.is_post_order_action)
  60. self.assertEqual(D('0.00'), discount.amount)
  61. self.assertEqual('Something happened', discount.message)