diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/template_tests/test_loaders.py | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/tests/template_tests/test_loaders.py b/tests/template_tests/test_loaders.py index 11f20c6deb..35921e0472 100644 --- a/tests/template_tests/test_loaders.py +++ b/tests/template_tests/test_loaders.py @@ -49,11 +49,46 @@ class CachedLoaderTests(SimpleTestCase): self.assertEqual(template.origin.template_name, 'index.html') self.assertEqual(template.origin.loader, self.engine.template_loaders[0].loaders[0]) - def test_get_template_missing(self): + def test_get_template_missing_debug_off(self): + """ + With template debugging disabled, the raw TemplateDoesNotExist class + should be cached when a template is missing. See ticket #26306 and + docstrings in the cached loader for details. + """ + self.engine.debug = False with self.assertRaises(TemplateDoesNotExist): - self.engine.get_template('doesnotexist.html') - e = self.engine.template_loaders[0].get_template_cache['doesnotexist.html'] - self.assertEqual(e.args[0], 'doesnotexist.html') + self.engine.get_template('prod-template-missing.html') + e = self.engine.template_loaders[0].get_template_cache['prod-template-missing.html'] + self.assertEqual(e, TemplateDoesNotExist) + + def test_get_template_missing_debug_on(self): + """ + With template debugging enabled, a TemplateDoesNotExist instance + should be cached when a template is missing. + """ + self.engine.debug = True + with self.assertRaises(TemplateDoesNotExist): + self.engine.get_template('debug-template-missing.html') + e = self.engine.template_loaders[0].get_template_cache['debug-template-missing.html'] + self.assertIsInstance(e, TemplateDoesNotExist) + self.assertEqual(e.args[0], 'debug-template-missing.html') + + @unittest.skipIf(six.PY2, "Python 2 doesn't set extra exception attributes") + def test_cached_exception_no_traceback(self): + """ + When a TemplateDoesNotExist instance is cached, the cached instance + should not contain the __traceback__, __context__, or __cause__ + attributes that Python sets when raising exceptions. + """ + self.engine.debug = True + with self.assertRaises(TemplateDoesNotExist): + self.engine.get_template('no-traceback-in-cache.html') + e = self.engine.template_loaders[0].get_template_cache['no-traceback-in-cache.html'] + + error_msg = "Cached TemplateDoesNotExist must not have been thrown." + self.assertIsNone(e.__traceback__, error_msg) + self.assertIsNone(e.__context__, error_msg) + self.assertIsNone(e.__cause__, error_msg) @ignore_warnings(category=RemovedInDjango20Warning) def test_load_template(self): |
