diff options
| author | Juan Catalano <catalanojuan@gmail.com> | 2013-09-06 20:23:25 -0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-09-13 20:09:41 -0400 |
| commit | 6feb75129f42fdac751d0b79a2a005b4c0ad5c2d (patch) | |
| tree | 5bb6cbb83482c15af59cfce69ae2339ff2aa0f3d /tests/utils_tests | |
| parent | 39b49fd33970720b973498d2c8275ced129d3a7a (diff) | |
Fixed #21060 -- Refactored admin's autodiscover method to make it reusable.
We want to be able to use it for instance for discovering `tasks.py` modules
inside the INSTALLED_APPS.
This commit therefore moves the logic to `autodiscover_modules` method in
django.utils.module_loading.
Diffstat (limited to 'tests/utils_tests')
| -rw-r--r-- | tests/utils_tests/test_module/__init__.py | 3 | ||||
| -rw-r--r-- | tests/utils_tests/test_module/another_bad_module.py | 8 | ||||
| -rw-r--r-- | tests/utils_tests/test_module/another_good_module.py | 1 | ||||
| -rw-r--r-- | tests/utils_tests/test_module_loading.py | 31 |
4 files changed, 42 insertions, 1 deletions
diff --git a/tests/utils_tests/test_module/__init__.py b/tests/utils_tests/test_module/__init__.py index e69de29bb2..8f33921eb6 100644 --- a/tests/utils_tests/test_module/__init__.py +++ b/tests/utils_tests/test_module/__init__.py @@ -0,0 +1,3 @@ +class SiteMock(object): + _registry = {} +site = SiteMock() diff --git a/tests/utils_tests/test_module/another_bad_module.py b/tests/utils_tests/test_module/another_bad_module.py new file mode 100644 index 0000000000..eac25c4aa5 --- /dev/null +++ b/tests/utils_tests/test_module/another_bad_module.py @@ -0,0 +1,8 @@ +from . import site +content = 'Another Bad Module' + +site._registry.update({ + 'foo': 'bar', +}) + +raise Exception('Some random exception.') diff --git a/tests/utils_tests/test_module/another_good_module.py b/tests/utils_tests/test_module/another_good_module.py new file mode 100644 index 0000000000..ed4b2d34a2 --- /dev/null +++ b/tests/utils_tests/test_module/another_good_module.py @@ -0,0 +1 @@ +content = 'Another Good Module' diff --git a/tests/utils_tests/test_module_loading.py b/tests/utils_tests/test_module_loading.py index 2423215778..d808ab8783 100644 --- a/tests/utils_tests/test_module_loading.py +++ b/tests/utils_tests/test_module_loading.py @@ -6,7 +6,10 @@ import unittest from zipimport import zipimporter from django.core.exceptions import ImproperlyConfigured -from django.utils.module_loading import import_by_path, module_has_submodule +from django.test import SimpleTestCase +from django.test.utils import override_settings +from django.utils import six +from django.utils.module_loading import autodiscover_modules, import_by_path, module_has_submodule from django.utils._os import upath @@ -130,6 +133,32 @@ class ModuleImportTestCase(unittest.TestCase): self.assertIsNotNone(traceback.tb_next.tb_next, 'Should have more than the calling frame in the traceback.') +@override_settings(INSTALLED_APPS=('utils_tests.test_module',)) +class AutodiscoverModulesTestCase(SimpleTestCase): + + def test_autodiscover_modules_found(self): + autodiscover_modules('good_module') + + def test_autodiscover_modules_not_found(self): + autodiscover_modules('missing_module') + + def test_autodiscover_modules_found_but_bad_module(self): + with six.assertRaisesRegex(self, ImportError, "No module named '?a_package_name_that_does_not_exist'?"): + autodiscover_modules('bad_module') + + def test_autodiscover_modules_several_one_bad_module(self): + with six.assertRaisesRegex(self, ImportError, "No module named '?a_package_name_that_does_not_exist'?"): + autodiscover_modules('good_module', 'bad_module') + + def test_autodiscover_modules_several_found(self): + autodiscover_modules('good_module', 'another_good_module') + + def test_validate_registry_keeps_intact(self): + from .test_module import site + with six.assertRaisesRegex(self, Exception, "Some random exception."): + autodiscover_modules('another_bad_module', register_to=site) + self.assertEqual(site._registry, {}) + class ProxyFinder(object): def __init__(self): |
