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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from os.path import dirname
  2. import sys
  3. from django.test import TestCase
  4. from django.conf import settings
  5. from django.test.utils import override_settings
  6. import oscar
  7. from oscar.core.loading import (
  8. get_model, AppNotFoundError, get_classes, get_class, ClassNotFoundError)
  9. from tests import temporary_python_path
  10. class TestClassLoading(TestCase):
  11. """
  12. Oscar's class loading utilities
  13. """
  14. def test_load_oscar_classes_correctly(self):
  15. Product, Category = get_classes('catalogue.models', ('Product', 'Category'))
  16. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  17. self.assertEqual('oscar.apps.catalogue.models', Category.__module__)
  18. def test_load_oscar_class_correctly(self):
  19. Product = get_class('catalogue.models', 'Product')
  20. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  21. def test_load_oscar_class_from_dashboard_subapp(self):
  22. ReportForm = get_class('dashboard.reports.forms', 'ReportForm')
  23. self.assertEqual('oscar.apps.dashboard.reports.forms', ReportForm.__module__)
  24. def test_raise_exception_when_bad_appname_used(self):
  25. with self.assertRaises(AppNotFoundError):
  26. get_classes('fridge.models', ('Product', 'Category'))
  27. def test_raise_exception_when_bad_classname_used(self):
  28. with self.assertRaises(ClassNotFoundError):
  29. get_class('catalogue.models', 'Monkey')
  30. def test_raise_importerror_if_app_raises_importerror(self):
  31. """
  32. This tests that Oscar doesn't fall back to using the Oscar catalogue
  33. app if the overriding app throws an ImportError.
  34. """
  35. apps = list(settings.INSTALLED_APPS)
  36. apps[apps.index('oscar.apps.catalogue')] = 'tests._site.import_error_app.catalogue'
  37. with override_settings(INSTALLED_APPS=apps):
  38. with self.assertRaises(ImportError):
  39. get_class('catalogue.app', 'CatalogueApplication')
  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 override_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 override_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 override_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 override_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. def test_loading_classes_with_root_app(self):
  62. import tests._site.shipping
  63. path = dirname(dirname(tests._site.shipping.__file__))
  64. with temporary_python_path([path]):
  65. self.installed_apps[
  66. self.installed_apps.index('tests._site.shipping')] = 'shipping'
  67. with override_settings(INSTALLED_APPS=self.installed_apps):
  68. (Free,) = get_classes('shipping.methods', ('Free',))
  69. self.assertEqual('shipping.methods', Free.__module__)
  70. def test_overriding_view_is_possible_without_overriding_app(self):
  71. from oscar.apps.customer.app import application, CustomerApplication
  72. # If test fails, it's helpful to know if it's caused by order of
  73. # execution
  74. self.assertEqual(CustomerApplication().summary_view.__module__,
  75. 'tests._site.apps.customer.views')
  76. self.assertEqual(application.summary_view.__module__,
  77. 'tests._site.apps.customer.views')
  78. class ClassLoadingWithLocalOverrideWithMultipleSegmentsTests(TestCase):
  79. def setUp(self):
  80. self.installed_apps = list(settings.INSTALLED_APPS)
  81. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.apps.shipping'
  82. def test_loading_class_defined_in_local_module(self):
  83. with override_settings(INSTALLED_APPS=self.installed_apps):
  84. (Free,) = get_classes('shipping.methods', ('Free',))
  85. self.assertEqual('tests._site.apps.shipping.methods', Free.__module__)
  86. class TestGetCoreAppsFunction(TestCase):
  87. """
  88. oscar.get_core_apps function
  89. """
  90. def test_returns_core_apps_when_no_overrides_specified(self):
  91. apps = oscar.get_core_apps()
  92. self.assertEqual(oscar.OSCAR_CORE_APPS, apps)
  93. def test_uses_non_dashboard_override_when_specified(self):
  94. apps = oscar.get_core_apps(overrides=['apps.shipping'])
  95. self.assertTrue('apps.shipping' in apps)
  96. self.assertTrue('oscar.apps.shipping' not in apps)
  97. def test_uses_dashboard_override_when_specified(self):
  98. apps = oscar.get_core_apps(overrides=['apps.dashboard.catalogue'])
  99. self.assertTrue('apps.dashboard.catalogue' in apps)
  100. self.assertTrue('oscar.apps.dashboard.catalogue' not in apps)
  101. self.assertTrue('oscar.apps.catalogue' in apps)
  102. class TestOverridingCoreApps(TestCase):
  103. def test_means_the_overriding_model_is_registered_first(self):
  104. klass = get_model('partner', 'StockRecord')
  105. self.assertEqual(
  106. 'tests._site.apps.partner.models', klass.__module__)
  107. class TestAppLabelsForModels(TestCase):
  108. def test_all_oscar_models_have_app_labels(self):
  109. from django.apps import apps
  110. models = apps.get_models()
  111. missing = []
  112. for model in models:
  113. # Ignore non-Oscar models
  114. if 'oscar' not in repr(model):
  115. continue
  116. # Don't know how to get the actual model's Meta class. But if
  117. # the parent doesn't have a Meta class, it's doesn't have an
  118. # base in Oscar anyway and is not intended to be overridden
  119. abstract_model = model.__base__
  120. meta_class = getattr(abstract_model, 'Meta', None)
  121. if meta_class is None:
  122. continue
  123. if not hasattr(meta_class, 'app_label'):
  124. missing.append(model)
  125. if missing:
  126. self.fail("Those models don't have an app_label set: %s" % missing)
  127. class TestDynamicLoadingOn3rdPartyApps(TestCase):
  128. core_app_prefix = 'thirdparty_package.apps'
  129. def setUp(self):
  130. self.installed_apps = list(settings.INSTALLED_APPS)
  131. sys.path.append('./tests/_site/')
  132. def tearDown(self):
  133. sys.path.remove('./tests/_site/')
  134. def test_load_core_3rd_party_class_correctly(self):
  135. self.installed_apps.append('thirdparty_package.apps.myapp')
  136. with override_settings(INSTALLED_APPS=self.installed_apps):
  137. Cow, Goat = get_classes('myapp.models', ('Cow', 'Goat'), self.core_app_prefix)
  138. self.assertEqual('thirdparty_package.apps.myapp.models', Cow.__module__)
  139. self.assertEqual('thirdparty_package.apps.myapp.models', Goat.__module__)
  140. def test_load_overriden_3rd_party_class_correctly(self):
  141. self.installed_apps.append('apps.myapp')
  142. with override_settings(INSTALLED_APPS=self.installed_apps):
  143. Cow, Goat = get_classes('myapp.models', ('Cow', 'Goat'), self.core_app_prefix)
  144. self.assertEqual('thirdparty_package.apps.myapp.models', Cow.__module__)
  145. self.assertEqual('apps.myapp.models', Goat.__module__)