diff options
| author | Preston Timmons <prestontimmons@gmail.com> | 2015-04-11 19:41:45 -0400 |
|---|---|---|
| committer | Preston Timmons <prestontimmons@gmail.com> | 2015-04-20 09:11:37 -0500 |
| commit | fb267a1d85c22924231be8cec6c58c42ae57913f (patch) | |
| tree | d85f9285bea1c37d7af83ebc5c84996d2e9d51dd /tests/template_tests/syntax_tests | |
| parent | d84f01ff08922c70f9bb0861846c8ace0764b2dc (diff) | |
Updated template tests to create their own engine.
This continues work to treat Django templates as a library.
Diffstat (limited to 'tests/template_tests/syntax_tests')
| -rw-r--r-- | tests/template_tests/syntax_tests/test_cache.py | 15 | ||||
| -rw-r--r-- | tests/template_tests/syntax_tests/test_if_changed.py | 14 | ||||
| -rw-r--r-- | tests/template_tests/syntax_tests/test_include.py | 89 |
3 files changed, 45 insertions, 73 deletions
diff --git a/tests/template_tests/syntax_tests/test_cache.py b/tests/template_tests/syntax_tests/test_cache.py index 1742b7111c..f088510d5d 100644 --- a/tests/template_tests/syntax_tests/test_cache.py +++ b/tests/template_tests/syntax_tests/test_cache.py @@ -1,5 +1,5 @@ from django.core.cache import cache -from django.template import Context, Template, TemplateSyntaxError +from django.template import Context, Engine, TemplateSyntaxError from django.test import SimpleTestCase, override_settings from ..utils import setup @@ -119,8 +119,13 @@ class CacheTagTests(SimpleTestCase): class CacheTests(SimpleTestCase): + @classmethod + def setUpClass(cls): + cls.engine = Engine() + super(CacheTests, cls).setUpClass() + def test_cache_regression_20130(self): - t = Template('{% load cache %}{% cache 1 regression_20130 %}foo{% endcache %}') + t = self.engine.from_string('{% load cache %}{% cache 1 regression_20130 %}foo{% endcache %}') cachenode = t.nodelist[1] self.assertEqual(cachenode.fragment_name, 'regression_20130') @@ -139,8 +144,8 @@ class CacheTests(SimpleTestCase): When a cache called "template_fragments" is present, the cache tag will use it in preference to 'default' """ - t1 = Template('{% load cache %}{% cache 1 fragment %}foo{% endcache %}') - t2 = Template('{% load cache %}{% cache 1 fragment using="default" %}bar{% endcache %}') + t1 = self.engine.from_string('{% load cache %}{% cache 1 fragment %}foo{% endcache %}') + t2 = self.engine.from_string('{% load cache %}{% cache 1 fragment using="default" %}bar{% endcache %}') ctx = Context() o1 = t1.render(ctx) @@ -154,7 +159,7 @@ class CacheTests(SimpleTestCase): When a cache that doesn't exist is specified, the cache tag will raise a TemplateSyntaxError '""" - t = Template('{% load cache %}{% cache 1 backend using="unknown" %}bar{% endcache %}') + t = self.engine.from_string('{% load cache %}{% cache 1 backend using="unknown" %}bar{% endcache %}') ctx = Context() with self.assertRaises(TemplateSyntaxError): diff --git a/tests/template_tests/syntax_tests/test_if_changed.py b/tests/template_tests/syntax_tests/test_if_changed.py index b06c0bbdd9..62f9563020 100644 --- a/tests/template_tests/syntax_tests/test_if_changed.py +++ b/tests/template_tests/syntax_tests/test_if_changed.py @@ -1,4 +1,4 @@ -from django.template import Context, Template +from django.template import Context, Engine from django.test import SimpleTestCase from ..utils import setup @@ -157,11 +157,19 @@ class IfChangedTagTests(SimpleTestCase): class IfChangedTests(SimpleTestCase): + @classmethod + def setUpClass(cls): + cls.engine = Engine() + super(IfChangedTests, cls).setUpClass() + def test_ifchanged_concurrency(self): """ #15849 -- ifchanged should be thread-safe. """ - template = Template('[0{% for x in foo %},{% with var=get_value %}{% ifchanged %}{{ var }}{% endifchanged %}{% endwith %}{% endfor %}]') + template = self.engine.from_string( + '[0{% for x in foo %},{% with var=get_value %}{% ifchanged %}' + '{{ var }}{% endifchanged %}{% endwith %}{% endfor %}]' + ) # Using generator to mimic concurrency. # The generator is not passed to the 'for' loop, because it does a list(values) @@ -184,6 +192,6 @@ class IfChangedTests(SimpleTestCase): """ #19890. The content of ifchanged template tag was rendered twice. """ - template = Template('{% ifchanged %}{% cycle "1st time" "2nd time" %}{% endifchanged %}') + template = self.engine.from_string('{% ifchanged %}{% cycle "1st time" "2nd time" %}{% endifchanged %}') output = template.render(Context({})) self.assertEqual(output, '1st time') diff --git a/tests/template_tests/syntax_tests/test_include.py b/tests/template_tests/syntax_tests/test_include.py index ecd3d59c29..5a05b32c33 100644 --- a/tests/template_tests/syntax_tests/test_include.py +++ b/tests/template_tests/syntax_tests/test_include.py @@ -1,7 +1,7 @@ from django.template import ( - Context, Template, TemplateDoesNotExist, TemplateSyntaxError, engines, + Context, Engine, TemplateDoesNotExist, TemplateSyntaxError, ) -from django.test import SimpleTestCase, override_settings +from django.test import SimpleTestCase from ..utils import setup from .test_basic import basic_templates @@ -205,110 +205,69 @@ class IncludeTagTests(SimpleTestCase): 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') + engine = Engine(app_dirs=True, debug=True) + template = engine.get_template('test_include_error.html') with self.assertRaises(TemplateDoesNotExist) as e: - template.render() + template.render(Context()) 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') + engine = Engine(app_dirs=True, debug=True) + template = engine.get_template('test_extends_error.html') with self.assertRaises(TemplateDoesNotExist) as e: - template.render() + template.render(Context()) 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. """ + engine = Engine(debug=True, loaders=[ + ('django.template.loaders.cached.Loader', [ + 'django.template.loaders.app_directories.Loader', + ]), + ]) - template = engines['django'].get_template('test_extends_error.html') + template = engine.get_template('test_extends_error.html') with self.assertRaises(TemplateDoesNotExist) as e: - template.render() + template.render(Context()) 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') + template = engine.get_template('test_extends_error.html') with self.assertRaises(TemplateDoesNotExist) as e: - template.render() + template.render(Context()) self.assertEqual(e.exception.args[0], 'missing.html') def test_include_template_argument(self): """ Support any render() supporting object """ + engine = Engine() ctx = Context({ - 'tmpl': Template('This worked!'), + 'tmpl': engine.from_string('This worked!'), }) - outer_tmpl = Template('{% include tmpl %}') + outer_tmpl = engine.from_string('{% 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) + Engine(debug=True).from_string('{% include "this_does_not_exist.html" %}') - @override_settings(TEMPLATES=[{ - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'APP_DIRS': True, - 'OPTIONS': { - 'debug': True, - }, - }]) def test_include_recursive(self): comments = [ { @@ -322,9 +281,9 @@ class IncludeTests(SimpleTestCase): ] } ] - - t = engines['django'].get_template('recursive_include.html') + engine = Engine(app_dirs=True) + t = engine.get_template('recursive_include.html') self.assertEqual( "Recursion! A1 Recursion! B1 B2 B3 Recursion! C1", - t.render({'comments': comments}).replace(' ', '').replace('\n', ' ').strip(), + t.render(Context({'comments': comments})).replace(' ', '').replace('\n', ' ').strip(), ) |
