Skip to content

Commit 93473f1

Browse files
committed
Add tests for handling non-strings in 'override_tag's 'default_html' parameter
1 parent a03ed78 commit 93473f1

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% load test_tags_invalid %}
2+
3+
MARMA{% default_html_tag_invalid empty_string %}LADE01
4+
MARMA{% default_html_tag_invalid none %}LADE02
5+
MARMA{% default_html_tag_invalid dict %}LADE03
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
context:
2+
test_invalid_tags: False
3+
tags:
4+
default_html_tag_invalid:
5+
empty_string:
6+
raw: ''
7+
none:
8+
raw: None
9+
dict:
10+
zero: 0
11+
two: 2
12+
five: 5
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django import template
2+
3+
from pattern_library.monkey_utils import override_tag
4+
5+
register = template.Library()
6+
7+
8+
@register.simple_tag()
9+
def default_html_tag_invalid(arg=None):
10+
"Just pass, never do anything"
11+
pass
12+
13+
14+
# Test overriding tag with a default_html that's not valid in Django >= 4.0
15+
override_tag(register, "default_html_tag_invalid", default_html=[1, 2, 3])

tests/tests/test_commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ 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
1920
patterns/atoms/test_atom/test_atom.html
2021
""",
2122
stderr.getvalue(),

tests/tests/test_tags.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.test import SimpleTestCase
2+
from unittest.mock import patch
23

34
from .utils import reverse
45

@@ -43,3 +44,32 @@ def test_falsey_default_html_overide(self):
4344
self.assertContains(response, "POTATO1")
4445
self.assertContains(response, "POTANoneTO2")
4546
self.assertContains(response, "POTA0TO3")
47+
48+
def test_bad_default_html_warning(self):
49+
with patch("django.VERSION", (3, 2, 0, "final", 0)):
50+
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+
)
65+
66+
def test_bad_default_html_error(self):
67+
with patch("django.VERSION", (4, 2, 0, "final", 0)):
68+
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+
)
75+
self.assertIn("default_html argument to override_tag must be a string", str(cm.exception))

0 commit comments

Comments
 (0)