Skip to content

Commit

Permalink
Refs django#10941 -- Added tests in querystring template tag.
Browse files Browse the repository at this point in the history
These extra tests assert over the handling of empty params (None, empty
dict, empty QueryDict), and also for dicts having non-string keys.
  • Loading branch information
nessita authored and ryancheley committed Dec 29, 2024
1 parent e3d2a22 commit e3d88f2
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions tests/template_tests/syntax_tests/test_querystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,38 @@ def assertRenderEqual(self, template_name, context, expected):
output = self.engine.render_to_string(template_name, context)
self.assertEqual(output, expected)

@setup({"querystring_empty": "{% querystring %}"})
def test_querystring_empty(self):
@setup({"test_querystring_empty_get_params": "{% querystring %}"})
def test_querystring_empty_get_params(self):
context = RequestContext(self.request_factory.get("/"))
self.assertRenderEqual("querystring_empty", context, expected="")
self.assertRenderEqual(
"test_querystring_empty_get_params", context, expected=""
)

@setup({"querystring_non_empty": "{% querystring %}"})
def test_querystring_non_empty(self):
@setup({"test_querystring_non_empty_get_params": "{% querystring %}"})
def test_querystring_non_empty_get_params(self):
request = self.request_factory.get("/", {"a": "b"})
context = RequestContext(request)
self.assertRenderEqual("querystring_non_empty", context, expected="?a=b")
self.assertRenderEqual(
"test_querystring_non_empty_get_params", context, expected="?a=b"
)

@setup({"querystring_multiple": "{% querystring %}"})
def test_querystring_multiple(self):
request = self.request_factory.get("/", {"x": "y", "a": "b"})
context = RequestContext(request)
self.assertRenderEqual("querystring_multiple", context, expected="?x=y&a=b")

@setup({"test_querystring_empty_params": "{% querystring qd %}"})
def test_querystring_empty_params(self):
cases = [None, {}, QueryDict()]
request = self.request_factory.get("/")
for param in cases:
with self.subTest(param=param):
context = RequestContext(request, {"qd": param})
self.assertRenderEqual(
"test_querystring_empty_params", context, expected=""
)

@setup({"querystring_replace": "{% querystring a=1 %}"})
def test_querystring_replace(self):
request = self.request_factory.get("/", {"x": "y", "a": "b"})
Expand Down Expand Up @@ -64,6 +79,14 @@ def test_querystring_add_list(self):
context = RequestContext(request, {"my_list": [2, 3]})
self.assertRenderEqual("querystring_list", context, expected="?a=2&a=3")

@setup({"querystring_dict": "{% querystring a=my_dict %}"})
def test_querystring_add_dict(self):
request = self.request_factory.get("/")
context = RequestContext(request, {"my_dict": {i: i * 2 for i in range(3)}})
self.assertRenderEqual(
"querystring_dict", context, expected="?a=0&a=1&a=2"
)

@setup({"querystring_query_dict": "{% querystring request.GET a=2 %}"})
def test_querystring_with_explicit_query_dict(self):
request = self.request_factory.get("/", {"a": 1})
Expand Down

0 comments on commit e3d88f2

Please sign in to comment.