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.

tests.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from django.test import TestCase
  2. from django.core.exceptions import ValidationError
  3. from django.conf import settings
  4. from oscar.core.loading import import_module, AppNotFoundError, \
  5. get_classes, get_class
  6. from oscar.core.validators import ExtendedURLValidator
  7. from oscar.test import patch_settings
  8. class ImportAppTests(TestCase):
  9. def test_a_specified_class_is_imported_correctly(self):
  10. module = import_module('analytics.models', ['ProductRecord'])
  11. self.assertEqual('oscar.apps.analytics.models', module.__name__)
  12. def test_unknown_apps_raise_exception(self):
  13. self.assertRaises(AppNotFoundError, import_module, 'banana', ['skin'])
  14. class ClassLoadingTests(TestCase):
  15. def test_loading_oscar_classes(self):
  16. Product, Category = get_classes('catalogue.models', ('Product', 'Category'))
  17. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  18. self.assertEqual('oscar.apps.catalogue.models', Category.__module__)
  19. def test_loading_oscar_class(self):
  20. Product = get_class('catalogue.models', 'Product')
  21. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  22. def test_loading_oscar_class_from_dashboard_subapp(self):
  23. ReportForm = get_class('dashboard.reports.forms', 'ReportForm')
  24. self.assertEqual('oscar.apps.dashboard.reports.forms', ReportForm.__module__)
  25. def test_bad_appname_raises_exception(self):
  26. with self.assertRaises(AppNotFoundError):
  27. Product, Category = get_classes('fridge.models', ('Product', 'Category'))
  28. class ClassLoadingWithLocalOverrideTests(TestCase):
  29. def setUp(self):
  30. self.installed_apps = list(settings.INSTALLED_APPS)
  31. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests.shipping'
  32. def test_loading_class_defined_in_local_module(self):
  33. with patch_settings(INSTALLED_APPS=self.installed_apps):
  34. (Free,) = get_classes('shipping.methods', ('Free',))
  35. self.assertEqual('tests.shipping.methods', Free.__module__)
  36. def test_loading_class_which_is_not_defined_in_local_module(self):
  37. with patch_settings(INSTALLED_APPS=self.installed_apps):
  38. (FixedPrice,) = get_classes('shipping.methods', ('FixedPrice',))
  39. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  40. def test_loading_class_from_module_not_defined_in_local_app(self):
  41. with patch_settings(INSTALLED_APPS=self.installed_apps):
  42. (Repository,) = get_classes('shipping.repository', ('Repository',))
  43. self.assertEqual('oscar.apps.shipping.repository', Repository.__module__)
  44. def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
  45. with patch_settings(INSTALLED_APPS=self.installed_apps):
  46. (Free, FixedPrice) = get_classes('shipping.methods', ('Free', 'FixedPrice'))
  47. self.assertEqual('tests.shipping.methods', Free.__module__)
  48. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  49. class ValidatorTests(TestCase):
  50. def test_validate_local_url(self):
  51. v = ExtendedURLValidator(verify_exists=True)
  52. try:
  53. v('/')
  54. except ValidationError:
  55. self.fail('ExtendedURLValidator raised ValidationError unexpectedly!')
  56. try:
  57. v('/?q=test') # Query strings shouldn't affect validation
  58. except ValidationError:
  59. self.fail('ExtendedURLValidator raised ValidationError unexpectedly!')
  60. with self.assertRaises(ValidationError):
  61. v('/invalid/')
  62. with self.assertRaises(ValidationError):
  63. v('/invalid/?q=test') # Query strings shouldn't affect validation
  64. try:
  65. v('products/')
  66. except ValidationError:
  67. self.fail('ExtendedURLValidator raised ValidationError unexpectedly!')
  68. with self.assertRaises(ValidationError):
  69. v('/products') # Missing the / is bad