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.9KB

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