diff --git a/tests/urlpatterns/path_same_name_urls.py b/tests/urlpatterns/path_same_name_urls.py new file mode 100644 index 00000000000..8eee949316c --- /dev/null +++ b/tests/urlpatterns/path_same_name_urls.py @@ -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//', views.empty_view, name='number_of_args'), + # Different names of the keyword arguments. + path('kwargs_names/a//', views.empty_view, name='kwargs_names'), + path('kwargs_names/b//', views.empty_view, name='kwargs_names'), + # Different path converters. + path('converter/path//', views.empty_view, name='converter'), + path('converter/str//', views.empty_view, name='converter'), + path('converter/slug//', views.empty_view, name='converter'), + path('converter/int//', views.empty_view, name='converter'), + path('converter/uuid//', 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'), +] diff --git a/tests/urlpatterns/tests.py b/tests/urlpatterns/tests.py index b149e0d5126..95bea583394 100644 --- a/tests/urlpatterns/tests.py +++ b/tests/urlpatterns/tests.py @@ -200,6 +200,44 @@ class ConverterTests(SimpleTestCase): 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): def test_non_identifier_parameter_name_causes_exception(self): msg = (