2009-03-31 07:23:50 +00:00
|
|
|
import sys
|
2007-07-23 04:45:01 +00:00
|
|
|
|
2021-05-07 20:49:07 +00:00
|
|
|
from django.template import Context, Engine, TemplateDoesNotExist, TemplateSyntaxError
|
2015-03-03 21:48:26 +00:00
|
|
|
from django.template.base import UNKNOWN_SOURCE
|
2015-02-21 19:15:22 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-12-30 15:51:16 +00:00
|
|
|
from django.urls import NoReverseMatch
|
2016-04-03 08:02:20 +00:00
|
|
|
from django.utils import translation
|
2021-05-07 20:49:07 +00:00
|
|
|
from django.utils.html import escape
|
2014-12-17 21:10:57 +00:00
|
|
|
|
|
|
|
|
2021-08-06 08:09:31 +00:00
|
|
|
class TemplateTestMixin:
|
|
|
|
def _engine(self, **kwargs):
|
|
|
|
return Engine(debug=self.debug_engine, **kwargs)
|
2013-08-30 19:08:40 +00:00
|
|
|
|
|
|
|
def test_string_origin(self):
|
2021-08-06 08:09:31 +00:00
|
|
|
template = self._engine().from_string("string template")
|
2015-03-03 21:48:26 +00:00
|
|
|
self.assertEqual(template.origin.name, UNKNOWN_SOURCE)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(template.origin.loader_name)
|
2015-03-03 21:48:26 +00:00
|
|
|
self.assertEqual(template.source, "string template")
|
2013-08-30 19:08:40 +00:00
|
|
|
|
2015-04-11 23:41:45 +00:00
|
|
|
@override_settings(SETTINGS_MODULE=None)
|
2009-04-01 22:46:46 +00:00
|
|
|
def test_url_reverse_no_settings_module(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
"""
|
|
|
|
#9005 -- url tag shouldn't require settings.SETTINGS_MODULE to
|
|
|
|
be set.
|
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
t = self._engine().from_string("{% url will_not_match %}")
|
2009-04-01 22:46:46 +00:00
|
|
|
c = Context()
|
2015-12-30 15:51:16 +00:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
2011-09-16 01:16:25 +00:00
|
|
|
t.render(c)
|
|
|
|
|
2013-02-23 16:41:30 +00:00
|
|
|
def test_url_reverse_view_name(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
"""
|
|
|
|
#19827 -- url tag should keep original strack trace when reraising
|
|
|
|
exception.
|
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
t = self._engine().from_string("{% url will_not_match %}")
|
2013-02-23 16:41:30 +00:00
|
|
|
c = Context()
|
|
|
|
try:
|
|
|
|
t.render(c)
|
2015-12-30 15:51:16 +00:00
|
|
|
except NoReverseMatch:
|
2013-02-23 16:41:30 +00:00
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
depth = 0
|
|
|
|
while tb.tb_next is not None:
|
|
|
|
tb = tb.tb_next
|
|
|
|
depth += 1
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertGreater(
|
|
|
|
depth, 5, "The traceback context was lost when reraising the traceback."
|
|
|
|
)
|
2013-02-23 16:41:30 +00:00
|
|
|
|
2011-09-16 01:16:25 +00:00
|
|
|
def test_no_wrapped_exception(self):
|
|
|
|
"""
|
2015-02-23 21:26:55 +00:00
|
|
|
# 16770 -- The template system doesn't wrap exceptions, but annotates
|
|
|
|
them.
|
2011-09-16 01:16:25 +00:00
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine()
|
2011-09-16 01:16:25 +00:00
|
|
|
c = Context({"coconuts": lambda: 42 / 0})
|
2015-03-06 15:53:25 +00:00
|
|
|
t = engine.from_string("{{ coconuts }}")
|
|
|
|
|
|
|
|
with self.assertRaises(ZeroDivisionError) as e:
|
2011-09-16 01:16:25 +00:00
|
|
|
t.render(c)
|
|
|
|
|
2021-08-06 08:09:31 +00:00
|
|
|
if self.debug_engine:
|
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual(debug["start"], 0)
|
|
|
|
self.assertEqual(debug["end"], 14)
|
2009-04-04 19:34:52 +00:00
|
|
|
|
2010-02-21 23:38:33 +00:00
|
|
|
def test_invalid_block_suggestion(self):
|
2015-02-23 21:26:55 +00:00
|
|
|
"""
|
2016-04-03 08:02:20 +00:00
|
|
|
Error messages should include the unexpected block name and be in all
|
|
|
|
English.
|
2015-02-23 21:26:55 +00:00
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine()
|
2015-10-04 17:50:18 +00:00
|
|
|
msg = (
|
|
|
|
"Invalid block tag on line 1: 'endblock', expected 'elif', 'else' "
|
|
|
|
"or 'endif'. Did you forget to register or load this tag?"
|
|
|
|
)
|
2016-04-03 08:02:20 +00:00
|
|
|
with self.settings(USE_I18N=True), translation.override("de"):
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
engine.from_string("{% if 1 %}lala{% endblock %}{% endif %}")
|
2015-02-23 21:26:55 +00:00
|
|
|
|
2015-10-04 17:50:18 +00:00
|
|
|
def test_unknown_block_tag(self):
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine()
|
2015-10-04 17:50:18 +00:00
|
|
|
msg = (
|
|
|
|
"Invalid block tag on line 1: 'foobar'. Did you forget to "
|
|
|
|
"register or load this tag?"
|
2015-02-23 21:26:55 +00:00
|
|
|
)
|
2015-10-04 17:50:18 +00:00
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
engine.from_string("lala{% foobar %}")
|
2010-02-21 23:38:33 +00:00
|
|
|
|
2015-03-06 15:53:25 +00:00
|
|
|
def test_compile_filter_expression_error(self):
|
|
|
|
"""
|
|
|
|
19819 -- Make sure the correct token is highlighted for
|
|
|
|
FilterExpression errors.
|
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine()
|
2015-03-06 15:53:25 +00:00
|
|
|
msg = "Could not parse the remainder: '@bar' from 'foo@bar'"
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg) as e:
|
|
|
|
engine.from_string("{% if 1 %}{{ foo@bar }}{% endif %}")
|
|
|
|
|
2021-08-06 08:09:31 +00:00
|
|
|
if self.debug_engine:
|
|
|
|
debug = e.exception.template_debug
|
|
|
|
self.assertEqual((debug["start"], debug["end"]), (10, 23))
|
|
|
|
self.assertEqual((debug["during"]), "{{ foo@bar }}")
|
2015-03-06 15:53:25 +00:00
|
|
|
|
|
|
|
def test_compile_tag_error(self):
|
|
|
|
"""
|
|
|
|
Errors raised while compiling nodes should include the token
|
|
|
|
information.
|
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine(
|
2015-05-08 20:10:36 +00:00
|
|
|
libraries={"bad_tag": "template_tests.templatetags.bad_tag"},
|
|
|
|
)
|
2015-03-06 15:53:25 +00:00
|
|
|
with self.assertRaises(RuntimeError) as e:
|
|
|
|
engine.from_string("{% load bad_tag %}{% badtag %}")
|
2021-08-06 08:09:31 +00:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(e.exception.template_debug["during"], "{% badtag %}")
|
2015-03-06 15:53:25 +00:00
|
|
|
|
2016-12-14 02:35:21 +00:00
|
|
|
def test_compile_tag_error_27584(self):
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine(
|
2016-12-14 02:35:21 +00:00
|
|
|
app_dirs=True,
|
|
|
|
libraries={"tag_27584": "template_tests.templatetags.tag_27584"},
|
|
|
|
)
|
|
|
|
t = engine.get_template("27584_parent.html")
|
|
|
|
with self.assertRaises(TemplateSyntaxError) as e:
|
|
|
|
t.render(Context())
|
2021-08-06 08:09:31 +00:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(e.exception.template_debug["during"], "{% badtag %}")
|
2016-12-14 02:35:21 +00:00
|
|
|
|
2017-03-25 12:22:12 +00:00
|
|
|
def test_compile_tag_error_27956(self):
|
|
|
|
"""Errors in a child of {% extends %} are displayed correctly."""
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine(
|
2017-03-25 12:22:12 +00:00
|
|
|
app_dirs=True,
|
|
|
|
libraries={"tag_27584": "template_tests.templatetags.tag_27584"},
|
|
|
|
)
|
|
|
|
t = engine.get_template("27956_child.html")
|
|
|
|
with self.assertRaises(TemplateSyntaxError) as e:
|
|
|
|
t.render(Context())
|
2021-08-06 08:09:31 +00:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(e.exception.template_debug["during"], "{% badtag %}")
|
2017-03-25 12:22:12 +00:00
|
|
|
|
2021-05-07 20:49:07 +00:00
|
|
|
def test_render_tag_error_in_extended_block(self):
|
|
|
|
"""Errors in extended block are displayed correctly."""
|
2021-08-06 08:09:31 +00:00
|
|
|
e = self._engine(app_dirs=True)
|
2021-05-07 20:49:07 +00:00
|
|
|
template = e.get_template("test_extends_block_error.html")
|
|
|
|
context = Context()
|
|
|
|
with self.assertRaises(TemplateDoesNotExist) as cm:
|
|
|
|
template.render(context)
|
2021-08-06 08:09:31 +00:00
|
|
|
if self.debug_engine:
|
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.template_debug["during"],
|
|
|
|
escape('{% include "missing.html" %}'),
|
|
|
|
)
|
2021-05-07 20:49:07 +00:00
|
|
|
|
2013-05-30 07:25:58 +00:00
|
|
|
def test_super_errors(self):
|
|
|
|
"""
|
2015-02-23 21:26:55 +00:00
|
|
|
#18169 -- NoReverseMatch should not be silence in block.super.
|
2013-05-30 07:25:58 +00:00
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine(app_dirs=True)
|
2015-04-11 23:41:45 +00:00
|
|
|
t = engine.get_template("included_content.html")
|
2015-12-30 15:51:16 +00:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
2015-04-11 23:41:45 +00:00
|
|
|
t.render(Context())
|
2013-05-30 07:25:58 +00:00
|
|
|
|
2015-02-14 09:21:06 +00:00
|
|
|
def test_extends_generic_template(self):
|
|
|
|
"""
|
2015-02-23 21:26:55 +00:00
|
|
|
#24338 -- Allow extending django.template.backends.django.Template
|
|
|
|
objects.
|
2015-02-14 09:21:06 +00:00
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
engine = self._engine()
|
2015-04-11 23:41:45 +00:00
|
|
|
parent = engine.from_string("{% block content %}parent{% endblock %}")
|
|
|
|
child = engine.from_string(
|
2015-02-14 09:21:06 +00:00
|
|
|
"{% extends parent %}{% block content %}child{% endblock %}"
|
|
|
|
)
|
2015-04-11 23:41:45 +00:00
|
|
|
self.assertEqual(child.render(Context({"parent": parent})), "child")
|
2016-01-15 02:36:05 +00:00
|
|
|
|
|
|
|
def test_node_origin(self):
|
|
|
|
"""
|
|
|
|
#25848 -- Set origin on Node so debugging tools can determine which
|
|
|
|
template the node came from even if extending or including templates.
|
|
|
|
"""
|
2021-08-06 08:09:31 +00:00
|
|
|
template = self._engine().from_string("content")
|
2016-01-15 02:36:05 +00:00
|
|
|
for node in template.nodelist:
|
|
|
|
self.assertEqual(node.origin, template.origin)
|
2021-08-06 08:09:31 +00:00
|
|
|
|
2022-05-20 04:01:10 +00:00
|
|
|
def test_render_built_in_type_method(self):
|
|
|
|
"""
|
|
|
|
Templates should not crash when rendering methods for built-in types
|
|
|
|
without required arguments.
|
|
|
|
"""
|
|
|
|
template = self._engine().from_string("{{ description.count }}")
|
|
|
|
self.assertEqual(template.render(Context({"description": "test"})), "")
|
|
|
|
|
2021-08-06 08:09:31 +00:00
|
|
|
|
|
|
|
class TemplateTests(TemplateTestMixin, SimpleTestCase):
|
|
|
|
debug_engine = False
|
|
|
|
|
|
|
|
|
|
|
|
class DebugTemplateTests(TemplateTestMixin, SimpleTestCase):
|
|
|
|
debug_engine = True
|