Added tests for using the same name for multiple URL patterns.

This commit is contained in:
Jack Cushman 2019-12-21 13:11:43 -05:00 committed by Mariusz Felisiak
parent 196009c72c
commit 07f6ff09e4
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,21 @@
from django.urls import path, re_path
from . import views
urlpatterns = [
# Different number of arguments.
path('number_of_args/0/', views.empty_view, name='number_of_args'),
path('number_of_args/1/<value>/', views.empty_view, name='number_of_args'),
# Different names of the keyword arguments.
path('kwargs_names/a/<a>/', views.empty_view, name='kwargs_names'),
path('kwargs_names/b/<b>/', views.empty_view, name='kwargs_names'),
# Different path converters.
path('converter/path/<path:value>/', views.empty_view, name='converter'),
path('converter/str/<str:value>/', views.empty_view, name='converter'),
path('converter/slug/<slug:value>/', views.empty_view, name='converter'),
path('converter/int/<int:value>/', views.empty_view, name='converter'),
path('converter/uuid/<uuid:value>/', views.empty_view, name='converter'),
# Different regular expressions.
re_path(r'^regex/uppercase/([A-Z]+)/', views.empty_view, name='regex'),
re_path(r'^regex/lowercase/([a-z]+)/', views.empty_view, name='regex'),
]

View File

@ -200,6 +200,44 @@ class ConverterTests(SimpleTestCase):
resolve(url) resolve(url)
@override_settings(ROOT_URLCONF='urlpatterns.path_same_name_urls')
class SameNameTests(SimpleTestCase):
def test_matching_urls_same_name(self):
tests = [
('number_of_args', [
([], {}, '0/'),
([1], {}, '1/1/'),
]),
('kwargs_names', [
([], {'a': 1}, 'a/1/'),
([], {'b': 1}, 'b/1/'),
]),
('converter', [
(['a/b'], {}, 'path/a/b/'),
(['a b'], {}, 'str/a%20b/'),
(['a-b'], {}, 'slug/a-b/'),
(['2'], {}, 'int/2/'),
(
['39da9369-838e-4750-91a5-f7805cd82839'],
{},
'uuid/39da9369-838e-4750-91a5-f7805cd82839/'
),
]),
('regex', [
(['ABC'], {}, 'uppercase/ABC/'),
(['abc'], {}, 'lowercase/abc/'),
]),
]
for url_name, cases in tests:
for args, kwargs, url_suffix in cases:
expected_url = '/%s/%s' % (url_name, url_suffix)
with self.subTest(url=expected_url):
self.assertEqual(
reverse(url_name, args=args, kwargs=kwargs),
expected_url,
)
class ParameterRestrictionTests(SimpleTestCase): class ParameterRestrictionTests(SimpleTestCase):
def test_non_identifier_parameter_name_causes_exception(self): def test_non_identifier_parameter_name_causes_exception(self):
msg = ( msg = (