Skip to content

Commit f03ec89

Browse files
committed
Fix tests for handling invalid default_html argument to override_tag
1 parent 141ca98 commit f03ec89

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

pattern_library/monkey_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def node_render(context):
9191
if django.VERSION < (4, 0):
9292
warnings.warn(
9393
"default_html argument to override_tag should be a string to ensure compatibility "
94-
'with Django >= 4.0 (line %s in "%s")' % (trace.lineno, trace.filename),
94+
'with Django >= 4.0 (line %s in "%s")'
95+
% (trace.lineno, trace.filename),
9596
Warning,
9697
)
9798
else:

tests/templates/patterns/atoms/tags_test_atom/invalid_tags_test_atom.yaml

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/tests/test_commands.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def test_displays_patterns(self):
1616
call_command("render_patterns", dry_run=True, stdout=stdout, stderr=stderr)
1717
self.assertIn(
1818
"""patterns/atoms/tags_test_atom/tags_test_atom.html
19-
patterns/atoms/tags_test_atom/invalid_tags_test_atom.html
2019
patterns/atoms/test_atom/test_atom.html
2120
""",
2221
stderr.getvalue(),

tests/tests/test_tags.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
from django.test import SimpleTestCase
21
from unittest.mock import patch
32

3+
from django.shortcuts import render
4+
from django.test import RequestFactory, SimpleTestCase
5+
6+
from pattern_library import get_pattern_context_var_name
7+
48
from .utils import reverse
59

610

@@ -45,31 +49,53 @@ def test_falsey_default_html_overide(self):
4549
self.assertContains(response, "POTANoneTO2")
4650
self.assertContains(response, "POTA0TO3")
4751

52+
53+
class TagsTestFailCase(SimpleTestCase):
4854
def test_bad_default_html_warning(self):
55+
"""
56+
Test that the library raises a warning when passing a non-string `default_html` argument to `override_tag`
57+
in Django < 4.0
58+
"""
4959
with patch("django.VERSION", (3, 2, 0, "final", 0)):
5060
with self.assertWarns(Warning) as cm:
51-
response = self.client.get(
52-
reverse(
53-
"pattern_library:render_pattern",
54-
kwargs={
55-
"pattern_template_name": "patterns/atoms/tags_test_atom/invalid_tags_test_atom.html",
56-
},
57-
),
58-
)
59-
self.assertContains(response, "MARMALADE01")
60-
self.assertContains(response, "MARMANoneLADE02")
61-
self.assertIn(
62-
"default_html argument to override_tag should be a string to ensure compatibility with Django",
63-
str(cm.warnings[0]),
61+
template_name = (
62+
"patterns/atoms/tags_test_atom/invalid_tags_test_atom.html.fail"
6463
)
64+
request = RequestFactory().get("/")
65+
66+
# Rendering the template with a non-string `default_html` argument will cause Django >= 4 to raise
67+
# a `TypeError`, which we need to catch and ignore in order to check that the warning is raised
68+
try:
69+
render(
70+
request,
71+
template_name,
72+
context={get_pattern_context_var_name(): True},
73+
)
74+
except TypeError:
75+
pass
76+
77+
self.assertIn(
78+
"default_html argument to override_tag should be a string to ensure compatibility with Django",
79+
str(cm.warnings[0]),
80+
)
6581

6682
def test_bad_default_html_error(self):
83+
"""
84+
Test that the library raises a TypeError when passing a non-string `default_html` argument to `override_tag`
85+
in Django >= 4.0
86+
"""
6787
with patch("django.VERSION", (4, 2, 0, "final", 0)):
6888
with self.assertRaises(TypeError) as cm:
69-
self.client.get(
70-
reverse(
71-
"pattern_library:render_pattern",
72-
kwargs={"pattern_template_name": "patterns/atoms/tags_test_atom/invalid_tags_test_atom.html"},
73-
),
89+
template_name = (
90+
"patterns/atoms/tags_test_atom/invalid_tags_test_atom.html.fail"
7491
)
75-
self.assertIn("default_html argument to override_tag must be a string", str(cm.exception))
92+
request = RequestFactory().get("/")
93+
render(
94+
request,
95+
template_name,
96+
context={get_pattern_context_var_name(): True},
97+
)
98+
self.assertIn(
99+
"default_html argument to override_tag must be a string",
100+
str(cm.exception),
101+
)

0 commit comments

Comments
 (0)