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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 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. class ClassLoadingWithLocalOverrideWithMultipleSegmentsTests(TestCase):
  70. def setUp(self):
  71. self.installed_apps = list(settings.INSTALLED_APPS)
  72. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests._site.apps.shipping'
  73. def test_loading_class_defined_in_local_module(self):
  74. with override_settings(INSTALLED_APPS=self.installed_apps):
  75. (Free,) = get_classes('shipping.methods', ('Free',))
  76. self.assertEqual('tests._site.apps.shipping.methods', Free.__module__)
  77. class TestGetCoreAppsFunction(TestCase):
  78. """
  79. oscar.get_core_apps function
  80. """
  81. def test_returns_core_apps_when_no_overrides_specified(self):
  82. apps = oscar.get_core_apps()
  83. self.assertEqual(oscar.OSCAR_CORE_APPS, apps)
  84. def test_uses_non_dashboard_override_when_specified(self):
  85. apps = oscar.get_core_apps(overrides=['apps.shipping'])
  86. self.assertTrue('apps.shipping' in apps)
  87. self.assertTrue('oscar.apps.shipping' not in apps)
  88. def test_uses_dashboard_override_when_specified(self):
  89. apps = oscar.get_core_apps(overrides=['apps.dashboard.catalogue'])
  90. self.assertTrue('apps.dashboard.catalogue' in apps)
  91. self.assertTrue('oscar.apps.dashboard.catalogue' not in apps)
  92. self.assertTrue('oscar.apps.catalogue' in apps)