diff options
author | Alokik Vijay <alokik.roe@gmail.com> | 2022-03-28 21:26:20 +0530 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-03-29 10:27:40 +0200 |
commit | baf9604ed8fed3e6e7ddfaca2d83c377c81399ae (patch) | |
tree | 141aee743c3cae65e6e4522859489d3c0da1d4aa /tests/urlpatterns | |
parent | 83c803f161044fbfbfcd9a0c94ca93dc131be662 (diff) | |
download | django-baf9604ed8fed3e6e7ddfaca2d83c377c81399ae.tar.gz |
Fixed #16406 -- Added ResolveMatch.captured_kwargs and extra_kwargs.
Thanks Florian Apolloner for the review and implementation idea.
Diffstat (limited to 'tests/urlpatterns')
-rw-r--r-- | tests/urlpatterns/more_urls.py | 7 | ||||
-rw-r--r-- | tests/urlpatterns/path_urls.py | 9 | ||||
-rw-r--r-- | tests/urlpatterns/tests.py | 37 |
3 files changed, 51 insertions, 2 deletions
diff --git a/tests/urlpatterns/more_urls.py b/tests/urlpatterns/more_urls.py index bbfd962c4e..8fc3abd518 100644 --- a/tests/urlpatterns/more_urls.py +++ b/tests/urlpatterns/more_urls.py @@ -3,5 +3,10 @@ from django.urls import re_path from . import views urlpatterns = [ - re_path(r"^more/(?P<extra>\w+)/$", views.empty_view, name="inner-more"), + re_path( + r"^more/(?P<extra>\w+)/$", + views.empty_view, + {"sub-extra": True}, + name="inner-more", + ), ] diff --git a/tests/urlpatterns/path_urls.py b/tests/urlpatterns/path_urls.py index 5ebdfe1b16..8a356b5463 100644 --- a/tests/urlpatterns/path_urls.py +++ b/tests/urlpatterns/path_urls.py @@ -13,6 +13,13 @@ urlpatterns = [ views.empty_view, name="articles-year-month-day", ), + path("books/2007/", views.empty_view, {"extra": True}, name="books-2007"), + path( + "books/<int:year>/<int:month>/<int:day>/", + views.empty_view, + {"extra": True}, + name="books-year-month-day", + ), path("users/", views.empty_view, name="users"), path("users/<id>/", views.empty_view, name="user-with-id"), path("included_urls/", include("urlpatterns.included_urls")), @@ -27,6 +34,6 @@ urlpatterns = [ views.empty_view, name="regex_only_optional", ), - path("", include("urlpatterns.more_urls")), + path("", include("urlpatterns.more_urls"), {"sub-extra": False}), path("<lang>/<path:url>/", views.empty_view, name="lang-and-path"), ] diff --git a/tests/urlpatterns/tests.py b/tests/urlpatterns/tests.py index cbe61278da..f8d73fdb4a 100644 --- a/tests/urlpatterns/tests.py +++ b/tests/urlpatterns/tests.py @@ -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/") |