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.

core_tests.py 6.5KB

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