Skip to content

Commit d46d903

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

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

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: 32 additions & 21 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,38 @@ 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]),
64-
)
61+
template_name = "patterns/atoms/tags_test_atom/invalid_tags_test_atom.html.fail"
62+
request = RequestFactory().get('/')
63+
64+
# Rendering the template with a non-string `default_html` argument will cause Django >= 4 to raise
65+
# a `TypeError`, which we need to catch and ignore in order to check that the warning is raised
66+
try:
67+
render(request, template_name, context={get_pattern_context_var_name(): True})
68+
except TypeError:
69+
pass
70+
71+
self.assertIn(
72+
"default_html argument to override_tag should be a string to ensure compatibility with Django",
73+
str(cm.warnings[0]),
74+
)
6575

6676
def test_bad_default_html_error(self):
77+
"""
78+
Test that the library raises a TypeError when passing a non-string `default_html` argument to `override_tag`
79+
in Django >= 4.0
80+
"""
6781
with patch("django.VERSION", (4, 2, 0, "final", 0)):
6882
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-
),
74-
)
83+
template_name = "patterns/atoms/tags_test_atom/invalid_tags_test_atom.html.fail"
84+
request = RequestFactory().get('/')
85+
render(request, template_name, context={get_pattern_context_var_name(): True})
7586
self.assertIn("default_html argument to override_tag must be a string", str(cm.exception))

0 commit comments

Comments
 (0)