diff options
author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-17 22:10:57 +0100 |
---|---|---|
committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-28 17:02:30 +0100 |
commit | cf0fd65ed42d5d4f0585da413db4b1cf7c6b0d1a (patch) | |
tree | 3f4f652525adf16178018e4ce0c7eddeb0f61360 /tests | |
parent | d3a982556d655adcf4ba331d2def685d8249170f (diff) | |
download | django-cf0fd65ed42d5d4f0585da413db4b1cf7c6b0d1a.tar.gz |
Deprecated TEMPLATE_LOADERS.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/settings_tests/tests.py | 8 | ||||
-rw-r--r-- | tests/template_tests/test_loaders.py | 114 | ||||
-rw-r--r-- | tests/template_tests/tests.py | 99 | ||||
-rw-r--r-- | tests/view_tests/tests/test_debug.py | 33 | ||||
-rw-r--r-- | tests/view_tests/tests/test_defaults.py | 25 |
5 files changed, 168 insertions, 111 deletions
diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index c87f6b6204..f1a43763b6 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -431,14 +431,14 @@ class IsOverriddenTest(TestCase): s = Settings('fake_settings_module') self.assertTrue(s.is_overridden('SECRET_KEY')) - self.assertFalse(s.is_overridden('TEMPLATE_LOADERS')) + self.assertFalse(s.is_overridden('ALLOWED_HOSTS')) finally: del sys.modules['fake_settings_module'] def test_override(self): - self.assertFalse(settings.is_overridden('TEMPLATE_LOADERS')) - with override_settings(TEMPLATE_LOADERS=[]): - self.assertTrue(settings.is_overridden('TEMPLATE_LOADERS')) + self.assertFalse(settings.is_overridden('ALLOWED_HOSTS')) + with override_settings(ALLOWED_HOSTS=[]): + self.assertTrue(settings.is_overridden('ALLOWED_HOSTS')) class TestTupleSettings(unittest.TestCase): diff --git a/tests/template_tests/test_loaders.py b/tests/template_tests/test_loaders.py index 14390c3f46..47e69ad3c5 100644 --- a/tests/template_tests/test_loaders.py +++ b/tests/template_tests/test_loaders.py @@ -16,7 +16,7 @@ except ImportError: from django.template import TemplateDoesNotExist, Context -from django.template.loaders.eggs import Loader as EggLoader +from django.template.loaders import cached, eggs from django.template.engine import Engine from django.template import loader from django.test import SimpleTestCase, override_settings @@ -26,6 +26,11 @@ from django.utils._os import upath from django.utils.six import StringIO +TEMPLATES_DIR = os.path.join(os.path.dirname(upath(__file__)), 'templates') + +GLOBAL_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.dirname(upath(__file__))), 'templates') + + # Mock classes and objects for pkg_resources functions. class MockLoader(object): pass @@ -48,7 +53,10 @@ def create_egg(name, resources): @unittest.skipUnless(pkg_resources, 'setuptools is not installed') class EggLoaderTest(SimpleTestCase): + def setUp(self): + self.loader = eggs.Loader(Engine.get_default()) + # Defined here b/c at module scope we may not have pkg_resources class MockProvider(pkg_resources.NullProvider): def __init__(self, module): @@ -81,70 +89,64 @@ class EggLoaderTest(SimpleTestCase): @override_settings(INSTALLED_APPS=['egg_empty']) def test_empty(self): "Loading any template on an empty egg should fail" - egg_loader = EggLoader(Engine.get_default()) - self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html") + with self.assertRaises(TemplateDoesNotExist): + self.loader.load_template_source("not-existing.html") @override_settings(INSTALLED_APPS=['egg_1']) def test_non_existing(self): "Template loading fails if the template is not in the egg" - egg_loader = EggLoader(Engine.get_default()) - self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html") + with self.assertRaises(TemplateDoesNotExist): + self.loader.load_template_source("not-existing.html") @override_settings(INSTALLED_APPS=['egg_1']) def test_existing(self): "A template can be loaded from an egg" - egg_loader = EggLoader(Engine.get_default()) - contents, template_name = egg_loader.load_template_source("y.html") + contents, template_name = self.loader.load_template_source("y.html") self.assertEqual(contents, "y") self.assertEqual(template_name, "egg:egg_1:templates/y.html") def test_not_installed(self): "Loading an existent template from an egg not included in any app should fail" - egg_loader = EggLoader(Engine.get_default()) - self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html") + with self.assertRaises(TemplateDoesNotExist): + self.loader.load_template_source("y.html") -@override_settings( - TEMPLATE_LOADERS=( - ('django.template.loaders.cached.Loader', ( - 'django.template.loaders.filesystem.Loader', - )), - ) -) class CachedLoader(SimpleTestCase): + + def setUp(self): + self.loader = cached.Loader(Engine.get_default(), [ + 'django.template.loaders.filesystem.Loader', + ]) + def test_templatedir_caching(self): "Check that the template directories form part of the template cache key. Refs #13573" - template_loader = Engine.get_default().template_loaders[0] - # Retrieve a template specifying a template directory to check - t1, name = template_loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'first'),)) + t1, name = self.loader.find_template('test.html', (os.path.join(TEMPLATES_DIR, 'first'),)) # Now retrieve the same template name, but from a different directory - t2, name = template_loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'second'),)) + t2, name = self.loader.find_template('test.html', (os.path.join(TEMPLATES_DIR, 'second'),)) # The two templates should not have the same content self.assertNotEqual(t1.render(Context({})), t2.render(Context({}))) def test_missing_template_is_cached(self): "#19949 -- Check that the missing template is cached." - template_loader = Engine.get_default().template_loaders[0] - # Empty cache, which may be filled from previous tests. - template_loader.reset() # Check that 'missing.html' isn't already in cache before 'missing.html' is loaded - self.assertRaises(KeyError, lambda: template_loader.template_cache["missing.html"]) + with self.assertRaises(KeyError): + self.loader.template_cache["missing.html"] # Try to load it, it should fail - self.assertRaises(TemplateDoesNotExist, template_loader.load_template, "missing.html") + with self.assertRaises(TemplateDoesNotExist): + self.loader.load_template("missing.html") # Verify that the fact that the missing template, which hasn't been found, has actually # been cached: - self.assertEqual(template_loader.template_cache.get("missing.html"), - TemplateDoesNotExist, + cached_miss = self.loader.template_cache["missing.html"] + self.assertEqual(cached_miss, TemplateDoesNotExist, "Cached template loader doesn't cache file lookup misses. It should.") -@override_settings( - TEMPLATE_DIRS=( - os.path.join(os.path.dirname(upath(__file__)), 'templates'), - ) -) +@override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [TEMPLATES_DIR], +}]) class RenderToStringTest(SimpleTestCase): def test_basic(self): self.assertEqual(loader.render_to_string('test_context.html'), 'obj:\n') @@ -164,11 +166,10 @@ class RenderToStringTest(SimpleTestCase): loader.select_template, []) -@override_settings( - TEMPLATE_DIRS=( - os.path.join(os.path.dirname(upath(__file__)), 'templates'), - ) -) +@override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [TEMPLATES_DIR], +}]) class DeprecatedRenderToStringTest(IgnorePendingDeprecationWarningsMixin, SimpleTestCase): def test_existing_context_kept_clean(self): @@ -191,7 +192,10 @@ class DeprecatedRenderToStringTest(IgnorePendingDeprecationWarningsMixin, Simple loader.render_to_string('test_context_stack.html', context_instance=Context()).strip()) -class TemplateDirsOverrideTest(IgnorePendingDeprecationWarningsMixin, unittest.TestCase): +@override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', +}]) +class TemplateDirsOverrideTest(IgnorePendingDeprecationWarningsMixin, SimpleTestCase): dirs_tuple = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),) dirs_list = list(dirs_tuple) @@ -212,14 +216,18 @@ class TemplateDirsOverrideTest(IgnorePendingDeprecationWarningsMixin, unittest.T self.assertEqual(template.render(Context({})), 'spam eggs\n') -@override_settings( - TEMPLATE_LOADERS=( - ('django.template.loaders.cached.Loader', ( - 'django.template.loaders.filesystem.Loader', - 'django.template.loaders.app_directories.Loader', - )), - ) -) +@override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [GLOBAL_TEMPLATES_DIR], + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.cached.Loader', [ + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', + ]), + ], + }, +}]) class PriorityCacheLoader(SimpleTestCase): def test_basic(self): """ @@ -229,10 +237,16 @@ class PriorityCacheLoader(SimpleTestCase): self.assertEqual(t1.render(Context({})), 'priority\n') -@override_settings( - TEMPLATE_LOADERS=('django.template.loaders.filesystem.Loader', - 'django.template.loaders.app_directories.Loader',), -) +@override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [GLOBAL_TEMPLATES_DIR], + 'OPTIONS': { + 'loaders': [ + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', + ], + }, +}]) class PriorityLoader(SimpleTestCase): def test_basic(self): """ diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 5128fd64d5..876df43aa4 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -17,6 +17,11 @@ from django.test.utils import override_settings, extend_sys_path from django.utils._os import upath +TESTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(upath(__file__)))) + +TEMPLATES_DIR = os.path.join(TESTS_DIR, 'templates') + + class TemplateLoaderTests(SimpleTestCase): def test_loaders_security(self): @@ -73,7 +78,10 @@ class TemplateLoaderTests(SimpleTestCase): test_template_sources('/DIR1/index.HTML', template_dirs, ['/DIR1/index.HTML']) - @override_settings(TEMPLATE_LOADERS=['django.template.loaders.filesystem.Loader']) + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [TEMPLATES_DIR], + }]) # Turn TEMPLATE_DEBUG on, so that the origin file name will be kept with # the compiled templates. @override_settings(TEMPLATE_DEBUG=True) @@ -90,10 +98,17 @@ class TemplateLoaderTests(SimpleTestCase): self.assertTrue(template_name.endswith(load_name), 'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name) - @override_settings(TEMPLATE_LOADERS=[ - ('django.template.loaders.cached.Loader', - ['django.template.loaders.filesystem.Loader']), - ]) + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [TEMPLATES_DIR], + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.cached.Loader', [ + 'django.template.loaders.filesystem.Loader', + ]), + ], + }, + }]) @override_settings(TEMPLATE_DEBUG=True) def test_cached_loader_debug_origin(self): # Same comment as in test_loader_debug_origin. @@ -130,7 +145,10 @@ class TemplateLoaderTests(SimpleTestCase): # Test the base loader class via the app loader. load_template # from base is used by all shipped loaders excepting cached, # which has its own test. - @override_settings(TEMPLATE_LOADERS=['django.template.loaders.app_directories.Loader']) + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + }]) def test_include_missing_template(self): """ Tests that the correct template is identified as not existing @@ -151,7 +169,10 @@ class TemplateLoaderTests(SimpleTestCase): # Test the base loader class via the app loader. load_template # from base is used by all shipped loaders excepting cached, # which has its own test. - @override_settings(TEMPLATE_LOADERS=['django.template.loaders.app_directories.Loader']) + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + }]) def test_extends_include_missing_baseloader(self): """ Tests that the correct template is identified as not existing @@ -168,34 +189,39 @@ class TemplateLoaderTests(SimpleTestCase): self.assertEqual(e.args[0], 'missing.html') self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.cached.Loader', [ + 'django.template.loaders.app_directories.Loader', + ]), + ], + }, + }]) @override_settings(TEMPLATE_DEBUG=True) def test_extends_include_missing_cachedloader(self): """ Same as test_extends_include_missing_baseloader, only tests behavior of the cached loader instead of base loader. """ - with override_settings(TEMPLATE_LOADERS=[ - ('django.template.loaders.cached.Loader', [ - 'django.template.loaders.app_directories.Loader', - ]), - ]): - load_name = 'test_extends_error.html' - tmpl = loader.get_template(load_name) - r = None - try: - r = tmpl.render(template.Context({})) - except template.TemplateDoesNotExist as e: - self.assertEqual(e.args[0], 'missing.html') - self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) - - # For the cached loader, repeat the test, to ensure the first attempt did not cache a - # result that behaves incorrectly on subsequent attempts. - tmpl = loader.get_template(load_name) - try: - tmpl.render(template.Context({})) - except template.TemplateDoesNotExist as e: - self.assertEqual(e.args[0], 'missing.html') - self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) + load_name = 'test_extends_error.html' + tmpl = loader.get_template(load_name) + r = None + try: + r = tmpl.render(template.Context({})) + except template.TemplateDoesNotExist as e: + self.assertEqual(e.args[0], 'missing.html') + self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) + + # For the cached loader, repeat the test, to ensure the first attempt did not cache a + # result that behaves incorrectly on subsequent attempts. + tmpl = loader.get_template(load_name) + try: + tmpl.render(template.Context({})) + except template.TemplateDoesNotExist as e: + self.assertEqual(e.args[0], 'missing.html') + self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r) def test_include_template_argument(self): """ @@ -429,11 +455,16 @@ class RequestContextTests(unittest.TestCase): def setUp(self): self.fake_request = RequestFactory().get('/') - @override_settings(TEMPLATE_LOADERS=[ - ('django.template.loaders.locmem.Loader', { - 'child': '{{ var|default:"none" }}', - }), - ]) + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.locmem.Loader', { + 'child': '{{ var|default:"none" }}', + }), + ], + }, + }]) def test_include_only(self): """ Regression test for #15721, ``{% include %}`` and ``RequestContext`` diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index b9987c72b0..ab04c6622d 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -63,21 +63,28 @@ class DebugViewTests(TestCase): response = self.client.get('/raises400/') self.assertContains(response, '<div class="context" id="', status_code=400) + # Ensure no 403.html template exists to test the default case. + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + }]) def test_403(self): - # Ensure no 403.html template exists to test the default case. - with override_settings(TEMPLATE_LOADERS=[]): - response = self.client.get('/raises403/') - self.assertContains(response, '<h1>403 Forbidden</h1>', status_code=403) - + response = self.client.get('/raises403/') + self.assertContains(response, '<h1>403 Forbidden</h1>', status_code=403) + + # Set up a test 403.html template. + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.locmem.Loader', { + '403.html': 'This is a test template for a 403 error.', + }), + ], + }, + }]) def test_403_template(self): - # Set up a test 403.html template. - with override_settings(TEMPLATE_LOADERS=[ - ('django.template.loaders.locmem.Loader', { - '403.html': 'This is a test template for a 403 Forbidden error.', - }) - ]): - response = self.client.get('/raises403/') - self.assertContains(response, 'test template', status_code=403) + response = self.client.get('/raises403/') + self.assertContains(response, 'test template', status_code=403) def test_404(self): response = self.client.get('/raises404/') diff --git a/tests/view_tests/tests/test_defaults.py b/tests/view_tests/tests/test_defaults.py index 6a3a495c23..7b95211a5a 100644 --- a/tests/view_tests/tests/test_defaults.py +++ b/tests/view_tests/tests/test_defaults.py @@ -35,21 +35,26 @@ class DefaultsTests(TestCase): response = self.client.get('/server_error/') self.assertEqual(response.status_code, 500) + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.locmem.Loader', { + '404.html': 'This is a test template for a 404 error.', + '500.html': 'This is a test template for a 500 error.', + }), + ], + }, + }]) def test_custom_templates(self): """ Test that 404.html and 500.html templates are picked by their respective handler. """ - with override_settings(TEMPLATE_LOADERS=[ - ('django.template.loaders.locmem.Loader', { - '404.html': 'This is a test template for a 404 error.', - '500.html': 'This is a test template for a 500 error.', - }), - ]): - for code, url in ((404, '/non_existing_url/'), (500, '/server_error/')): - response = self.client.get(url) - self.assertContains(response, "test template for a %d error" % code, - status_code=code) + for code, url in ((404, '/non_existing_url/'), (500, '/server_error/')): + response = self.client.get(url) + self.assertContains(response, "test template for a %d error" % code, + status_code=code) def test_get_absolute_url_attributes(self): "A model can set attributes on the get_absolute_url method" |