8
8
from rest_framework .response import Response
9
9
from rest_framework .routers import SimpleRouter , DefaultRouter
10
10
from rest_framework .test import APIRequestFactory
11
+ from collections import namedtuple
11
12
12
13
factory = APIRequestFactory ()
13
14
@@ -260,6 +261,14 @@ def list_route_get(self, request, *args, **kwargs):
260
261
def detail_route_get (self , request , * args , ** kwargs ):
261
262
return Response ({'method' : 'link2' })
262
263
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
+
263
272
264
273
class TestDynamicListAndDetailRouter (TestCase ):
265
274
def setUp (self ):
@@ -268,22 +277,33 @@ def setUp(self):
268
277
def test_list_and_detail_route_decorators (self ):
269
278
routes = self .router .get_routes (DynamicListAndDetailViewSet )
270
279
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' )
271
282
# 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
+ ]):
273
290
route = decorator_routes [i ]
274
291
# 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_' ):
276
296
self .assertEqual (route .url ,
277
- '^{{prefix}}/{0}{{trailing_slash}}$' .format (endpoint ))
297
+ '^{{prefix}}/{0}{{trailing_slash}}$' .format (custom_method_name ))
278
298
else :
279
299
self .assertEqual (route .url ,
280
- '^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$' .format (endpoint ))
300
+ '^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$' .format (custom_method_name ))
281
301
# check method to function mapping
282
- if endpoint .endswith ('_post' ):
302
+ if method_name .endswith ('_post' ):
283
303
method_map = 'post'
284
304
else :
285
305
method_map = 'get'
286
- self .assertEqual (route .mapping [method_map ], endpoint )
306
+ self .assertEqual (route .mapping [method_map ], method_name )
287
307
288
308
289
309
class TestRootWithAListlessViewset (TestCase ):
0 commit comments