mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 13:54:47 +03:00
Schema tests:
- add similar action test - decouple links check from overall request
This commit is contained in:
parent
95ba1b3615
commit
2d87d70d09
|
@ -11,7 +11,7 @@ from rest_framework.routers import DefaultRouter
|
||||||
from rest_framework.schemas import SchemaGenerator
|
from rest_framework.schemas import SchemaGenerator
|
||||||
from rest_framework.test import APIClient
|
from rest_framework.test import APIClient
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import GenericViewSet, ModelViewSet
|
||||||
|
|
||||||
|
|
||||||
class MockUser(object):
|
class MockUser(object):
|
||||||
|
@ -19,6 +19,11 @@ class MockUser(object):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class MockCoreapiObject(object):
|
||||||
|
def __eq__(self, value):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class ExamplePagination(pagination.PageNumberPagination):
|
class ExamplePagination(pagination.PageNumberPagination):
|
||||||
page_size = 100
|
page_size = 100
|
||||||
|
|
||||||
|
@ -43,11 +48,11 @@ class ExampleViewSet(ModelViewSet):
|
||||||
|
|
||||||
@detail_route(methods=['put', 'post'], serializer_class=AnotherSerializer)
|
@detail_route(methods=['put', 'post'], serializer_class=AnotherSerializer)
|
||||||
def custom_action(self, request, pk):
|
def custom_action(self, request, pk):
|
||||||
return super(ExampleSerializer, self).retrieve(self, request)
|
pass
|
||||||
|
|
||||||
@list_route()
|
@list_route()
|
||||||
def custom_list_action(self, request):
|
def custom_list_action(self, request):
|
||||||
return super(ExampleViewSet, self).list(self, request)
|
pass
|
||||||
|
|
||||||
def get_serializer(self, *args, **kwargs):
|
def get_serializer(self, *args, **kwargs):
|
||||||
assert self.request
|
assert self.request
|
||||||
|
@ -55,6 +60,22 @@ class ExampleViewSet(ModelViewSet):
|
||||||
return super(ExampleViewSet, self).get_serializer(*args, **kwargs)
|
return super(ExampleViewSet, self).get_serializer(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleViewSet1(GenericViewSet):
|
||||||
|
serializer_class = ExampleSerializer
|
||||||
|
|
||||||
|
@detail_route(methods=['post'])
|
||||||
|
def custom_action(self, request, pk):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleViewSet2(GenericViewSet):
|
||||||
|
serializer_class = ExampleSerializer
|
||||||
|
|
||||||
|
@detail_route(methods=['post'])
|
||||||
|
def custom_action(self, request, pk):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ExampleView(APIView):
|
class ExampleView(APIView):
|
||||||
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
||||||
|
|
||||||
|
@ -70,10 +91,18 @@ router.register('example', ExampleViewSet, base_name='example')
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^', include(router.urls))
|
url(r'^', include(router.urls))
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns2 = [
|
urlpatterns2 = [
|
||||||
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
|
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
router = DefaultRouter(schema_title='Example API' if coreapi else None)
|
||||||
|
router.register('example1', ExampleViewSet1, base_name='example')
|
||||||
|
router.register('example2', ExampleViewSet2, base_name='example')
|
||||||
|
urlpatterns3 = [
|
||||||
|
url(r'^', include(router.urls))
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
||||||
@override_settings(ROOT_URLCONF='tests.test_schemas')
|
@override_settings(ROOT_URLCONF='tests.test_schemas')
|
||||||
|
@ -119,121 +148,128 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
expected = coreapi.Document(
|
expected = coreapi.Document(
|
||||||
url='',
|
url='',
|
||||||
title='Example API',
|
title='Example API',
|
||||||
content={
|
content={'example': MockCoreapiObject()}
|
||||||
'example': {
|
|
||||||
'list': coreapi.Link(
|
|
||||||
url='/example/',
|
|
||||||
action='get',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('page', required=False, location='query'),
|
|
||||||
coreapi.Field('ordering', required=False, location='query')
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'create': coreapi.Link(
|
|
||||||
url='/example/',
|
|
||||||
action='post',
|
|
||||||
encoding='application/json',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('a', required=True, location='form', description='A field description'),
|
|
||||||
coreapi.Field('b', required=False, location='form')
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'retrieve': coreapi.Link(
|
|
||||||
url='/example/{pk}/',
|
|
||||||
action='get',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('pk', required=True, location='path')
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'custom_action_post': coreapi.Link(
|
|
||||||
url='/example/{pk}/custom_action/',
|
|
||||||
action='post',
|
|
||||||
encoding='application/json',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
|
||||||
coreapi.Field('c', required=True, location='form'),
|
|
||||||
coreapi.Field('d', required=False, location='form'),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'custom_action_put': coreapi.Link(
|
|
||||||
url='/example/{pk}/custom_action/',
|
|
||||||
action='put',
|
|
||||||
encoding='application/json',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
|
||||||
coreapi.Field('c', required=True, location='form'),
|
|
||||||
coreapi.Field('d', required=False, location='form'),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'custom_list_action': coreapi.Link(
|
|
||||||
url='/example/custom_list_action/',
|
|
||||||
action='get'
|
|
||||||
),
|
|
||||||
'update': coreapi.Link(
|
|
||||||
url='/example/{pk}/',
|
|
||||||
action='put',
|
|
||||||
encoding='application/json',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
|
||||||
coreapi.Field('a', required=True, location='form', description='A field description'),
|
|
||||||
coreapi.Field('b', required=False, location='form')
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'partial_update': coreapi.Link(
|
|
||||||
url='/example/{pk}/',
|
|
||||||
action='patch',
|
|
||||||
encoding='application/json',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
|
||||||
coreapi.Field('a', required=False, location='form', description='A field description'),
|
|
||||||
coreapi.Field('b', required=False, location='form')
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'destroy': coreapi.Link(
|
|
||||||
url='/example/{pk}/',
|
|
||||||
action='delete',
|
|
||||||
fields=[
|
|
||||||
coreapi.Field('pk', required=True, location='path')
|
|
||||||
]
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
self.assertEqual(response.data, expected)
|
self.assertEqual(response.data, expected)
|
||||||
|
|
||||||
def test_multiple_http_methods_for_detail_route(self):
|
def test_links(self):
|
||||||
client = APIClient()
|
client = APIClient()
|
||||||
client.force_authenticate(MockUser())
|
client.force_authenticate(MockUser())
|
||||||
response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json')
|
response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json')
|
||||||
put_action = ('custom_action_put',
|
self.assertEqual(response.status_code, 200)
|
||||||
coreapi.Link(
|
expected_links = [
|
||||||
url='/example/{pk}/custom_action/',
|
coreapi.Link( # list
|
||||||
action='put',
|
url='/example/',
|
||||||
encoding='application/json',
|
action='get',
|
||||||
fields=[
|
fields=[
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
coreapi.Field('page', required=False, location='query'),
|
||||||
coreapi.Field('c', required=True, location='form'),
|
coreapi.Field('ordering', required=False, location='query')
|
||||||
coreapi.Field('d', required=False, location='form'),
|
]
|
||||||
]
|
),
|
||||||
))
|
coreapi.Link( # create
|
||||||
post_action = ('custom_action_post',
|
url='/example/',
|
||||||
coreapi.Link(
|
action='post',
|
||||||
url='/example/{pk}/custom_action/',
|
encoding='application/json',
|
||||||
action='post',
|
fields=[
|
||||||
encoding='application/json',
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||||
fields=[
|
coreapi.Field('b', required=False, location='form')
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
]
|
||||||
coreapi.Field('c', required=True, location='form'),
|
),
|
||||||
coreapi.Field('d', required=False, location='form'),
|
coreapi.Link( # retrieve
|
||||||
]
|
url='/example/{pk}/',
|
||||||
))
|
action='get',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path')
|
||||||
|
]
|
||||||
|
),
|
||||||
|
coreapi.Link( # custom_action post
|
||||||
|
url='/example/{pk}/custom_action/',
|
||||||
|
action='post',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('c', required=True, location='form'),
|
||||||
|
coreapi.Field('d', required=False, location='form'),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
coreapi.Link( # custom_action put
|
||||||
|
url='/example/{pk}/custom_action/',
|
||||||
|
action='put',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('c', required=True, location='form'),
|
||||||
|
coreapi.Field('d', required=False, location='form'),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
coreapi.Link( # custom_list_action
|
||||||
|
url='/example/custom_list_action/',
|
||||||
|
action='get'
|
||||||
|
),
|
||||||
|
coreapi.Link( # update
|
||||||
|
url='/example/{pk}/',
|
||||||
|
action='put',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||||
|
coreapi.Field('b', required=False, location='form')
|
||||||
|
]
|
||||||
|
),
|
||||||
|
coreapi.Link( # partial_update
|
||||||
|
url='/example/{pk}/',
|
||||||
|
action='patch',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('a', required=False, location='form', description='A field description'),
|
||||||
|
coreapi.Field('b', required=False, location='form')
|
||||||
|
]
|
||||||
|
),
|
||||||
|
coreapi.Link( # destroy
|
||||||
|
url='/example/{pk}/',
|
||||||
|
action='delete',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path')
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
self.assertIn(put_action, response.data['example'].items())
|
response_links = response.data['example'].links.values()
|
||||||
self.assertIn(post_action, response.data['example'].items())
|
for link in expected_links:
|
||||||
|
self.assertIn(link, response_links)
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
||||||
class TestSchemaGenerator(TestCase):
|
class TestSchemaGenerator(TestCase):
|
||||||
|
def test_similar_actions(self):
|
||||||
|
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns3)
|
||||||
|
schema = schema_generator.get_schema()
|
||||||
|
self.assertIn('example1', schema)
|
||||||
|
self.assertIn('example2', schema)
|
||||||
|
custom_action_1 = coreapi.Link(
|
||||||
|
url='/example1/{pk}/custom_action/',
|
||||||
|
action='post',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||||
|
coreapi.Field('b', required=False, location='form')
|
||||||
|
]
|
||||||
|
)
|
||||||
|
custom_action_2 = coreapi.Link(
|
||||||
|
url='/example2/{pk}/custom_action/',
|
||||||
|
action='post',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||||
|
coreapi.Field('b', required=False, location='form')
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.assertIn(custom_action_1, schema['example1'].links.values())
|
||||||
|
self.assertIn(custom_action_2, schema['example2'].links.values())
|
||||||
|
|
||||||
def test_view(self):
|
def test_view(self):
|
||||||
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
|
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
|
||||||
schema = schema_generator.get_schema()
|
schema = schema_generator.get_schema()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user