diff --git a/tests/template_tests/syntax_tests/test_querystring.py b/tests/template_tests/syntax_tests/test_querystring.py index 3f1cf3d281d72..a2ceee29c5e00 100644 --- a/tests/template_tests/syntax_tests/test_querystring.py +++ b/tests/template_tests/syntax_tests/test_querystring.py @@ -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})