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/utils_tests/test_module_loading.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'tests/utils_tests/test_module_loading.py') 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): -- cgit v1.3