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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from os.path import dirname
  2. from django.test import TestCase
  3. from django.conf import settings
  4. from django.test.utils import override_settings
  5. import oscar
  6. from oscar.core.loading import (
  7. get_model, AppNotFoundError, get_classes, get_class, ClassNotFoundError)
  8. from tests import temporary_python_path
  9. class TestClassLoading(TestCase):
  10. """
  11. Oscar's class loading utilities
  12. """
  13. def test_load_oscar_classes_correctly(self):
  14. Product, Category = get_classes('catalogue.models', ('Product', 'Category'))
  15. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  16. self.assertEqual('oscar.apps.catalogue.models', Category.__module__)
  17. def test_load_oscar_class_correctly(self):
  18. Product = get_class('catalogue.models', 'Product')
  19. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  20. def test_load_oscar_class_from_dashboard_subapp(self):
  21. ReportForm = get_class('dashboard.reports.forms', 'ReportForm')
  22. self.assertEqual('oscar.apps.dashboard.reports.forms', ReportForm.__module__)
  23. def test_raise_exception_when_bad_appname_used(self):
  24. with self.assertRaises(AppNotFoundError):
  25. get_classes('fridge.models', ('Product', 'Category'))
  26. def test_raise_exception_when_bad_classname_used(self):
  27. with self.assertRaises(ClassNotFoundError):
  28. get_class('catalogue.models', 'Monkey')
  29. def test_raise_importerror_if_app_raises_importerror(self):
  30. """
  31. This tests that Oscar doesn't fall back to using the Oscar catalogue
  32. app if the overriding app throws an ImportError.
  33. """
  34. apps = list(settings.INSTALLED_APPS)
  35. apps[apps.index('oscar.apps.catalogue')] = 'tests._site.import_error_app.catalogue'
  36. with override_settings(INSTALLED_APPS=apps):
  37. with self.assertRaises(ImportError):
  38. get_class('catalogue.app', 'CatalogueApplication')
  39. class ClassLoadingWithLocalOverrideTests(TestCase):
  40. def setUp(self):
  41. self.installed_apps = list(settings.INSTALLED_APPS)
  42. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.shipping'
  43. def test_loading_class_defined_in_local_module(self):
  44. with override_settings(INSTALLED_APPS=self.installed_apps):
  45. (Free,) = get_classes('shipping.methods', ('Free',))
  46. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  47. def test_loading_class_which_is_not_defined_in_local_module(self):
  48. with override_settings(INSTALLED_APPS=self.installed_apps):
  49. (FixedPrice,) = get_classes('shipping.methods', ('FixedPrice',))
  50. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  51. def test_loading_class_from_module_not_defined_in_local_app(self):
  52. with override_settings(INSTALLED_APPS=self.installed_apps):
  53. (Repository,) = get_classes('shipping.repository', ('Repository',))
  54. self.assertEqual('oscar.apps.shipping.repository', Repository.__module__)
  55. def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
  56. with override_settings(INSTALLED_APPS=self.installed_apps):
  57. (Free, FixedPrice) = get_classes('shipping.methods', ('Free', 'FixedPrice'))
  58. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  59. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  60. def test_loading_classes_with_root_app(self):
  61. import tests._site.shipping
  62. path = dirname(dirname(tests._site.shipping.__file__))
  63. with temporary_python_path([path]):
  64. self.installed_apps[
  65. self.installed_apps.index('tests._site.shipping')] = 'shipping'
  66. with override_settings(INSTALLED_APPS=self.installed_apps):
  67. (Free,) = get_classes('shipping.methods', ('Free',))
  68. self.assertEqual('shipping.methods', Free.__module__)
  69. def test_overriding_view_is_possible_without_overriding_app(self):
  70. from oscar.apps.customer.app import application, CustomerApplication
  71. # If test fails, it's helpful to know if it's caused by order of
  72. # execution
  73. self.assertEqual(CustomerApplication().summary_view.__module__,
  74. 'tests._site.apps.customer.views')
  75. self.assertEqual(application.summary_view.__module__,
  76. 'tests._site.apps.customer.views')
  77. class ClassLoadingWithLocalOverrideWithMultipleSegmentsTests(TestCase):
  78. def setUp(self):
  79. self.installed_apps = list(settings.INSTALLED_APPS)
  80. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.apps.shipping'
  81. def test_loading_class_defined_in_local_module(self):
  82. with override_settings(INSTALLED_APPS=self.installed_apps):
  83. (Free,) = get_classes('shipping.methods', ('Free',))
  84. self.assertEqual('tests._site.apps.shipping.methods', Free.__module__)
  85. class TestGetCoreAppsFunction(TestCase):
  86. """
  87. oscar.get_core_apps function
  88. """
  89. def test_returns_core_apps_when_no_overrides_specified(self):
  90. apps = oscar.get_core_apps()
  91. self.assertEqual(oscar.OSCAR_CORE_APPS, apps)
  92. def test_uses_non_dashboard_override_when_specified(self):
  93. apps = oscar.get_core_apps(overrides=['apps.shipping'])
  94. self.assertTrue('apps.shipping' in apps)
  95. self.assertTrue('oscar.apps.shipping' not in apps)
  96. def test_uses_dashboard_override_when_specified(self):
  97. apps = oscar.get_core_apps(overrides=['apps.dashboard.catalogue'])
  98. self.assertTrue('apps.dashboard.catalogue' in apps)
  99. self.assertTrue('oscar.apps.dashboard.catalogue' not in apps)
  100. self.assertTrue('oscar.apps.catalogue' in apps)
  101. class TestOverridingCoreApps(TestCase):
  102. def test_means_the_overriding_model_is_registered_first(self):
  103. klass = get_model('partner', 'StockRecord')
  104. self.assertEqual('tests._site.apps.partner.models',
  105. klass.__module__)
  106. class TestAppLabelsForModels(TestCase):
  107. def test_all_oscar_models_have_app_labels(self):
  108. from django.apps import apps
  109. models = apps.get_models()
  110. missing = []
  111. for model in models:
  112. # Ignore non-Oscar models
  113. if not 'oscar' in repr(model):
  114. continue
  115. # Don't know how to get the actual model's Meta class. But if
  116. # the parent doesn't have a Meta class, it's doesn't have an
  117. # base in Oscar anyway and is not intended to be overridden
  118. abstract_model = model.__base__
  119. meta_class = getattr(abstract_model, 'Meta', None)
  120. if meta_class is None:
  121. continue
  122. if not hasattr(meta_class, 'app_label'):
  123. missing.append(model)
  124. if missing:
  125. self.fail("Those models don't have an app_label set: %s" % missing)