Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

loading_tests.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 tests import temporary_python_path
  7. from oscar.core.loading import (
  8. import_module, AppNotFoundError,
  9. get_classes, get_class, ClassNotFoundError)
  10. class TestImportModule(TestCase):
  11. """
  12. oscar.core.loading.import_module
  13. """
  14. def test_imports_a_class_correctly(self):
  15. module = import_module('analytics.models', ['ProductRecord'])
  16. self.assertEqual('oscar.apps.analytics.models', module.__name__)
  17. def test_raises_exception_for_unknown_app(self):
  18. self.assertRaises(AppNotFoundError, import_module, 'banana', ['skin'])
  19. class TestClassLoading(TestCase):
  20. """
  21. Oscar's class loading utilities
  22. """
  23. def test_load_oscar_classes_correctly(self):
  24. Product, Category = get_classes('catalogue.models', ('Product', 'Category'))
  25. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  26. self.assertEqual('oscar.apps.catalogue.models', Category.__module__)
  27. def test_load_oscar_class_correctly(self):
  28. Product = get_class('catalogue.models', 'Product')
  29. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  30. def test_load_oscar_class_from_dashboard_subapp(self):
  31. ReportForm = get_class('dashboard.reports.forms', 'ReportForm')
  32. self.assertEqual('oscar.apps.dashboard.reports.forms', ReportForm.__module__)
  33. def test_raise_exception_when_bad_appname_used(self):
  34. with self.assertRaises(AppNotFoundError):
  35. get_classes('fridge.models', ('Product', 'Category'))
  36. def test_raise_exception_when_bad_classname_used(self):
  37. with self.assertRaises(ClassNotFoundError):
  38. get_class('catalogue.models', 'Monkey')
  39. def test_raise_importerror_if_app_raises_importerror(self):
  40. installed_apps = list(settings.INSTALLED_APPS)
  41. installed_apps.insert(0, 'tests._site.import_error_app.catalogue')
  42. with override_settings(INSTALLED_APPS=installed_apps):
  43. with self.assertRaises(ImportError):
  44. get_class('catalogue.app', 'CatalogueApplication')
  45. class ClassLoadingWithLocalOverrideTests(TestCase):
  46. def setUp(self):
  47. self.installed_apps = list(settings.INSTALLED_APPS)
  48. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.shipping'
  49. def test_loading_class_defined_in_local_module(self):
  50. with override_settings(INSTALLED_APPS=self.installed_apps):
  51. (Free,) = get_classes('shipping.methods', ('Free',))
  52. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  53. def test_loading_class_which_is_not_defined_in_local_module(self):
  54. with override_settings(INSTALLED_APPS=self.installed_apps):
  55. (FixedPrice,) = get_classes('shipping.methods', ('FixedPrice',))
  56. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  57. def test_loading_class_from_module_not_defined_in_local_app(self):
  58. with override_settings(INSTALLED_APPS=self.installed_apps):
  59. (Repository,) = get_classes('shipping.repository', ('Repository',))
  60. self.assertEqual('oscar.apps.shipping.repository', Repository.__module__)
  61. def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
  62. with override_settings(INSTALLED_APPS=self.installed_apps):
  63. (Free, FixedPrice) = get_classes('shipping.methods', ('Free', 'FixedPrice'))
  64. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  65. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  66. def test_loading_classes_with_root_app(self):
  67. import tests._site.shipping
  68. path = dirname(dirname(tests._site.shipping.__file__))
  69. with temporary_python_path([path]):
  70. self.installed_apps[
  71. self.installed_apps.index('tests._site.shipping')] = 'shipping'
  72. with override_settings(INSTALLED_APPS=self.installed_apps):
  73. (Free,) = get_classes('shipping.methods', ('Free',))
  74. self.assertEqual('shipping.methods', Free.__module__)
  75. class ClassLoadingWithLocalOverrideWithMultipleSegmentsTests(TestCase):
  76. def setUp(self):
  77. self.installed_apps = list(settings.INSTALLED_APPS)
  78. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.apps.shipping'
  79. def test_loading_class_defined_in_local_module(self):
  80. with override_settings(INSTALLED_APPS=self.installed_apps):
  81. (Free,) = get_classes('shipping.methods', ('Free',))
  82. self.assertEqual('tests._site.apps.shipping.methods', Free.__module__)
  83. class TestGetCoreAppsFunction(TestCase):
  84. """
  85. oscar.get_core_apps function
  86. """
  87. def test_returns_core_apps_when_no_overrides_specified(self):
  88. apps = oscar.get_core_apps()
  89. self.assertEqual(oscar.OSCAR_CORE_APPS, apps)
  90. def test_uses_non_dashboard_override_when_specified(self):
  91. apps = oscar.get_core_apps(overrides=['apps.shipping'])
  92. self.assertTrue('apps.shipping' in apps)
  93. self.assertTrue('oscar.apps.shipping' not in apps)
  94. def test_uses_dashboard_override_when_specified(self):
  95. apps = oscar.get_core_apps(overrides=['apps.dashboard.catalogue'])
  96. self.assertTrue('apps.dashboard.catalogue' in apps)
  97. self.assertTrue('oscar.apps.dashboard.catalogue' not in apps)
  98. self.assertTrue('oscar.apps.catalogue' in apps)