|
1 | | -from django.test import SimpleTestCase |
2 | 1 | from unittest.mock import patch |
3 | 2 |
|
| 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 | + |
4 | 8 | from .utils import reverse |
5 | 9 |
|
6 | 10 |
|
@@ -45,31 +49,38 @@ def test_falsey_default_html_overide(self): |
45 | 49 | self.assertContains(response, "POTANoneTO2") |
46 | 50 | self.assertContains(response, "POTA0TO3") |
47 | 51 |
|
| 52 | + |
| 53 | +class TagsTestFailCase(SimpleTestCase): |
48 | 54 | 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 | + """ |
49 | 59 | with patch("django.VERSION", (3, 2, 0, "final", 0)): |
50 | 60 | 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 | + ) |
65 | 75 |
|
66 | 76 | 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 | + """ |
67 | 81 | with patch("django.VERSION", (4, 2, 0, "final", 0)): |
68 | 82 | 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}) |
75 | 86 | self.assertIn("default_html argument to override_tag must be a string", str(cm.exception)) |
0 commit comments