mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 22:04:48 +03:00
Clean up schema tests
This commit is contained in:
parent
657a7c5e00
commit
4a0bf19d55
|
@ -6,7 +6,6 @@ from django.test import TestCase, override_settings
|
||||||
from rest_framework import filters, pagination, permissions, serializers
|
from rest_framework import filters, pagination, permissions, serializers
|
||||||
from rest_framework.compat import coreapi
|
from rest_framework.compat import coreapi
|
||||||
from rest_framework.decorators import detail_route, list_route
|
from rest_framework.decorators import detail_route, list_route
|
||||||
from rest_framework.response import Response
|
|
||||||
from rest_framework.routers import DefaultRouter
|
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
|
||||||
|
@ -55,24 +54,11 @@ class ExampleViewSet(ModelViewSet):
|
||||||
return super(ExampleViewSet, self).get_serializer(*args, **kwargs)
|
return super(ExampleViewSet, self).get_serializer(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class ExampleView(APIView):
|
|
||||||
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
|
||||||
return Response()
|
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
|
||||||
return Response()
|
|
||||||
|
|
||||||
|
|
||||||
router = DefaultRouter(schema_title='Example API' if coreapi else None)
|
router = DefaultRouter(schema_title='Example API' if coreapi else None)
|
||||||
router.register('example', ExampleViewSet, base_name='example')
|
router.register('example', ExampleViewSet, base_name='example')
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^', include(router.urls))
|
url(r'^', include(router.urls))
|
||||||
]
|
]
|
||||||
urlpatterns2 = [
|
|
||||||
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
||||||
|
@ -192,60 +178,62 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
self.assertEqual(response.data, expected)
|
self.assertEqual(response.data, expected)
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleListView(APIView):
|
||||||
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
||||||
|
|
||||||
|
def get(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleDetailView(APIView):
|
||||||
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
||||||
|
|
||||||
|
def get(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
||||||
class TestSchemaGenerator(TestCase):
|
class TestSchemaGenerator(TestCase):
|
||||||
def test_view(self):
|
def setUp(self):
|
||||||
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
|
self.patterns = [
|
||||||
schema = schema_generator.get_schema()
|
url('^example/?$', ExampleListView.as_view()),
|
||||||
|
url('^example/(?P<pk>\d+)/?$', ExampleDetailView.as_view()),
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_schema_for_regular_views(self):
|
||||||
|
"""
|
||||||
|
Ensure that schema generation works for APIView classes.
|
||||||
|
"""
|
||||||
|
generator = SchemaGenerator(title='Example API', patterns=self.patterns)
|
||||||
|
schema = generator.get_schema()
|
||||||
expected = coreapi.Document(
|
expected = coreapi.Document(
|
||||||
url='',
|
url='',
|
||||||
title='Test View',
|
title='Example API',
|
||||||
content={
|
content={
|
||||||
'example-view': {
|
'example': {
|
||||||
'create': coreapi.Link(
|
'create': coreapi.Link(
|
||||||
url='/example-view/',
|
url='/example/',
|
||||||
action='post',
|
action='post',
|
||||||
fields=[]
|
fields=[]
|
||||||
),
|
),
|
||||||
'list': coreapi.Link(
|
'list': coreapi.Link(
|
||||||
url='/example-view/',
|
url='/example/',
|
||||||
action='get',
|
action='get',
|
||||||
fields=[]
|
fields=[]
|
||||||
)
|
),
|
||||||
}
|
'read': coreapi.Link(
|
||||||
}
|
url='/example/{pk}/',
|
||||||
)
|
action='get',
|
||||||
self.assertEqual(schema, expected)
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path')
|
||||||
|
|
||||||
class SnippetListView(APIView):
|
|
||||||
def get(self, *args, **kwargs):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class SnippetDetailView(APIView):
|
|
||||||
def get(self, *args, **kwargs):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
|
||||||
class TestDocumentLinksOnExplicitlyDefinedPatterns(TestCase):
|
|
||||||
"""
|
|
||||||
Given a "list" and "detail" view with explicitly defined urlpatterns,
|
|
||||||
and that the views support common HTTP methods, Document Link objects
|
|
||||||
should be created for both the "list" and "detail" endpoints.
|
|
||||||
"""
|
|
||||||
def setUp(self):
|
|
||||||
self.patterns = [
|
|
||||||
url('^snippets/?$', SnippetListView.as_view()),
|
|
||||||
url('^snippets/(?P<pk>\d+)/?$', SnippetDetailView.as_view()),
|
|
||||||
]
|
]
|
||||||
self.generator = SchemaGenerator(title='Test View', patterns=self.patterns)
|
)
|
||||||
self.document = self.generator.get_schema()
|
}
|
||||||
|
}
|
||||||
def test_there_should_be_a_link_to_the_snippets_list_view(self):
|
)
|
||||||
expected = '/snippets/'
|
print schema
|
||||||
snippets = self.document._data['snippets']
|
print expected
|
||||||
urls = [link.url for link in snippets._data.values()]
|
self.assertEqual(schema, expected)
|
||||||
|
|
||||||
self.assertIn(expected, urls)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user