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_custom_range.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from django.core.exceptions import ValidationError
  2. from django.test import TestCase
  3. from django.utils.translation import gettext_lazy as _
  4. from oscar.apps.offer import custom
  5. from oscar.test.factories import create_product
  6. class CustomRange(object):
  7. name = "Custom range"
  8. def contains_product(self, product):
  9. return product.title.startswith("A")
  10. def num_products(self):
  11. return None
  12. class CustomRangeLazy(object):
  13. name = _("Custom range with gettext_lazy")
  14. def contains_product(self, product):
  15. return product.title.startswith("B")
  16. def num_products(self):
  17. return None
  18. class TestACustomRange(TestCase):
  19. def test_creating_duplicate_range_fails(self):
  20. custom.create_range(CustomRange)
  21. self.assertRaises(ValueError, custom.create_range, CustomRange)
  22. def test_must_have_a_text_name(self):
  23. try:
  24. custom.create_range(CustomRangeLazy)
  25. except ValidationError:
  26. pass
  27. else:
  28. self.fail("Range can't have gettext titles")
  29. def test_correctly_includes_match(self):
  30. rng = custom.create_range(CustomRange)
  31. test_product = create_product(title="A tale")
  32. self.assertTrue(rng.contains_product(test_product))
  33. def test_correctly_excludes_nonmatch(self):
  34. rng = custom.create_range(CustomRange)
  35. test_product = create_product(title="B tale")
  36. self.assertFalse(rng.contains_product(test_product))