Opened 3 weeks ago

Closed 8 days ago

Last modified 8 days ago

#36589 closed Bug (fixed)

assertTemplateUsed asserts against template partial name regardless of origin

Reported by: Jacob Walls Owned by: Caitlin B
Component: Testing framework Version: dev
Severity: Release blocker Keywords:
Cc: Farhan Ali, Carlton Gibson Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, you can provide a named template partial like "hello" to assertTemplateUsed, which scrupulously complies with its doc'd intention to assert against names, but I can imagine wanting to provide the "qualified" path like my_page.html#hello:

diff --git a/tests/test_utils/templates/template_used/partials.html b/tests/test_utils/templates/template_used/partials.html
new file mode 100644
index 0000000000..5c8db3d657
--- /dev/null
+++ b/tests/test_utils/templates/template_used/partials.html
@@ -0,0 +1,5 @@
+{% partialdef hello %}
+<p>Hello</p>
+{% endpartialdef %}

diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
index 9c22b61b4f..f7500ddc22 100644
--- a/tests/test_utils/tests.py
+++ b/tests/test_utils/tests.py
@@ -510,6 +510,14 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase):
             render_to_string("template_used/base.html")
             render_to_string("template_used/base.html")
 
+        # passes
+        with self.assertTemplateUsed("hello"):
+            render_to_string("template_used/partials.html#hello")
+
+        # fails
+        with self.assertTemplateUsed("template_used/partials.html#hello"):
+            render_to_string("template_used/partials.html#hello")
+
     def test_nested_usage(self):
         with self.assertTemplateUsed("template_used/base.html"):
             with self.assertTemplateUsed("template_used/include.html"):

Notice the hint "Actual template(s) used: hello" does not point to the origin, really.

AssertionError: False is not true : Template 'template_used/partials.html#hello' was not a template used to render the response. Actual template(s) used: hello

And if we regard the unqualified way as dubious, I'm suggesting we should yank it before 6.0.

The fix doesn't look to be invasive. Curious to hear from others.

Change History (9)

comment:1 by Jacob Walls, 3 weeks ago

Component: Template systemTesting framework

comment:2 by Natalia Bidart, 3 weeks ago

Triage Stage: UnreviewedAccepted

Hey Jacob, thanks for the report! I have reproduced how assertTemplateUsed only matches the partial name (hello) and ignores the qualified path (template_used/partials.html#hello).
I agree that using template_used/partials.html#hello should provide a passing test assertion.

comment:3 by Caitlin B, 12 days ago

Owner: set to Caitlin B
Status: newassigned

comment:4 by Caitlin B, 12 days ago

Has patch: set

comment:5 by Jacob Walls, 8 days ago

Triage Stage: AcceptedReady for checkin

comment:6 by Natalia Bidart, 8 days ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

Added some commentary about a potential issue with the code and the need to use subTest when asserting over multiple things in the same unit test.

comment:7 by Jacob Walls, 8 days ago

Patch needs improvement: unset

comment:8 by Jacob Walls <jacobtylerwalls@…>, 8 days ago

Resolution: fixed
Status: assignedclosed

In 0e0b4214:

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.

comment:9 by Jacob Walls, 8 days ago

Triage Stage: AcceptedReady for checkin
Note: See TracTickets for help on using tickets.
Back to Top