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_post_order_action.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from oscar.apps.offer import custom, models, utils
  4. from oscar.test import factories
  5. from oscar.test.basket import add_product
  6. class CustomAction(models.Benefit):
  7. class Meta:
  8. proxy = True
  9. app_label = 'tests'
  10. def apply(self, basket, condition, offer):
  11. condition.consume_items(offer, basket, ())
  12. return models.PostOrderAction(
  13. "Something will happen")
  14. def apply_deferred(self, basket, order, application):
  15. return "Something happened"
  16. @property
  17. def description(self):
  18. return "Will do something"
  19. def create_offer():
  20. range = models.Range.objects.create(
  21. name="All products", includes_all_products=True)
  22. condition = models.CountCondition.objects.create(
  23. range=range,
  24. type=models.Condition.COUNT,
  25. value=1)
  26. benefit = custom.create_benefit(CustomAction)
  27. return models.ConditionalOffer.objects.create(
  28. condition=condition,
  29. benefit=benefit,
  30. offer_type=models.ConditionalOffer.SITE)
  31. class TestAnOfferWithAPostOrderAction(TestCase):
  32. def setUp(self):
  33. self.basket = factories.create_basket(empty=True)
  34. add_product(self.basket, D('12.00'), 1)
  35. create_offer()
  36. utils.Applicator().apply(self.basket)
  37. def test_applies_correctly_to_basket_which_meets_condition(self):
  38. self.assertEqual(1, len(self.basket.offer_applications))
  39. self.assertEqual(
  40. 1, len(self.basket.offer_applications.post_order_actions))
  41. action = self.basket.offer_applications.post_order_actions[0]
  42. self.assertEqual('Something will happen', action['description'])
  43. def test_has_discount_recorded_correctly_when_order_is_placed(self):
  44. order = factories.create_order(basket=self.basket)
  45. discounts = order.discounts.all()
  46. self.assertEqual(1, len(discounts))
  47. self.assertEqual(1, len(order.post_order_actions))
  48. discount = discounts[0]
  49. self.assertTrue(discount.is_post_order_action)
  50. self.assertEqual(D('0.00'), discount.amount)
  51. self.assertEqual('Something happened', discount.message)