Skip to content

Commit ea8c405

Browse files
committed
Tests for validating custom_method_name router attribute
1 parent d972df7 commit ea8c405

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

tests/test_routers.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from rest_framework.response import Response
99
from rest_framework.routers import SimpleRouter, DefaultRouter
1010
from rest_framework.test import APIRequestFactory
11+
from collections import namedtuple
1112

1213
factory = APIRequestFactory()
1314

@@ -260,6 +261,14 @@ def list_route_get(self, request, *args, **kwargs):
260261
def detail_route_get(self, request, *args, **kwargs):
261262
return Response({'method': 'link2'})
262263

264+
@list_route(custom_method_name="list_custom-route")
265+
def list_custom_route_get(self, request, *args, **kwargs):
266+
return Response({'method': 'link1'})
267+
268+
@detail_route(custom_method_name="detail_custom-route")
269+
def detail_custom_route_get(self, request, *args, **kwargs):
270+
return Response({'method': 'link2'})
271+
263272

264273
class TestDynamicListAndDetailRouter(TestCase):
265274
def setUp(self):
@@ -268,22 +277,33 @@ def setUp(self):
268277
def test_list_and_detail_route_decorators(self):
269278
routes = self.router.get_routes(DynamicListAndDetailViewSet)
270279
decorator_routes = [r for r in routes if not (r.name.endswith('-list') or r.name.endswith('-detail'))]
280+
281+
MethodNamesMap = namedtuple('MethodNamesMap', 'method_name custom_method_name')
271282
# Make sure all these endpoints exist and none have been clobbered
272-
for i, endpoint in enumerate(['list_route_get', 'list_route_post', 'detail_route_get', 'detail_route_post']):
283+
for i, endpoint in enumerate([MethodNamesMap('list_custom_route_get', 'list_custom-route'),
284+
MethodNamesMap('list_route_get', 'list_route_get'),
285+
MethodNamesMap('list_route_post', 'list_route_post'),
286+
MethodNamesMap('detail_custom_route_get', 'detail_custom-route'),
287+
MethodNamesMap('detail_route_get', 'detail_route_get'),
288+
MethodNamesMap('detail_route_post', 'detail_route_post')
289+
]):
273290
route = decorator_routes[i]
274291
# check url listing
275-
if endpoint.startswith('list_'):
292+
method_name = endpoint.method_name
293+
custom_method_name = endpoint.custom_method_name
294+
295+
if method_name.startswith('list_'):
276296
self.assertEqual(route.url,
277-
'^{{prefix}}/{0}{{trailing_slash}}$'.format(endpoint))
297+
'^{{prefix}}/{0}{{trailing_slash}}$'.format(custom_method_name))
278298
else:
279299
self.assertEqual(route.url,
280-
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(endpoint))
300+
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(custom_method_name))
281301
# check method to function mapping
282-
if endpoint.endswith('_post'):
302+
if method_name.endswith('_post'):
283303
method_map = 'post'
284304
else:
285305
method_map = 'get'
286-
self.assertEqual(route.mapping[method_map], endpoint)
306+
self.assertEqual(route.mapping[method_map], method_name)
287307

288308

289309
class TestRootWithAListlessViewset(TestCase):

0 commit comments

Comments
 (0)