Skip to content

Commit

Permalink
Refs django#10941 -- Added extra tests for 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 committed Aug 16, 2024
1 parent a57596e commit 946cefc
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions tests/template_tests/syntax_tests/test_querystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,81 @@


class QueryStringTagTests(SimpleTestCase):
def setUp(self):
self.request_factory = RequestFactory()

@setup({"querystring_empty": "{% querystring %}"})
def test_querystring_empty(self):
request = self.request_factory.get("/")
template = self.engine.get_template("querystring_empty")
context = RequestContext(request)
output = template.render(context)

request_factory = RequestFactory()

def assertRenderEqual(self, template_name, request=None, **context):
if request is None:
request = self.request_factory.get("/")
template = self.engine.get_template(template_name)
context = RequestContext(request, context)
return template.render(context)

@setup({"test_querystring_empty_get_params": "{% querystring %}"})
def test_querystring_empty_get_params(self):
output = self.assertRenderEqual("test_querystring_empty_get_params")
self.assertEqual(output, "")

@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"})
template = self.engine.get_template("querystring_non_empty")
context = RequestContext(request)
output = template.render(context)
output = self.assertRenderEqual(
"test_querystring_non_empty_get_params", request
)
self.assertEqual(output, "?a=b")

@setup({"test_querystring_empty_params": "{% querystring qd %}"})
def test_querystring_empty_params(self):
cases = [None, {}, QueryDict()]
for param in cases:
with self.subTest(param=param):
output = self.assertRenderEqual(
"test_querystring_empty_params", qd=param
)
self.assertEqual(output, "")

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

@setup({"querystring_replace": "{% querystring a=1 %}"})
def test_querystring_replace(self):
request = self.request_factory.get("/", {"x": "y", "a": "b"})
template = self.engine.get_template("querystring_replace")
context = RequestContext(request)
output = template.render(context)
output = self.assertRenderEqual("querystring_replace", request)
self.assertEqual(output, "?x=y&a=1")

@setup({"querystring_add": "{% querystring test_new='something' %}"})
def test_querystring_add(self):
request = self.request_factory.get("/", {"a": "b"})
template = self.engine.get_template("querystring_add")
context = RequestContext(request)
output = template.render(context)
output = self.assertRenderEqual("querystring_add", request)
self.assertEqual(output, "?a=b&test_new=something")

@setup({"querystring_remove": "{% querystring test=None a=1 %}"})
def test_querystring_remove(self):
request = self.request_factory.get("/", {"test": "value", "a": "1"})
template = self.engine.get_template("querystring_remove")
context = RequestContext(request)
output = template.render(context)
output = self.assertRenderEqual("querystring_remove", request)
self.assertEqual(output, "?a=1")

@setup({"querystring_remove_nonexistent": "{% querystring nonexistent=None a=1 %}"})
def test_querystring_remove_nonexistent(self):
request = self.request_factory.get("/", {"x": "y", "a": "1"})
template = self.engine.get_template("querystring_remove_nonexistent")
context = RequestContext(request)
output = template.render(context)
output = self.assertRenderEqual("querystring_remove_nonexistent", request)
self.assertEqual(output, "?x=y&a=1")

@setup({"querystring_list": "{% querystring a=my_list %}"})
def test_querystring_add_list(self):
request = self.request_factory.get("/")
template = self.engine.get_template("querystring_list")
context = RequestContext(request, {"my_list": [2, 3]})
output = template.render(context)
output = self.assertRenderEqual("querystring_list", my_list=[2, 3])
self.assertEqual(output, "?a=2&a=3")

@setup({"querystring_dict": "{% querystring a=my_dict %}"})
def test_querystring_add_dict(self):
output = self.assertRenderEqual(
"querystring_dict", my_dict={i: i * 2 for i in range(3)}
)
self.assertEqual(output, "?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 946cefc

Please sign in to comment.