您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

custom_condition_tests.py 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django.test import TestCase
  2. from django_dynamic_fixture import G
  3. from oscar.apps.offer import custom, models
  4. from oscar.apps.basket.models import Basket
  5. from oscar.core.compat import get_user_model
  6. User = get_user_model()
  7. class BasketOwnerCalledBarry(models.Condition):
  8. class Meta:
  9. proxy = True
  10. def is_satisfied(self, basket):
  11. if not basket.owner:
  12. return False
  13. return basket.owner.first_name.lower() == 'barry'
  14. def can_apply_condition(self, product):
  15. return False
  16. class TestCustomCondition(TestCase):
  17. def setUp(self):
  18. self.condition = custom.create_condition(BasketOwnerCalledBarry)
  19. self.offer = models.ConditionalOffer(
  20. condition=self.condition)
  21. self.basket = Basket()
  22. def test_is_not_satified_by_non_match(self):
  23. self.basket.owner = G(User, first_name="Alan")
  24. self.assertFalse(self.offer.is_condition_satisfied(self.basket))
  25. def test_is_satified_by_match(self):
  26. self.basket.owner = G(User, first_name="Barry")
  27. self.assertTrue(self.offer.is_condition_satisfied(self.basket))