From 5d263dee304fdaf95e18d2f0619d6925984a7f02 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 20 Jan 2014 22:15:14 +0200 Subject: Fixed #21674 -- Deprecated the import_by_path() function in favor of import_string(). Thanks Aymeric Augustin for the suggestion and review. --- tests/file_storage/tests.py | 18 ++++++------------ tests/staticfiles_tests/tests.py | 4 ++-- tests/utils_tests/test_module_loading.py | 32 +++++++++++++++++++++++++------- tests/wsgi/tests.py | 2 +- 4 files changed, 34 insertions(+), 22 deletions(-) (limited to 'tests') diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 2f047f049a..ae2076c5e4 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -16,7 +16,7 @@ except ImportError: import dummy_threading as threading from django.core.cache import cache -from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured +from django.core.exceptions import SuspiciousOperation from django.core.files.base import File, ContentFile from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.uploadedfile import SimpleUploadedFile @@ -43,29 +43,23 @@ class GetStorageClassTests(SimpleTestCase): """ get_storage_class raises an error if the requested import don't exist. """ - with six.assertRaisesRegex(self, ImproperlyConfigured, - "Error importing module storage: \"No module named '?storage'?\""): + with six.assertRaisesRegex(self, ImportError, "No module named '?storage'?"): get_storage_class('storage.NonExistingStorage') def test_get_nonexisting_storage_class(self): """ get_storage_class raises an error if the requested class don't exist. """ - self.assertRaisesMessage( - ImproperlyConfigured, - 'Module "django.core.files.storage" does not define a ' - '"NonExistingStorage" attribute/class', - get_storage_class, - 'django.core.files.storage.NonExistingStorage') + self.assertRaises(ImportError, get_storage_class, + 'django.core.files.storage.NonExistingStorage') def test_get_nonexisting_storage_module(self): """ get_storage_class raises an error if the requested module don't exist. """ # Error message may or may not be the fully qualified path. - with six.assertRaisesRegex(self, ImproperlyConfigured, - "Error importing module django.core.files.non_existing_storage: " - "\"No module named '?(django.core.files.)?non_existing_storage'?\""): + with six.assertRaisesRegex(self, ImportError, + "No module named '?(django.core.files.)?non_existing_storage'?"): get_storage_class( 'django.core.files.non_existing_storage.NonExistingStorage') diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py index 74e3782c83..1d3daff11e 100644 --- a/tests/staticfiles_tests/tests.py +++ b/tests/staticfiles_tests/tests.py @@ -788,11 +788,11 @@ class TestMiscFinder(TestCase): finders.FileSystemFinder) def test_get_finder_bad_classname(self): - self.assertRaises(ImproperlyConfigured, finders.get_finder, + self.assertRaises(ImportError, finders.get_finder, 'django.contrib.staticfiles.finders.FooBarFinder') def test_get_finder_bad_module(self): - self.assertRaises(ImproperlyConfigured, + self.assertRaises(ImportError, finders.get_finder, 'foo.bar.FooBarFinder') def test_cache(self): diff --git a/tests/utils_tests/test_module_loading.py b/tests/utils_tests/test_module_loading.py index 905163b1b9..2eb01ad770 100644 --- a/tests/utils_tests/test_module_loading.py +++ b/tests/utils_tests/test_module_loading.py @@ -3,13 +3,15 @@ from importlib import import_module import os import sys import unittest +import warnings from zipimport import zipimporter from django.core.exceptions import ImproperlyConfigured from django.test import SimpleTestCase, modify_settings -from django.test.utils import extend_sys_path +from django.test.utils import IgnorePendingDeprecationWarningsMixin, extend_sys_path from django.utils import six -from django.utils.module_loading import autodiscover_modules, import_by_path, module_has_submodule +from django.utils.module_loading import (autodiscover_modules, import_by_path, import_string, + module_has_submodule) from django.utils._os import upath @@ -107,15 +109,13 @@ class EggLoader(unittest.TestCase): self.assertRaises(ImportError, import_module, 'egg_module.sub1.sub2.no_such_module') -class ModuleImportTestCase(unittest.TestCase): +class ModuleImportTestCase(IgnorePendingDeprecationWarningsMixin, unittest.TestCase): def test_import_by_path(self): - cls = import_by_path( - 'django.utils.module_loading.import_by_path') + cls = import_by_path('django.utils.module_loading.import_by_path') self.assertEqual(cls, import_by_path) # Test exceptions raised - for path in ('no_dots_in_path', 'unexistent.path', - 'utils_tests.unexistent'): + for path in ('no_dots_in_path', 'unexistent.path', 'utils_tests.unexistent'): self.assertRaises(ImproperlyConfigured, import_by_path, path) with self.assertRaises(ImproperlyConfigured) as cm: @@ -132,6 +132,24 @@ class ModuleImportTestCase(unittest.TestCase): self.assertIsNotNone(traceback.tb_next.tb_next, 'Should have more than the calling frame in the traceback.') + def test_import_by_path_pending_deprecation_warning(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always', category=PendingDeprecationWarning) + cls = import_by_path('django.utils.module_loading.import_by_path') + self.assertEqual(cls, import_by_path) + self.assertEqual(len(w), 1) + self.assertTrue(issubclass(w[-1].category, PendingDeprecationWarning)) + self.assertIn('deprecated', str(w[-1].message)) + + def test_import_string(self): + cls = import_string('django.utils.module_loading.import_string') + self.assertEqual(cls, import_string) + + # Test exceptions raised + self.assertRaises(ImportError, import_string, 'no_dots_in_path') + self.assertRaises(ImportError, import_string, 'utils_tests.unexistent') + self.assertRaises(ImportError, import_string, 'unexistent.path') + @modify_settings(INSTALLED_APPS={'append': 'utils_tests.test_module'}) class AutodiscoverModulesTestCase(SimpleTestCase): diff --git a/tests/wsgi/tests.py b/tests/wsgi/tests.py index 567ca94f98..299beacf46 100644 --- a/tests/wsgi/tests.py +++ b/tests/wsgi/tests.py @@ -101,6 +101,6 @@ class GetInternalWSGIApplicationTest(unittest.TestCase): def test_bad_name(self): with six.assertRaisesRegex(self, ImproperlyConfigured, - r"^WSGI application 'wsgi.wsgi.noexist' could not be loaded; Module.*"): + r"^WSGI application 'wsgi.wsgi.noexist' could not be loaded; Error importing.*"): get_internal_wsgi_application() -- cgit v1.2.1