Update simple router tests

Removed old test logic around link/action decorators from `v2.3`. Also
simplified the test by making the results explicit instead of computed.
This commit is contained in:
Ryan P Kilby 2018-05-16 08:50:58 -04:00
parent 3c60b4b7b6
commit 9b64818435

View File

@ -103,44 +103,29 @@ class BasicViewSet(viewsets.ViewSet):
def action1(self, request, *args, **kwargs): def action1(self, request, *args, **kwargs):
return Response({'method': 'action1'}) return Response({'method': 'action1'})
@action(methods=['post'], detail=True) @action(methods=['post', 'delete'], detail=True)
def action2(self, request, *args, **kwargs): def action2(self, request, *args, **kwargs):
return Response({'method': 'action2'}) return Response({'method': 'action2'})
@action(methods=['post', 'delete'], detail=True)
def action3(self, request, *args, **kwargs):
return Response({'method': 'action2'})
@action(detail=True)
def link1(self, request, *args, **kwargs):
return Response({'method': 'link1'})
@action(detail=True)
def link2(self, request, *args, **kwargs):
return Response({'method': 'link2'})
class TestSimpleRouter(TestCase): class TestSimpleRouter(TestCase):
def setUp(self): def setUp(self):
self.router = SimpleRouter() self.router = SimpleRouter()
def test_link_and_action_decorator(self): def test_action_routes(self):
routes = self.router.get_routes(BasicViewSet) # Get action routes (first two are list/detail)
decorator_routes = routes[2:] routes = self.router.get_routes(BasicViewSet)[2:]
# Make sure all these endpoints exist and none have been clobbered
for i, endpoint in enumerate(['action1', 'action2', 'action3', 'link1', 'link2']): assert routes[0].url == '^{prefix}/{lookup}/action1{trailing_slash}$'
route = decorator_routes[i] assert routes[0].mapping == {
# check url listing 'post': 'action1',
assert route.url == '^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(endpoint) }
# check method to function mapping
if endpoint == 'action3': assert routes[1].url == '^{prefix}/{lookup}/action2{trailing_slash}$'
methods_map = ['post', 'delete'] assert routes[1].mapping == {
elif endpoint.startswith('action'): 'post': 'action2',
methods_map = ['post'] 'delete': 'action2',
else: }
methods_map = ['get']
for method in methods_map:
assert route.mapping[method] == endpoint
class TestRootView(URLPatternsTestCase, TestCase): class TestRootView(URLPatternsTestCase, TestCase):