From df2d9e1c824764aa12e366c4c36e64bdb23370e5 Mon Sep 17 00:00:00 2001 From: ChanTsune <41658782+ChanTsune@users.noreply.github.com> Date: Mon, 27 Jul 2020 11:43:16 +0900 Subject: [PATCH 1/3] Update test_template_tags.py --- tests/tests/test_template_tags.py | 53 ++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tests/tests/test_template_tags.py b/tests/tests/test_template_tags.py index 0191721..2fc457e 100644 --- a/tests/tests/test_template_tags.py +++ b/tests/tests/test_template_tags.py @@ -1,3 +1,5 @@ +from django.test.client import RequestFactory + from django_boost.test import TestCase @@ -6,7 +8,56 @@ class TestBoostTemplateTag(TestCase): class TestBoostUrlTemplateTag(TestCase): - pass + + def test_urlencode(self): + from django_boost.templatetags.boost_url import urlencode + cases = [ + ("日本語", "%E6%97%A5%E6%9C%AC%E8%AA%9E"), + ("https://google.com", "https%3A//google.com") + ] + for a, e in cases: + self.assertEqual(urlencode(a), e) + + def test_urldecode(self): + from django_boost.templatetags.boost_url import urldecode + + cases = [ + ("%E6%97%A5%E6%9C%AC%E8%AA%9E", "日本語"), + ("https%3A//google.com", "https://google.com") + ] + for a, e in cases: + self.assertEqual(urldecode(a), e) + + def test_replace_parameters(self): + from django_boost.templatetags.boost_url import replace_parameters + + cases = [ + ("", ('p', 'p'), "p=p"), + ("q=q", ('p', 'p'), "q=q&p=p"), + ("q=q", ('q', 'x', 'p', 'p'), "q=x&p=p"), + ] + factory = RequestFactory() + for qs, args, expected in cases: + request = factory.request(**{'QUERY_STRING': qs}) + self.assertEqual(replace_parameters(request, *args), expected) + + with self.assertRaises(LookupError): + request = factory.request() + replace_parameters(request, 'q') + + def test_get_querystring(self): + from django_boost.templatetags.boost_url import get_querystring + + cases = [ + ("", "q", None), + ("q=q", "q", "q"), + ("q=q&q=j&q=k", "q", "k"), + ] + factory = RequestFactory() + for qs, key, value in cases: + request = factory.request(**{'QUERY_STRING': qs}) + self.assertEqual(get_querystring(request, key), value) + class TestBoostQueryTemplateTag(TestCase): From 4cdf9606d4009ecef37780be70e8e310a5136cef Mon Sep 17 00:00:00 2001 From: ChanTsune <41658782+ChanTsune@users.noreply.github.com> Date: Mon, 27 Jul 2020 11:47:28 +0900 Subject: [PATCH 2/3] refactor replace_parameters tag --- django_boost/templatetags/boost_url.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/django_boost/templatetags/boost_url.py b/django_boost/templatetags/boost_url.py index 68d4a76..dedfd98 100644 --- a/django_boost/templatetags/boost_url.py +++ b/django_boost/templatetags/boost_url.py @@ -4,6 +4,8 @@ from django.template import Library +from django_boost.utils.itertools import chunked + register = Library() @@ -25,8 +27,8 @@ def replace_parameters(request, *args): raise LookupError( "The number of arguments must be odd, but %s was given" % arg_len) url_dict = request.GET.copy() - for i in range(0, arg_len, 2): - url_dict[args[i]] = str(args[i + 1]) + for k, v in chunked(args, 2): + url_dict[k] = str(v) return url_dict.urlencode() From ce86e2925a90488d602dbdb43daf40b1a5988837 Mon Sep 17 00:00:00 2001 From: ChanTsune <41658782+ChanTsune@users.noreply.github.com> Date: Mon, 27 Jul 2020 16:19:32 +0900 Subject: [PATCH 3/3] Fix test case --- tests/tests/test_template_tags.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/tests/test_template_tags.py b/tests/tests/test_template_tags.py index 2fc457e..645dd62 100644 --- a/tests/tests/test_template_tags.py +++ b/tests/tests/test_template_tags.py @@ -8,7 +8,7 @@ class TestBoostTemplateTag(TestCase): class TestBoostUrlTemplateTag(TestCase): - + def test_urlencode(self): from django_boost.templatetags.boost_url import urlencode cases = [ @@ -32,15 +32,17 @@ def test_replace_parameters(self): from django_boost.templatetags.boost_url import replace_parameters cases = [ - ("", ('p', 'p'), "p=p"), - ("q=q", ('p', 'p'), "q=q&p=p"), - ("q=q", ('q', 'x', 'p', 'p'), "q=x&p=p"), + ("", ('p', 'p'), ("p=p",)), + ("q=q", ('p', 'p'), ("q=q", "p=p")), + ("q=q", ('q', 'x', 'p', 'p'), ("q=x", "p=p")), ] factory = RequestFactory() for qs, args, expected in cases: request = factory.request(**{'QUERY_STRING': qs}) - self.assertEqual(replace_parameters(request, *args), expected) - + actual = replace_parameters(request, *args) + for e in expected: + self.assertIn(e, actual) + with self.assertRaises(LookupError): request = factory.request() replace_parameters(request, 'q') @@ -59,7 +61,6 @@ def test_get_querystring(self): self.assertEqual(get_querystring(request, key), value) - class TestBoostQueryTemplateTag(TestCase): pass