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