mirror of
https://github.com/django/django.git
synced 2025-09-17 14:39:17 +00:00
Fixed #36589 -- Made assertTemplateUsed/NotUsed track full path for PartialTemplate.
Previously, assertTemplateUsed only matched partial names, ignoring the template origin. This caused assertions on partials specified by origin ("template.html#partial") to fail. Refs #36410.
This commit is contained in:
parent
6e89271a85
commit
0e0b4214c3
1
AUTHORS
1
AUTHORS
@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better:
|
|||||||
btoll@bestweb.net
|
btoll@bestweb.net
|
||||||
C8E
|
C8E
|
||||||
Caio Ariede <caio.ariede@gmail.com>
|
Caio Ariede <caio.ariede@gmail.com>
|
||||||
|
Caitie Baca <caitlin.baca@yahoo.com>
|
||||||
Calvin Spealman <ironfroggy@gmail.com>
|
Calvin Spealman <ironfroggy@gmail.com>
|
||||||
Cameron Curry
|
Cameron Curry
|
||||||
Cameron Knight (ckknight)
|
Cameron Knight (ckknight)
|
||||||
|
@ -43,6 +43,7 @@ from django.db.backends.base.base import NO_DB_ALIAS, BaseDatabaseWrapper
|
|||||||
from django.forms.fields import CharField
|
from django.forms.fields import CharField
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
from django.http.request import split_domain_port, validate_host
|
from django.http.request import split_domain_port, validate_host
|
||||||
|
from django.template.base import PartialTemplate
|
||||||
from django.test.client import AsyncClient, Client
|
from django.test.client import AsyncClient, Client
|
||||||
from django.test.html import HTMLParseError, parse_html
|
from django.test.html import HTMLParseError, parse_html
|
||||||
from django.test.signals import template_rendered
|
from django.test.signals import template_rendered
|
||||||
@ -138,10 +139,22 @@ class _AssertTemplateUsedContext:
|
|||||||
self.rendered_templates.append(template)
|
self.rendered_templates.append(template)
|
||||||
self.context.append(copy(context))
|
self.context.append(copy(context))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def rendered_template_names(self):
|
||||||
|
return [
|
||||||
|
(
|
||||||
|
f"{t.origin.template_name}#{t.name}"
|
||||||
|
if isinstance(t, PartialTemplate)
|
||||||
|
else t.name
|
||||||
|
)
|
||||||
|
for t in self.rendered_templates
|
||||||
|
if t.name is not None
|
||||||
|
]
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
self.test_case._assert_template_used(
|
self.test_case._assert_template_used(
|
||||||
self.template_name,
|
self.template_name,
|
||||||
[t.name for t in self.rendered_templates if t.name is not None],
|
self.rendered_template_names,
|
||||||
self.msg_prefix,
|
self.msg_prefix,
|
||||||
self.count,
|
self.count,
|
||||||
)
|
)
|
||||||
@ -159,11 +172,8 @@ class _AssertTemplateUsedContext:
|
|||||||
|
|
||||||
class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
|
class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
|
||||||
def test(self):
|
def test(self):
|
||||||
rendered_template_names = [
|
|
||||||
t.name for t in self.rendered_templates if t.name is not None
|
|
||||||
]
|
|
||||||
self.test_case.assertFalse(
|
self.test_case.assertFalse(
|
||||||
self.template_name in rendered_template_names,
|
self.template_name in self.rendered_template_names,
|
||||||
f"{self.msg_prefix}Template '{self.template_name}' was used "
|
f"{self.msg_prefix}Template '{self.template_name}' was used "
|
||||||
f"unexpectedly in rendering the response",
|
f"unexpectedly in rendering the response",
|
||||||
)
|
)
|
||||||
|
3
tests/test_utils/templates/template_used/partials.html
Normal file
3
tests/test_utils/templates/template_used/partials.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{% partialdef hello %}
|
||||||
|
<p>Hello</p>
|
||||||
|
{% endpartialdef %}
|
@ -646,6 +646,53 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase):
|
|||||||
self.assertTemplateNotUsed(response, "template.html")
|
self.assertTemplateNotUsed(response, "template.html")
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(ROOT_URLCONF="test_utils.urls")
|
||||||
|
class AssertTemplateUsedPartialTests(SimpleTestCase):
|
||||||
|
def test_template_used_pass(self):
|
||||||
|
with self.assertTemplateUsed("template_used/partials.html#hello"):
|
||||||
|
render_to_string("template_used/partials.html#hello")
|
||||||
|
|
||||||
|
def test_template_not_used_pass(self):
|
||||||
|
with self.assertTemplateNotUsed("hello"):
|
||||||
|
render_to_string("template_used/partials.html#hello")
|
||||||
|
|
||||||
|
def test_template_used_fail(self):
|
||||||
|
msg = "Template 'hello' was not a template used to render the response."
|
||||||
|
with (
|
||||||
|
self.assertRaisesMessage(AssertionError, msg),
|
||||||
|
self.assertTemplateUsed("hello"),
|
||||||
|
):
|
||||||
|
render_to_string("template_used/base.html")
|
||||||
|
|
||||||
|
def test_template_not_used_fail(self):
|
||||||
|
msg = (
|
||||||
|
"Template 'template_used/partials.html#hello' was used "
|
||||||
|
"unexpectedly in rendering the response"
|
||||||
|
)
|
||||||
|
with (
|
||||||
|
self.assertRaisesMessage(AssertionError, msg),
|
||||||
|
self.assertTemplateNotUsed("template_used/partials.html#hello"),
|
||||||
|
):
|
||||||
|
render_to_string("template_used/partials.html#hello")
|
||||||
|
|
||||||
|
def test_template_not_used_pass_non_partial(self):
|
||||||
|
with self.assertTemplateNotUsed(
|
||||||
|
"template_used/base.html#template_used/base.html"
|
||||||
|
):
|
||||||
|
render_to_string("template_used/base.html")
|
||||||
|
|
||||||
|
def test_template_used_fail_non_partial(self):
|
||||||
|
msg = (
|
||||||
|
"Template 'template_used/base.html#template_used/base.html' was not a "
|
||||||
|
"template used to render the response."
|
||||||
|
)
|
||||||
|
with (
|
||||||
|
self.assertRaisesMessage(AssertionError, msg),
|
||||||
|
self.assertTemplateUsed("template_used/base.html#template_used/base.html"),
|
||||||
|
):
|
||||||
|
render_to_string("template_used/base.html")
|
||||||
|
|
||||||
|
|
||||||
class HTMLEqualTests(SimpleTestCase):
|
class HTMLEqualTests(SimpleTestCase):
|
||||||
def test_html_parser(self):
|
def test_html_parser(self):
|
||||||
element = parse_html("<div><p>Hello</p></div>")
|
element = parse_html("<div><p>Hello</p></div>")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user