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.

condition_tests.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from django.test import TestCase
  2. from django.utils import six
  3. from oscar.apps.offer import custom, models
  4. from oscar.apps.basket.models import Basket
  5. from oscar.test import factories
  6. class TestConditionProxyModels(TestCase):
  7. def test_name_and_description(self):
  8. """
  9. Tests that the condition proxy classes all return a name and
  10. description. Unfortunately, the current implementations means
  11. a valid range and value are required.
  12. This test became necessary because the complex name/description logic
  13. broke with the python_2_unicode_compatible decorator.
  14. """
  15. range = factories.RangeFactory()
  16. for type, __ in models.Condition.TYPE_CHOICES:
  17. condition = models.Condition(type=type, range=range, value=5)
  18. self.assertTrue(all([
  19. condition.name,
  20. condition.description,
  21. six.text_type(condition)]))
  22. class BasketOwnerCalledBarry(models.Condition):
  23. class Meta:
  24. proxy = True
  25. app_label = 'tests'
  26. def is_satisfied(self, offer, basket):
  27. if not basket.owner:
  28. return False
  29. return basket.owner.first_name.lower() == 'barry'
  30. def can_apply_condition(self, product):
  31. return False
  32. class TestCustomCondition(TestCase):
  33. def setUp(self):
  34. self.condition = custom.create_condition(BasketOwnerCalledBarry)
  35. self.offer = models.ConditionalOffer(condition=self.condition)
  36. self.basket = Basket()
  37. def test_is_not_satified_by_non_match(self):
  38. self.basket.owner = factories.UserFactory(first_name="Alan")
  39. self.assertFalse(self.offer.is_condition_satisfied(self.basket))
  40. def test_is_satified_by_match(self):
  41. self.basket.owner = factories.UserFactory(first_name="Barry")
  42. self.assertTrue(self.offer.is_condition_satisfied(self.basket))