Refs #26431 -- Added tests for resolving URL and translate_url() with provided optional parameter.

This commit is contained in:
daniel a rios 2019-06-21 17:37:41 +02:00 committed by Mariusz Felisiak
parent b24e763846
commit d640c71fa3
4 changed files with 17 additions and 0 deletions

View File

@ -158,6 +158,11 @@ class URLTranslationTests(URLTestCaseBase):
# path() URL pattern # path() URL pattern
self.assertEqual(translate_url('/en/account/register-as-path/', 'nl'), '/nl/profiel/registreren-als-pad/') self.assertEqual(translate_url('/en/account/register-as-path/', 'nl'), '/nl/profiel/registreren-als-pad/')
self.assertEqual(translation.get_language(), 'en') 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'): with translation.override('nl'):
self.assertEqual(translate_url('/nl/gebruikers/', 'en'), '/en/users/') self.assertEqual(translate_url('/nl/gebruikers/', 'en'), '/en/users/')

View File

@ -15,6 +15,11 @@ urlpatterns = [
urlpatterns += i18n_patterns( urlpatterns += i18n_patterns(
path('prefixed/', view, name='prefixed'), path('prefixed/', view, name='prefixed'),
path('prefixed.xml', view, name='prefixed_xml'), path('prefixed.xml', view, name='prefixed_xml'),
re_path(
_(r'^with-arguments/(?P<argument>[\w-]+)/(?:(?P<optional>[\w-]+).html)?$'),
view,
name='with-arguments',
),
re_path(_(r'^users/$'), view, name='users'), re_path(_(r'^users/$'), view, name='users'),
re_path(_(r'^account/'), include('i18n.patterns.urls.namespace', namespace='account')), re_path(_(r'^account/'), include('i18n.patterns.urls.namespace', namespace='account')),
) )

View File

@ -11,6 +11,7 @@ urlpatterns = [
path('users/<id>/', views.empty_view, name='user-with-id'), path('users/<id>/', views.empty_view, name='user-with-id'),
path('included_urls/', include('urlpatterns.included_urls')), path('included_urls/', include('urlpatterns.included_urls')),
re_path(r'^regex/(?P<pk>[0-9]+)/$', views.empty_view, name='regex'), re_path(r'^regex/(?P<pk>[0-9]+)/$', views.empty_view, name='regex'),
re_path(r'^regex_optional/(?P<arg1>\d+)/(?:(?P<arg2>\d+)/)?', views.empty_view, name='regex_optional'),
path('', include('urlpatterns.more_urls')), path('', include('urlpatterns.more_urls')),
path('<lang>/<path:url>/', views.empty_view, name='lang-and-path'), path('<lang>/<path:url>/', views.empty_view, name='lang-and-path'),
] ]

View File

@ -54,6 +54,12 @@ class SimplifiedURLTests(SimpleTestCase):
self.assertEqual(match.kwargs, {'pk': '1'}) self.assertEqual(match.kwargs, {'pk': '1'})
self.assertEqual(match.route, '^regex/(?P<pk>[0-9]+)/$') self.assertEqual(match.route, '^regex/(?P<pk>[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<arg1>\d+)/(?:(?P<arg2>\d+)/)?')
def test_path_lookup_with_inclusion(self): def test_path_lookup_with_inclusion(self):
match = resolve('/included_urls/extra/something/') match = resolve('/included_urls/extra/something/')
self.assertEqual(match.url_name, 'inner-extra') self.assertEqual(match.url_name, 'inner-extra')