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

loading_tests.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from os.path import dirname
  2. from django.test import TestCase
  3. from django.conf import settings
  4. from oscar.core.loading import get_model
  5. from django.test.utils import override_settings
  6. import oscar
  7. from tests import temporary_python_path
  8. from oscar.core.loading import (
  9. AppNotFoundError,
  10. get_classes, get_class, ClassNotFoundError)
  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. installed_apps = list(settings.INSTALLED_APPS)
  33. installed_apps.insert(0, 'tests._site.import_error_app.catalogue')
  34. with override_settings(INSTALLED_APPS=installed_apps):
  35. with self.assertRaises(ImportError):
  36. get_class('catalogue.app', 'CatalogueApplication')
  37. class ClassLoadingWithLocalOverrideTests(TestCase):
  38. def setUp(self):
  39. self.installed_apps = list(settings.INSTALLED_APPS)
  40. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.shipping'
  41. def test_loading_class_defined_in_local_module(self):
  42. with override_settings(INSTALLED_APPS=self.installed_apps):
  43. (Free,) = get_classes('shipping.methods', ('Free',))
  44. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  45. def test_loading_class_which_is_not_defined_in_local_module(self):
  46. with override_settings(INSTALLED_APPS=self.installed_apps):
  47. (FixedPrice,) = get_classes('shipping.methods', ('FixedPrice',))
  48. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  49. def test_loading_class_from_module_not_defined_in_local_app(self):
  50. with override_settings(INSTALLED_APPS=self.installed_apps):
  51. (Repository,) = get_classes('shipping.repository', ('Repository',))
  52. self.assertEqual('oscar.apps.shipping.repository', Repository.__module__)
  53. def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
  54. with override_settings(INSTALLED_APPS=self.installed_apps):
  55. (Free, FixedPrice) = get_classes('shipping.methods', ('Free', 'FixedPrice'))
  56. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  57. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  58. def test_loading_classes_with_root_app(self):
  59. import tests._site.shipping
  60. path = dirname(dirname(tests._site.shipping.__file__))
  61. with temporary_python_path([path]):
  62. self.installed_apps[
  63. self.installed_apps.index('tests._site.shipping')] = 'shipping'
  64. with override_settings(INSTALLED_APPS=self.installed_apps):
  65. (Free,) = get_classes('shipping.methods', ('Free',))
  66. self.assertEqual('shipping.methods', Free.__module__)
  67. def test_overriding_view_is_possible_without_overriding_app(self):
  68. from oscar.apps.customer.app import application, CustomerApplication
  69. # If test fails, it's helpful to know if it's caused by order of
  70. # execution
  71. self.assertEqual(CustomerApplication().summary_view.__module__,
  72. 'tests._site.apps.customer.views')
  73. self.assertEqual(application.summary_view.__module__,
  74. 'tests._site.apps.customer.views')
  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)
  99. class TestOverridingCoreApps(TestCase):
  100. def test_means_the_overriding_model_is_registered_first(self):
  101. klass = get_model('partner', 'StockRecord')
  102. self.assertEqual('tests._site.apps.partner.models',
  103. klass.__module__)