1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #16406 -- Added ResolveMatch.captured_kwargs and extra_kwargs.

Thanks Florian Apolloner for the review and implementation idea.
This commit is contained in:
Alokik Vijay
2022-03-28 21:26:20 +05:30
committed by Mariusz Felisiak
parent 83c803f161
commit baf9604ed8
11 changed files with 163 additions and 14 deletions

View File

@@ -34,6 +34,8 @@ class SimplifiedURLTests(SimpleTestCase):
self.assertEqual(match.args, ())
self.assertEqual(match.kwargs, {})
self.assertEqual(match.route, "articles/2003/")
self.assertEqual(match.captured_kwargs, {})
self.assertEqual(match.extra_kwargs, {})
def test_path_lookup_with_typed_parameters(self):
match = resolve("/articles/2015/")
@@ -41,6 +43,8 @@ class SimplifiedURLTests(SimpleTestCase):
self.assertEqual(match.args, ())
self.assertEqual(match.kwargs, {"year": 2015})
self.assertEqual(match.route, "articles/<int:year>/")
self.assertEqual(match.captured_kwargs, {"year": 2015})
self.assertEqual(match.extra_kwargs, {})
def test_path_lookup_with_multiple_parameters(self):
match = resolve("/articles/2015/04/12/")
@@ -48,18 +52,44 @@ class SimplifiedURLTests(SimpleTestCase):
self.assertEqual(match.args, ())
self.assertEqual(match.kwargs, {"year": 2015, "month": 4, "day": 12})
self.assertEqual(match.route, "articles/<int:year>/<int:month>/<int:day>/")
self.assertEqual(match.captured_kwargs, {"year": 2015, "month": 4, "day": 12})
self.assertEqual(match.extra_kwargs, {})
def test_path_lookup_with_multiple_parameters_and_extra_kwarg(self):
match = resolve("/books/2015/04/12/")
self.assertEqual(match.url_name, "books-year-month-day")
self.assertEqual(match.args, ())
self.assertEqual(
match.kwargs, {"year": 2015, "month": 4, "day": 12, "extra": True}
)
self.assertEqual(match.route, "books/<int:year>/<int:month>/<int:day>/")
self.assertEqual(match.captured_kwargs, {"year": 2015, "month": 4, "day": 12})
self.assertEqual(match.extra_kwargs, {"extra": True})
def test_path_lookup_with_extra_kwarg(self):
match = resolve("/books/2007/")
self.assertEqual(match.url_name, "books-2007")
self.assertEqual(match.args, ())
self.assertEqual(match.kwargs, {"extra": True})
self.assertEqual(match.route, "books/2007/")
self.assertEqual(match.captured_kwargs, {})
self.assertEqual(match.extra_kwargs, {"extra": True})
def test_two_variable_at_start_of_path_pattern(self):
match = resolve("/en/foo/")
self.assertEqual(match.url_name, "lang-and-path")
self.assertEqual(match.kwargs, {"lang": "en", "url": "foo"})
self.assertEqual(match.route, "<lang>/<path:url>/")
self.assertEqual(match.captured_kwargs, {"lang": "en", "url": "foo"})
self.assertEqual(match.extra_kwargs, {})
def test_re_path(self):
match = resolve("/regex/1/")
self.assertEqual(match.url_name, "regex")
self.assertEqual(match.kwargs, {"pk": "1"})
self.assertEqual(match.route, "^regex/(?P<pk>[0-9]+)/$")
self.assertEqual(match.captured_kwargs, {"pk": "1"})
self.assertEqual(match.extra_kwargs, {})
def test_re_path_with_optional_parameter(self):
for url, kwargs in (
@@ -74,6 +104,8 @@ class SimplifiedURLTests(SimpleTestCase):
match.route,
r"^regex_optional/(?P<arg1>\d+)/(?:(?P<arg2>\d+)/)?",
)
self.assertEqual(match.captured_kwargs, kwargs)
self.assertEqual(match.extra_kwargs, {})
def test_re_path_with_missing_optional_parameter(self):
match = resolve("/regex_only_optional/")
@@ -84,6 +116,8 @@ class SimplifiedURLTests(SimpleTestCase):
match.route,
r"^regex_only_optional/(?:(?P<arg1>\d+)/)?",
)
self.assertEqual(match.captured_kwargs, {})
self.assertEqual(match.extra_kwargs, {})
def test_path_lookup_with_inclusion(self):
match = resolve("/included_urls/extra/something/")
@@ -94,6 +128,9 @@ class SimplifiedURLTests(SimpleTestCase):
match = resolve("/more/99/")
self.assertEqual(match.url_name, "inner-more")
self.assertEqual(match.route, r"^more/(?P<extra>\w+)/$")
self.assertEqual(match.kwargs, {"extra": "99", "sub-extra": True})
self.assertEqual(match.captured_kwargs, {"extra": "99"})
self.assertEqual(match.extra_kwargs, {"sub-extra": True})
def test_path_lookup_with_double_inclusion(self):
match = resolve("/included_urls/more/some_value/")