aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_routers.py
diff options
context:
space:
mode:
authortanwanirahul2014-11-03 14:44:47 +0100
committertanwanirahul2014-11-03 14:44:47 +0100
commitea8c40520165fc33343fceb15221b770701bdedf (patch)
treef41246ef7f9838398bb12968ae240c9a9ae87cc3 /tests/test_routers.py
parentd972df7c9c1867b4a0a57307f423a488c4d4f4b1 (diff)
downloaddjango-rest-framework-ea8c40520165fc33343fceb15221b770701bdedf.tar.bz2
Tests for validating custom_method_name router attribute
Diffstat (limited to 'tests/test_routers.py')
-rw-r--r--tests/test_routers.py32
1 files changed, 26 insertions, 6 deletions
diff --git a/tests/test_routers.py b/tests/test_routers.py
index f6f5a977..d426f832 100644
--- a/tests/test_routers.py
+++ b/tests/test_routers.py
@@ -8,6 +8,7 @@ from rest_framework.decorators import detail_route, list_route
from rest_framework.response import Response
from rest_framework.routers import SimpleRouter, DefaultRouter
from rest_framework.test import APIRequestFactory
+from collections import namedtuple
factory = APIRequestFactory()
@@ -260,6 +261,14 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
def detail_route_get(self, request, *args, **kwargs):
return Response({'method': 'link2'})
+ @list_route(custom_method_name="list_custom-route")
+ def list_custom_route_get(self, request, *args, **kwargs):
+ return Response({'method': 'link1'})
+
+ @detail_route(custom_method_name="detail_custom-route")
+ def detail_custom_route_get(self, request, *args, **kwargs):
+ return Response({'method': 'link2'})
+
class TestDynamicListAndDetailRouter(TestCase):
def setUp(self):
@@ -268,22 +277,33 @@ class TestDynamicListAndDetailRouter(TestCase):
def test_list_and_detail_route_decorators(self):
routes = self.router.get_routes(DynamicListAndDetailViewSet)
decorator_routes = [r for r in routes if not (r.name.endswith('-list') or r.name.endswith('-detail'))]
+
+ MethodNamesMap = namedtuple('MethodNamesMap', 'method_name custom_method_name')
# Make sure all these endpoints exist and none have been clobbered
- for i, endpoint in enumerate(['list_route_get', 'list_route_post', 'detail_route_get', 'detail_route_post']):
+ for i, endpoint in enumerate([MethodNamesMap('list_custom_route_get', 'list_custom-route'),
+ MethodNamesMap('list_route_get', 'list_route_get'),
+ MethodNamesMap('list_route_post', 'list_route_post'),
+ MethodNamesMap('detail_custom_route_get', 'detail_custom-route'),
+ MethodNamesMap('detail_route_get', 'detail_route_get'),
+ MethodNamesMap('detail_route_post', 'detail_route_post')
+ ]):
route = decorator_routes[i]
# check url listing
- if endpoint.startswith('list_'):
+ method_name = endpoint.method_name
+ custom_method_name = endpoint.custom_method_name
+
+ if method_name.startswith('list_'):
self.assertEqual(route.url,
- '^{{prefix}}/{0}{{trailing_slash}}$'.format(endpoint))
+ '^{{prefix}}/{0}{{trailing_slash}}$'.format(custom_method_name))
else:
self.assertEqual(route.url,
- '^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(endpoint))
+ '^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(custom_method_name))
# check method to function mapping
- if endpoint.endswith('_post'):
+ if method_name.endswith('_post'):
method_map = 'post'
else:
method_map = 'get'
- self.assertEqual(route.mapping[method_map], endpoint)
+ self.assertEqual(route.mapping[method_map], method_name)
class TestRootWithAListlessViewset(TestCase):