2023-10-15 20:01:35 +00:00
|
|
|
from django.http import QueryDict
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.test import RequestFactory, SimpleTestCase
|
|
|
|
|
|
|
|
from ..utils import setup
|
|
|
|
|
|
|
|
|
|
|
|
class QueryStringTagTests(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.request_factory = RequestFactory()
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_empty": "{% querystring %}"})
|
|
|
|
def test_querystring_empty(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/")
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_empty")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request)
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_non_empty": "{% querystring %}"})
|
|
|
|
def test_querystring_non_empty(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/", {"a": "b"})
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_non_empty")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request)
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "?a=b")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_multiple": "{% querystring %}"})
|
|
|
|
def test_querystring_multiple(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/", {"x": "y", "a": "b"})
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_multiple")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request)
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "?x=y&a=b")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_replace": "{% querystring a=1 %}"})
|
|
|
|
def test_querystring_replace(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/", {"x": "y", "a": "b"})
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_replace")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request)
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "?x=y&a=1")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_add": "{% querystring test_new='something' %}"})
|
|
|
|
def test_querystring_add(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/", {"a": "b"})
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_add")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request)
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "?a=b&test_new=something")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_remove": "{% querystring test=None a=1 %}"})
|
|
|
|
def test_querystring_remove(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/", {"test": "value", "a": "1"})
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_remove")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request)
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "?a=1")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_remove_nonexistent": "{% querystring nonexistent=None a=1 %}"})
|
|
|
|
def test_querystring_remove_nonexistent(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/", {"x": "y", "a": "1"})
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_remove_nonexistent")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request)
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "?x=y&a=1")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_list": "{% querystring a=my_list %}"})
|
|
|
|
def test_querystring_add_list(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/")
|
2024-07-15 16:28:55 +00:00
|
|
|
template = self.engine.get_template("querystring_list")
|
2023-10-15 20:01:35 +00:00
|
|
|
context = RequestContext(request, {"my_list": [2, 3]})
|
|
|
|
output = template.render(context)
|
|
|
|
self.assertEqual(output, "?a=2&a=3")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_query_dict": "{% querystring request.GET a=2 %}"})
|
|
|
|
def test_querystring_with_explicit_query_dict(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
request = self.request_factory.get("/", {"a": 1})
|
|
|
|
output = self.engine.render_to_string(
|
2024-07-15 16:28:55 +00:00
|
|
|
"querystring_query_dict", {"request": request}
|
2023-10-15 20:01:35 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(output, "?a=2")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_query_dict_no_request": "{% querystring my_query_dict a=2 %}"})
|
|
|
|
def test_querystring_with_explicit_query_dict_and_no_request(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
context = {"my_query_dict": QueryDict("a=1&b=2")}
|
|
|
|
output = self.engine.render_to_string(
|
2024-07-15 16:28:55 +00:00
|
|
|
"querystring_query_dict_no_request", context
|
2023-10-15 20:01:35 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(output, "?a=2&b=2")
|
|
|
|
|
2024-07-15 16:28:55 +00:00
|
|
|
@setup({"querystring_no_request_no_query_dict": "{% querystring %}"})
|
|
|
|
def test_querystring_without_request_or_explicit_query_dict(self):
|
2023-10-15 20:01:35 +00:00
|
|
|
msg = "'Context' object has no attribute 'request'"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2024-07-15 16:28:55 +00:00
|
|
|
self.engine.render_to_string("querystring_no_request_no_query_dict")
|