Skip to content

Commit 0e0b421

Browse files
Caitie Bacajacobtylerwalls
authored andcommitted
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.
1 parent 6e89271 commit 0e0b421

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better:
196196
btoll@bestweb.net
197197
C8E
198198
Caio Ariede <caio.ariede@gmail.com>
199+
Caitie Baca <caitlin.baca@yahoo.com>
199200
Calvin Spealman <ironfroggy@gmail.com>
200201
Cameron Curry
201202
Cameron Knight (ckknight)

django/test/testcases.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from django.forms.fields import CharField
4444
from django.http import QueryDict
4545
from django.http.request import split_domain_port, validate_host
46+
from django.template.base import PartialTemplate
4647
from django.test.client import AsyncClient, Client
4748
from django.test.html import HTMLParseError, parse_html
4849
from django.test.signals import template_rendered
@@ -138,10 +139,22 @@ def on_template_render(self, sender, signal, template, context, **kwargs):
138139
self.rendered_templates.append(template)
139140
self.context.append(copy(context))
140141

142+
@property
143+
def rendered_template_names(self):
144+
return [
145+
(
146+
f"{t.origin.template_name}#{t.name}"
147+
if isinstance(t, PartialTemplate)
148+
else t.name
149+
)
150+
for t in self.rendered_templates
151+
if t.name is not None
152+
]
153+
141154
def test(self):
142155
self.test_case._assert_template_used(
143156
self.template_name,
144-
[t.name for t in self.rendered_templates if t.name is not None],
157+
self.rendered_template_names,
145158
self.msg_prefix,
146159
self.count,
147160
)
@@ -159,11 +172,8 @@ def __exit__(self, exc_type, exc_value, traceback):
159172

160173
class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
161174
def test(self):
162-
rendered_template_names = [
163-
t.name for t in self.rendered_templates if t.name is not None
164-
]
165175
self.test_case.assertFalse(
166-
self.template_name in rendered_template_names,
176+
self.template_name in self.rendered_template_names,
167177
f"{self.msg_prefix}Template '{self.template_name}' was used "
168178
f"unexpectedly in rendering the response",
169179
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% partialdef hello %}
2+
<p>Hello</p>
3+
{% endpartialdef %}

tests/test_utils/tests.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,53 @@ def test_assert_used_on_http_response(self):
646646
self.assertTemplateNotUsed(response, "template.html")
647647

648648

649+
@override_settings(ROOT_URLCONF="test_utils.urls")
650+
class AssertTemplateUsedPartialTests(SimpleTestCase):
651+
def test_template_used_pass(self):
652+
with self.assertTemplateUsed("template_used/partials.html#hello"):
653+
render_to_string("template_used/partials.html#hello")
654+
655+
def test_template_not_used_pass(self):
656+
with self.assertTemplateNotUsed("hello"):
657+
render_to_string("template_used/partials.html#hello")
658+
659+
def test_template_used_fail(self):
660+
msg = "Template 'hello' was not a template used to render the response."
661+
with (
662+
self.assertRaisesMessage(AssertionError, msg),
663+
self.assertTemplateUsed("hello"),
664+
):
665+
render_to_string("template_used/base.html")
666+
667+
def test_template_not_used_fail(self):
668+
msg = (
669+
"Template 'template_used/partials.html#hello' was used "
670+
"unexpectedly in rendering the response"
671+
)
672+
with (
673+
self.assertRaisesMessage(AssertionError, msg),
674+
self.assertTemplateNotUsed("template_used/partials.html#hello"),
675+
):
676+
render_to_string("template_used/partials.html#hello")
677+
678+
def test_template_not_used_pass_non_partial(self):
679+
with self.assertTemplateNotUsed(
680+
"template_used/base.html#template_used/base.html"
681+
):
682+
render_to_string("template_used/base.html")
683+
684+
def test_template_used_fail_non_partial(self):
685+
msg = (
686+
"Template 'template_used/base.html#template_used/base.html' was not a "
687+
"template used to render the response."
688+
)
689+
with (
690+
self.assertRaisesMessage(AssertionError, msg),
691+
self.assertTemplateUsed("template_used/base.html#template_used/base.html"),
692+
):
693+
render_to_string("template_used/base.html")
694+
695+
649696
class HTMLEqualTests(SimpleTestCase):
650697
def test_html_parser(self):
651698
element = parse_html("<div><p>Hello</p></div>")

0 commit comments

Comments
 (0)