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.

custom_benefit_tests.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from django.contrib.auth.models import User
  4. from django_dynamic_fixture import G
  5. import mock
  6. from oscar.apps.offer import custom, models
  7. from oscar.apps.basket.models import Basket
  8. class ChangesOwnerName(models.Benefit):
  9. class Meta:
  10. proxy = True
  11. def apply(self, basket, condition, offer=None):
  12. basket.owner.first_name = "Terry"
  13. basket.owner.save()
  14. return D('0.00')
  15. @property
  16. def description(self):
  17. return 'Changes owners name'
  18. class NoDescription(models.Benefit):
  19. class Meta:
  20. proxy = True
  21. def apply(self, basket, condition, offer=None):
  22. return D('0.00')
  23. class TestACustomBenefit(TestCase):
  24. def test_must_implement_a_description_property(self):
  25. with self.assertRaises(RuntimeError):
  26. custom.create_benefit(NoDescription)
  27. class TestCustomBenefit(TestCase):
  28. def setUp(self):
  29. self.benefit = custom.create_benefit(ChangesOwnerName)
  30. self.condition = mock.Mock()
  31. self.basket = Basket()
  32. def test_applies_correctly(self):
  33. self.basket.owner = G(User, first_name="Alan")
  34. self.benefit.proxy().apply(self.basket, self.condition)
  35. self.assertEqual("Terry", self.basket.owner.first_name)