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.

loading_tests.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. from os.path import dirname
  2. from django.test import TestCase
  3. from django.core.exceptions import ValidationError
  4. from django.conf import settings
  5. from django.contrib.flatpages.models import FlatPage
  6. from django.test.utils import override_settings
  7. import oscar
  8. from oscar.core.loading import (
  9. import_module, AppNotFoundError, get_classes,
  10. get_class, ClassNotFoundError)
  11. from oscar.core.validators import (
  12. ExtendedURLValidator, URLDoesNotExistValidator)
  13. from tests import temporary_python_path
  14. class TestImportModule(TestCase):
  15. """
  16. oscar.core.loading.import_module
  17. """
  18. def test_imports_a_class_correctly(self):
  19. module = import_module('analytics.models', ['ProductRecord'])
  20. self.assertEqual('oscar.apps.analytics.models', module.__name__)
  21. def test_raises_exception_for_unknown_app(self):
  22. self.assertRaises(AppNotFoundError, import_module, 'banana', ['skin'])
  23. class TestClassLoading(TestCase):
  24. """
  25. Oscar's class loading utilities
  26. """
  27. def test_load_oscar_classes_correctly(self):
  28. Product, Category = get_classes('catalogue.models', ('Product', 'Category'))
  29. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  30. self.assertEqual('oscar.apps.catalogue.models', Category.__module__)
  31. def test_load_oscar_class_correctly(self):
  32. Product = get_class('catalogue.models', 'Product')
  33. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  34. def test_load_oscar_class_from_dashboard_subapp(self):
  35. ReportForm = get_class('dashboard.reports.forms', 'ReportForm')
  36. self.assertEqual('oscar.apps.dashboard.reports.forms', ReportForm.__module__)
  37. def test_raise_exception_when_bad_appname_used(self):
  38. with self.assertRaises(AppNotFoundError):
  39. get_classes('fridge.models', ('Product', 'Category'))
  40. def test_raise_exception_when_bad_classname_used(self):
  41. with self.assertRaises(ClassNotFoundError):
  42. get_class('catalogue.models', 'Monkey')
  43. class ClassLoadingWithLocalOverrideTests(TestCase):
  44. def setUp(self):
  45. self.installed_apps = list(settings.INSTALLED_APPS)
  46. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.shipping'
  47. def test_loading_class_defined_in_local_module(self):
  48. with override_settings(INSTALLED_APPS=self.installed_apps):
  49. (Free,) = get_classes('shipping.methods', ('Free',))
  50. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  51. def test_loading_class_which_is_not_defined_in_local_module(self):
  52. with override_settings(INSTALLED_APPS=self.installed_apps):
  53. (FixedPrice,) = get_classes('shipping.methods', ('FixedPrice',))
  54. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  55. def test_loading_class_from_module_not_defined_in_local_app(self):
  56. with override_settings(INSTALLED_APPS=self.installed_apps):
  57. (Repository,) = get_classes('shipping.repository', ('Repository',))
  58. self.assertEqual('oscar.apps.shipping.repository', Repository.__module__)
  59. def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
  60. with override_settings(INSTALLED_APPS=self.installed_apps):
  61. (Free, FixedPrice) = get_classes('shipping.methods', ('Free', 'FixedPrice'))
  62. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  63. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  64. def test_loading_classes_with_root_app(self):
  65. import tests._site.shipping
  66. path = dirname(dirname(tests._site.shipping.__file__))
  67. with temporary_python_path([path]):
  68. self.installed_apps[
  69. self.installed_apps.index('tests._site.shipping')] = 'shipping'
  70. with override_settings(INSTALLED_APPS=self.installed_apps):
  71. (Free,) = get_classes('shipping.methods', ('Free',))
  72. self.assertEqual('shipping.methods', Free.__module__)
  73. class TestExtendedURLValidator(TestCase):
  74. """
  75. ExtendedURLValidator
  76. """
  77. def test_validates_local_url(self):
  78. v = ExtendedURLValidator(verify_exists=True)
  79. try:
  80. v('/')
  81. except ValidationError:
  82. self.fail('ExtendedURLValidator raised ValidationError'
  83. 'unexpectedly!')
  84. try:
  85. v('/?q=test') # Query strings shouldn't affect validation
  86. except ValidationError:
  87. self.fail('ExtendedURLValidator raised ValidationError'
  88. 'unexpectedly!')
  89. with self.assertRaises(ValidationError):
  90. v('/invalid/')
  91. with self.assertRaises(ValidationError):
  92. v('/invalid/?q=test') # Query strings shouldn't affect validation
  93. try:
  94. v('catalogue/')
  95. except ValidationError:
  96. self.fail('ExtendedURLValidator raised ValidationError'
  97. 'unexpectedly!')
  98. with self.assertRaises(ValidationError):
  99. v('/catalogue') # Missing the / is bad
  100. FlatPage(title='test page', url='/test/page/').save()
  101. try:
  102. v('/test/page/')
  103. except ValidationError:
  104. self.fail('ExtendedURLValidator raises ValidationError'
  105. 'unexpectedly!')
  106. def test_raises_exception_for_missing_url(self):
  107. validator = URLDoesNotExistValidator()
  108. self.assertRaises(ValidationError, validator, '/')
  109. try:
  110. validator('/invalid/')
  111. except ValidationError:
  112. self.fail('URLDoesNotExistValidator raised ValidationError'
  113. 'unexpectedly!')
  114. FlatPage(title='test page', url='/test/page/').save()
  115. self.assertRaises(ValidationError, validator, '/test/page/')
  116. class ClassLoadingWithLocalOverrideWithMultipleSegmentsTests(TestCase):
  117. def setUp(self):
  118. self.installed_apps = list(settings.INSTALLED_APPS)
  119. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.apps.shipping'
  120. def test_loading_class_defined_in_local_module(self):
  121. with override_settings(INSTALLED_APPS=self.installed_apps):
  122. (Free,) = get_classes('shipping.methods', ('Free',))
  123. self.assertEqual('tests._site.apps.shipping.methods', Free.__module__)
  124. class TestGetCoreAppsFunction(TestCase):
  125. """
  126. oscar.get_core_apps function
  127. """
  128. def test_returns_core_apps_when_no_overrides_specified(self):
  129. apps = oscar.get_core_apps()
  130. self.assertEqual(oscar.OSCAR_CORE_APPS, apps)
  131. def test_uses_non_dashboard_override_when_specified(self):
  132. apps = oscar.get_core_apps(overrides=['apps.shipping'])
  133. self.assertTrue('apps.shipping' in apps)
  134. self.assertTrue('oscar.apps.shipping' not in apps)
  135. def test_uses_dashboard_override_when_specified(self):
  136. apps = oscar.get_core_apps(overrides=['apps.dashboard.catalogue'])
  137. self.assertTrue('apps.dashboard.catalogue' in apps)
  138. self.assertTrue('oscar.apps.dashboard.catalogue' not in apps)
  139. self.assertTrue('oscar.apps.catalogue' in apps)