Skip to content

Commit

Permalink
Refs django#10941 -- Added tests for querystring template tag to hand…
Browse files Browse the repository at this point in the history
…le empty params.
  • Loading branch information
nessita committed Aug 16, 2024
1 parent a57596e commit 0db2b42
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions tests/template_tests/syntax_tests/test_querystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,77 @@


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({"test_querystring_empty_dict": "{% querystring qd %}"})
def test_querystring_empty_dict(self):
output = self.assertRenderEqual("test_querystring_empty_dict", qd={})
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_query_dict": "{% querystring request.GET a=2 %}"})
Expand Down

0 comments on commit 0db2b42

Please sign in to comment.