1
0
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:
Caitie Baca 2025-09-11 13:06:47 -07:00 committed by Jacob Walls
parent 6e89271a85
commit 0e0b4214c3
4 changed files with 66 additions and 5 deletions

View File

@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better:
btoll@bestweb.net
C8E
Caio Ariede <caio.ariede@gmail.com>
Caitie Baca <caitlin.baca@yahoo.com>
Calvin Spealman <ironfroggy@gmail.com>
Cameron Curry
Cameron Knight (ckknight)

View File

@ -43,6 +43,7 @@ from django.db.backends.base.base import NO_DB_ALIAS, BaseDatabaseWrapper
from django.forms.fields import CharField
from django.http import QueryDict
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.html import HTMLParseError, parse_html
from django.test.signals import template_rendered
@ -138,10 +139,22 @@ class _AssertTemplateUsedContext:
self.rendered_templates.append(template)
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):
self.test_case._assert_template_used(
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.count,
)
@ -159,11 +172,8 @@ class _AssertTemplateUsedContext:
class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
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.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"unexpectedly in rendering the response",
)

View File

@ -0,0 +1,3 @@
{% partialdef hello %}
<p>Hello</p>
{% endpartialdef %}

View File

@ -646,6 +646,53 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase):
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):
def test_html_parser(self):
element = parse_html("<div><p>Hello</p></div>")