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

loading_tests.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. AppNotFoundError,
  9. get_classes, get_class, ClassNotFoundError)
  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. installed_apps = list(settings.INSTALLED_APPS)
  32. installed_apps.insert(0, 'tests._site.import_error_app.catalogue')
  33. with override_settings(INSTALLED_APPS=installed_apps):
  34. with self.assertRaises(ImportError):
  35. get_class('catalogue.app', 'CatalogueApplication')
  36. class ClassLoadingWithLocalOverrideTests(TestCase):
  37. def setUp(self):
  38. self.installed_apps = list(settings.INSTALLED_APPS)
  39. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.shipping'
  40. def test_loading_class_defined_in_local_module(self):
  41. with override_settings(INSTALLED_APPS=self.installed_apps):
  42. (Free,) = get_classes('shipping.methods', ('Free',))
  43. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  44. def test_loading_class_which_is_not_defined_in_local_module(self):
  45. with override_settings(INSTALLED_APPS=self.installed_apps):
  46. (FixedPrice,) = get_classes('shipping.methods', ('FixedPrice',))
  47. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  48. def test_loading_class_from_module_not_defined_in_local_app(self):
  49. with override_settings(INSTALLED_APPS=self.installed_apps):
  50. (Repository,) = get_classes('shipping.repository', ('Repository',))
  51. self.assertEqual('oscar.apps.shipping.repository', Repository.__module__)
  52. def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
  53. with override_settings(INSTALLED_APPS=self.installed_apps):
  54. (Free, FixedPrice) = get_classes('shipping.methods', ('Free', 'FixedPrice'))
  55. self.assertEqual('tests._site.shipping.methods', Free.__module__)
  56. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  57. def test_loading_classes_with_root_app(self):
  58. import tests._site.shipping
  59. path = dirname(dirname(tests._site.shipping.__file__))
  60. with temporary_python_path([path]):
  61. self.installed_apps[
  62. self.installed_apps.index('tests._site.shipping')] = 'shipping'
  63. with override_settings(INSTALLED_APPS=self.installed_apps):
  64. (Free,) = get_classes('shipping.methods', ('Free',))
  65. self.assertEqual('shipping.methods', Free.__module__)
  66. class ClassLoadingWithLocalOverrideWithMultipleSegmentsTests(TestCase):
  67. def setUp(self):
  68. self.installed_apps = list(settings.INSTALLED_APPS)
  69. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.apps.shipping'
  70. def test_loading_class_defined_in_local_module(self):
  71. with override_settings(INSTALLED_APPS=self.installed_apps):
  72. (Free,) = get_classes('shipping.methods', ('Free',))
  73. self.assertEqual('tests._site.apps.shipping.methods', Free.__module__)
  74. class TestGetCoreAppsFunction(TestCase):
  75. """
  76. oscar.get_core_apps function
  77. """
  78. def test_returns_core_apps_when_no_overrides_specified(self):
  79. apps = oscar.get_core_apps()
  80. self.assertEqual(oscar.OSCAR_CORE_APPS, apps)
  81. def test_uses_non_dashboard_override_when_specified(self):
  82. apps = oscar.get_core_apps(overrides=['apps.shipping'])
  83. self.assertTrue('apps.shipping' in apps)
  84. self.assertTrue('oscar.apps.shipping' not in apps)
  85. def test_uses_dashboard_override_when_specified(self):
  86. apps = oscar.get_core_apps(overrides=['apps.dashboard.catalogue'])
  87. self.assertTrue('apps.dashboard.catalogue' in apps)
  88. self.assertTrue('oscar.apps.dashboard.catalogue' not in apps)
  89. self.assertTrue('oscar.apps.catalogue' in apps)