Skip to content

Commit

Permalink
Refs django#10941 -- Added helper and refactored tests for querystrin…
Browse files Browse the repository at this point in the history
…g template tag.
  • Loading branch information
nessita committed Aug 20, 2024
1 parent ba46b09 commit 243ed5a
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions tests/template_tests/syntax_tests/test_querystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,61 @@


class QueryStringTagTests(SimpleTestCase):
def setUp(self):
self.request_factory = RequestFactory()
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)
def assertRenderEqual(self, template_name, request=None, expected="", **context):
if request is None:
request = self.request_factory.get("/")
template = self.engine.get_template(template_name)
context = RequestContext(request, context)
output = template.render(context)
self.assertEqual(output, "")
self.assertEqual(output, expected)

@setup({"querystring_non_empty": "{% querystring %}"})
def test_querystring_non_empty(self):
@setup({"test_querystring_empty_get_params": "{% querystring %}"})
def test_querystring_empty_get_params(self):
self.assertRenderEqual("test_querystring_empty_get_params", expected="")

@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)
self.assertEqual(output, "?a=b")
self.assertRenderEqual(
"test_querystring_non_empty_get_params", request, expected="?a=b"
)

@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)
self.assertEqual(output, "?x=y&a=b")
self.assertRenderEqual("querystring_multiple", request, expected="?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)
self.assertEqual(output, "?x=y&a=1")
self.assertRenderEqual("querystring_replace", request, expected="?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)
self.assertEqual(output, "?a=b&test_new=something")
self.assertRenderEqual(
"querystring_add", request, expected="?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)
self.assertEqual(output, "?a=1")
self.assertRenderEqual("querystring_remove", request, expected="?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)
self.assertEqual(output, "?x=y&a=1")
self.assertRenderEqual(
"querystring_remove_nonexistent", request, expected="?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)
self.assertEqual(output, "?a=2&a=3")
self.assertRenderEqual(
"querystring_list", my_list=[2, 3], expected="?a=2&a=3"
)

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

0 comments on commit 243ed5a

Please sign in to comment.