Skip to content

Commit 0c83933

Browse files
author
Ryan P Kilby
committed
Update tests to use action decorator
1 parent fec50fb commit 0c83933

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

tests/test_routers.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from rest_framework import permissions, serializers, viewsets
1212
from rest_framework.compat import get_regex_pattern
13-
from rest_framework.decorators import detail_route, list_route
13+
from rest_framework.decorators import action
1414
from rest_framework.response import Response
1515
from rest_framework.routers import DefaultRouter, SimpleRouter
1616
from rest_framework.test import APIRequestFactory
@@ -66,12 +66,12 @@ def get_object(self, *args, **kwargs):
6666

6767

6868
class RegexUrlPathViewSet(viewsets.ViewSet):
69-
@list_route(url_path='list/(?P<kwarg>[0-9]{4})')
69+
@action(detail=False, url_path='list/(?P<kwarg>[0-9]{4})')
7070
def regex_url_path_list(self, request, *args, **kwargs):
7171
kwarg = self.kwargs.get('kwarg', '')
7272
return Response({'kwarg': kwarg})
7373

74-
@detail_route(url_path='detail/(?P<kwarg>[0-9]{4})')
74+
@action(detail=True, url_path='detail/(?P<kwarg>[0-9]{4})')
7575
def regex_url_path_detail(self, request, *args, **kwargs):
7676
pk = self.kwargs.get('pk', '')
7777
kwarg = self.kwargs.get('kwarg', '')
@@ -111,23 +111,23 @@ class BasicViewSet(viewsets.ViewSet):
111111
def list(self, request, *args, **kwargs):
112112
return Response({'method': 'list'})
113113

114-
@detail_route(methods=['post'])
114+
@action(methods=['post'])
115115
def action1(self, request, *args, **kwargs):
116116
return Response({'method': 'action1'})
117117

118-
@detail_route(methods=['post'])
118+
@action(methods=['post'])
119119
def action2(self, request, *args, **kwargs):
120120
return Response({'method': 'action2'})
121121

122-
@detail_route(methods=['post', 'delete'])
122+
@action(methods=['post', 'delete'])
123123
def action3(self, request, *args, **kwargs):
124124
return Response({'method': 'action2'})
125125

126-
@detail_route()
126+
@action()
127127
def link1(self, request, *args, **kwargs):
128128
return Response({'method': 'link1'})
129129

130-
@detail_route()
130+
@action()
131131
def link2(self, request, *args, **kwargs):
132132
return Response({'method': 'link2'})
133133

@@ -296,7 +296,7 @@ def setUp(self):
296296
class TestViewSet(viewsets.ModelViewSet):
297297
permission_classes = []
298298

299-
@detail_route(methods=['post'], permission_classes=[permissions.AllowAny])
299+
@action(methods=['post'], permission_classes=[permissions.AllowAny])
300300
def custom(self, request, *args, **kwargs):
301301
return Response({
302302
'permission_classes': self.permission_classes
@@ -321,7 +321,7 @@ class TestActionAppliedToExistingRoute(TestCase):
321321
def test_exception_raised_when_action_applied_to_existing_route(self):
322322
class TestViewSet(viewsets.ModelViewSet):
323323

324-
@detail_route(methods=['post'])
324+
@action(methods=['post'])
325325
def retrieve(self, request, *args, **kwargs):
326326
return Response({
327327
'hello': 'world'
@@ -338,27 +338,27 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
338338
def list(self, request, *args, **kwargs):
339339
return Response({'method': 'list'})
340340

341-
@list_route(methods=['post'])
341+
@action(methods=['post'], detail=False)
342342
def list_route_post(self, request, *args, **kwargs):
343343
return Response({'method': 'action1'})
344344

345-
@detail_route(methods=['post'])
345+
@action(methods=['post'])
346346
def detail_route_post(self, request, *args, **kwargs):
347347
return Response({'method': 'action2'})
348348

349-
@list_route()
349+
@action(detail=False)
350350
def list_route_get(self, request, *args, **kwargs):
351351
return Response({'method': 'link1'})
352352

353-
@detail_route()
353+
@action()
354354
def detail_route_get(self, request, *args, **kwargs):
355355
return Response({'method': 'link2'})
356356

357-
@list_route(url_path="list_custom-route")
357+
@action(detail=False, url_path="list_custom-route")
358358
def list_custom_route_get(self, request, *args, **kwargs):
359359
return Response({'method': 'link1'})
360360

361-
@detail_route(url_path="detail_custom-route")
361+
@action(url_path="detail_custom-route")
362362
def detail_custom_route_get(self, request, *args, **kwargs):
363363
return Response({'method': 'link2'})
364364

tests/test_schemas.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
filters, generics, pagination, permissions, serializers
1111
)
1212
from rest_framework.compat import coreapi, coreschema, get_regex_pattern
13-
from rest_framework.decorators import (
14-
api_view, detail_route, list_route, schema
15-
)
13+
from rest_framework.decorators import action, api_view, schema
1614
from rest_framework.request import Request
1715
from rest_framework.routers import DefaultRouter, SimpleRouter
1816
from rest_framework.schemas import (
@@ -67,25 +65,25 @@ class ExampleViewSet(ModelViewSet):
6765
filter_backends = [filters.OrderingFilter]
6866
serializer_class = ExampleSerializer
6967

70-
@detail_route(methods=['post'], serializer_class=AnotherSerializer)
68+
@action(methods=['post'], serializer_class=AnotherSerializer)
7169
def custom_action(self, request, pk):
7270
"""
7371
A description of custom action.
7472
"""
7573
return super(ExampleSerializer, self).retrieve(self, request)
7674

77-
@detail_route(methods=['post'], serializer_class=AnotherSerializerWithListFields)
75+
@action(methods=['post'], serializer_class=AnotherSerializerWithListFields)
7876
def custom_action_with_list_fields(self, request, pk):
7977
"""
8078
A custom action using both list field and list serializer in the serializer.
8179
"""
8280
return super(ExampleSerializer, self).retrieve(self, request)
8381

84-
@list_route()
82+
@action(detail=False)
8583
def custom_list_action(self, request):
8684
return super(ExampleViewSet, self).list(self, request)
8785

88-
@list_route(methods=['post', 'get'], serializer_class=EmptySerializer)
86+
@action(methods=['post', 'get'], detail=False, serializer_class=EmptySerializer)
8987
def custom_list_action_multiple_methods(self, request):
9088
return super(ExampleViewSet, self).list(self, request)
9189

@@ -759,11 +757,11 @@ class NamingCollisionViewSet(GenericViewSet):
759757
"""
760758
permision_class = ()
761759

762-
@list_route()
760+
@action(detail=False)
763761
def detail(self, request):
764762
return {}
765763

766-
@list_route(url_path='detail/export')
764+
@action(detail=False, url_path='detail/export')
767765
def detail_export(self, request):
768766
return {}
769767

@@ -940,7 +938,7 @@ def options(self, request, *args, **kwargs):
940938

941939
class AViewSet(ModelViewSet):
942940

943-
@detail_route(methods=['options', 'get'])
941+
@action(methods=['options', 'get'])
944942
def custom_action(self, request, pk):
945943
pass
946944

0 commit comments

Comments
 (0)