diff options
| author | Preston Timmons <prestontimmons@gmail.com> | 2015-02-21 13:11:20 -0600 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@oscaro.com> | 2015-02-24 14:00:02 +0100 |
| commit | 06ffc764a9b2d2521e6a574d279b66ad411cc65c (patch) | |
| tree | cc6941da696ae676f2ce61b4e5d2db1af3285b97 /tests/template_tests/syntax_tests/test_include.py | |
| parent | 441a47e1efd46001ca454b80e0d5f8c5ea4e235b (diff) | |
Moved include tests into syntax_tests/test_include.py.
Diffstat (limited to 'tests/template_tests/syntax_tests/test_include.py')
| -rw-r--r-- | tests/template_tests/syntax_tests/test_include.py | 133 |
1 files changed, 131 insertions, 2 deletions
diff --git a/tests/template_tests/syntax_tests/test_include.py b/tests/template_tests/syntax_tests/test_include.py index 17ad9c1e4c..ecd3d59c29 100644 --- a/tests/template_tests/syntax_tests/test_include.py +++ b/tests/template_tests/syntax_tests/test_include.py @@ -1,5 +1,7 @@ -from django.template import Context, TemplateDoesNotExist, TemplateSyntaxError -from django.test import SimpleTestCase +from django.template import ( + Context, Template, TemplateDoesNotExist, TemplateSyntaxError, engines, +) +from django.test import SimpleTestCase, override_settings from ..utils import setup from .test_basic import basic_templates @@ -199,3 +201,130 @@ class IncludeTagTests(SimpleTestCase): template.render(context) else: self.assertEqual(template.render(context), '') + + +class IncludeTests(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(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + 'OPTIONS': { + # Enable debug, otherwise the exception raised during + # {% include %} processing will be suppressed. + 'debug': True, + } + }]) + def test_include_missing_template(self): + """ + Tests that the correct template is identified as not existing + when {% include %} specifies a template that does not exist. + """ + template = engines['django'].get_template('test_include_error.html') + with self.assertRaises(TemplateDoesNotExist) as e: + template.render() + self.assertEqual(e.exception.args[0], 'missing.html') + + # 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(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + 'OPTIONS': { + # Enable debug, otherwise the exception raised during + # {% include %} processing will be suppressed. + 'debug': True, + } + }]) + def test_extends_include_missing_baseloader(self): + """ + #12787 -- Tests that the correct template is identified as not existing + when {% extends %} specifies a template that does exist, but that + template has an {% include %} of something that does not exist. + """ + template = engines['django'].get_template('test_extends_error.html') + with self.assertRaises(TemplateDoesNotExist) as e: + template.render() + self.assertEqual(e.exception.args[0], 'missing.html') + + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'debug': True, + 'loaders': [ + ('django.template.loaders.cached.Loader', [ + 'django.template.loaders.app_directories.Loader', + ]), + ], + }, + }]) + def test_extends_include_missing_cachedloader(self): + """ + Test the cache loader separately since it overrides load_template. + """ + + template = engines['django'].get_template('test_extends_error.html') + with self.assertRaises(TemplateDoesNotExist) as e: + template.render() + self.assertEqual(e.exception.args[0], 'missing.html') + + # Repeat to ensure it still works when loading from the cache + template = engines['django'].get_template('test_extends_error.html') + with self.assertRaises(TemplateDoesNotExist) as e: + template.render() + self.assertEqual(e.exception.args[0], 'missing.html') + + def test_include_template_argument(self): + """ + Support any render() supporting object + """ + ctx = Context({ + 'tmpl': Template('This worked!'), + }) + outer_tmpl = Template('{% include tmpl %}') + output = outer_tmpl.render(ctx) + self.assertEqual(output, 'This worked!') + + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'debug': True, + }, + }]) + def test_include_immediate_missing(self): + """ + #16417 -- Include tags pointing to missing templates should not raise + an error at parsing time. + """ + template = Template('{% include "this_does_not_exist.html" %}') + self.assertIsInstance(template, Template) + + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + 'OPTIONS': { + 'debug': True, + }, + }]) + def test_include_recursive(self): + comments = [ + { + 'comment': 'A1', + 'children': [ + {'comment': 'B1', 'children': []}, + {'comment': 'B2', 'children': []}, + {'comment': 'B3', 'children': [ + {'comment': 'C1', 'children': []} + ]}, + ] + } + ] + + t = engines['django'].get_template('recursive_include.html') + self.assertEqual( + "Recursion! A1 Recursion! B1 B2 B3 Recursion! C1", + t.render({'comments': comments}).replace(' ', '').replace('\n', ' ').strip(), + ) |
