From d640c71fa35640b4c13107c074be2f52c52ad861 Mon Sep 17 00:00:00 2001 From: daniel a rios Date: Fri, 21 Jun 2019 17:37:41 +0200 Subject: [PATCH] Refs #26431 -- Added tests for resolving URL and translate_url() with provided optional parameter. --- tests/i18n/patterns/tests.py | 5 +++++ tests/i18n/patterns/urls/default.py | 5 +++++ tests/urlpatterns/path_urls.py | 1 + tests/urlpatterns/tests.py | 6 ++++++ 4 files changed, 17 insertions(+) diff --git a/tests/i18n/patterns/tests.py b/tests/i18n/patterns/tests.py index 6ed2c4ffebd..8e77d3fd8fb 100644 --- a/tests/i18n/patterns/tests.py +++ b/tests/i18n/patterns/tests.py @@ -158,6 +158,11 @@ class URLTranslationTests(URLTestCaseBase): # path() URL pattern self.assertEqual(translate_url('/en/account/register-as-path/', 'nl'), '/nl/profiel/registreren-als-pad/') self.assertEqual(translation.get_language(), 'en') + # URL with parameters. + self.assertEqual( + translate_url('/en/with-arguments/regular-argument/optional.html', 'nl'), + '/nl/with-arguments/regular-argument/optional.html', + ) with translation.override('nl'): self.assertEqual(translate_url('/nl/gebruikers/', 'en'), '/en/users/') diff --git a/tests/i18n/patterns/urls/default.py b/tests/i18n/patterns/urls/default.py index b0c2f2585ec..22fff166b3c 100644 --- a/tests/i18n/patterns/urls/default.py +++ b/tests/i18n/patterns/urls/default.py @@ -15,6 +15,11 @@ urlpatterns = [ urlpatterns += i18n_patterns( path('prefixed/', view, name='prefixed'), path('prefixed.xml', view, name='prefixed_xml'), + re_path( + _(r'^with-arguments/(?P[\w-]+)/(?:(?P[\w-]+).html)?$'), + view, + name='with-arguments', + ), re_path(_(r'^users/$'), view, name='users'), re_path(_(r'^account/'), include('i18n.patterns.urls.namespace', namespace='account')), ) diff --git a/tests/urlpatterns/path_urls.py b/tests/urlpatterns/path_urls.py index 953fe6b6d73..b40801b39d3 100644 --- a/tests/urlpatterns/path_urls.py +++ b/tests/urlpatterns/path_urls.py @@ -11,6 +11,7 @@ urlpatterns = [ path('users//', views.empty_view, name='user-with-id'), path('included_urls/', include('urlpatterns.included_urls')), re_path(r'^regex/(?P[0-9]+)/$', views.empty_view, name='regex'), + re_path(r'^regex_optional/(?P\d+)/(?:(?P\d+)/)?', views.empty_view, name='regex_optional'), path('', include('urlpatterns.more_urls')), path('//', views.empty_view, name='lang-and-path'), ] diff --git a/tests/urlpatterns/tests.py b/tests/urlpatterns/tests.py index 74503e45187..66212d7df8c 100644 --- a/tests/urlpatterns/tests.py +++ b/tests/urlpatterns/tests.py @@ -54,6 +54,12 @@ class SimplifiedURLTests(SimpleTestCase): self.assertEqual(match.kwargs, {'pk': '1'}) self.assertEqual(match.route, '^regex/(?P[0-9]+)/$') + def test_re_path_with_optional_parameter(self): + match = resolve('/regex_optional/1/2/') + self.assertEqual(match.url_name, 'regex_optional') + self.assertEqual(match.kwargs, {'arg1': '1', 'arg2': '2'}) + self.assertEqual(match.route, r'^regex_optional/(?P\d+)/(?:(?P\d+)/)?') + def test_path_lookup_with_inclusion(self): match = resolve('/included_urls/extra/something/') self.assertEqual(match.url_name, 'inner-extra')