Skip to content

Commit

Permalink
Update simple router tests
Browse files Browse the repository at this point in the history
Removed old test logic around link/action decorators from `v2.3`. Also
simplified the test by making the results explicit instead of computed.
  • Loading branch information
Ryan P Kilby committed Jun 25, 2018
1 parent 3c60b4b commit 9b64818
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions tests/test_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,44 +103,29 @@ def list(self, request, *args, **kwargs):
def action1(self, request, *args, **kwargs):
return Response({'method': 'action1'})

@action(methods=['post'], detail=True)
def action2(self, request, *args, **kwargs):
return Response({'method': 'action2'})

@action(methods=['post', 'delete'], detail=True)
def action3(self, request, *args, **kwargs):
def action2(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):
Expand Down

0 comments on commit 9b64818

Please sign in to comment.