mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 21:40:13 +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.compat import coreapi
|
||||
from rest_framework.decorators import detail_route, list_route
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from rest_framework.schemas import SchemaGenerator
|
||||
from rest_framework.test import APIClient
|
||||
|
@ -55,24 +54,11 @@ class ExampleViewSet(ModelViewSet):
|
|||
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.register('example', ExampleViewSet, base_name='example')
|
||||
urlpatterns = [
|
||||
url(r'^', include(router.urls))
|
||||
]
|
||||
urlpatterns2 = [
|
||||
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
|
||||
]
|
||||
|
||||
|
||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
||||
|
@ -192,60 +178,62 @@ class TestRouterGeneratedSchema(TestCase):
|
|||
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')
|
||||
class TestSchemaGenerator(TestCase):
|
||||
def test_view(self):
|
||||
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
|
||||
schema = schema_generator.get_schema()
|
||||
def setUp(self):
|
||||
self.patterns = [
|
||||
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(
|
||||
url='',
|
||||
title='Test View',
|
||||
title='Example API',
|
||||
content={
|
||||
'example-view': {
|
||||
'example': {
|
||||
'create': coreapi.Link(
|
||||
url='/example-view/',
|
||||
url='/example/',
|
||||
action='post',
|
||||
fields=[]
|
||||
),
|
||||
'list': coreapi.Link(
|
||||
url='/example-view/',
|
||||
url='/example/',
|
||||
action='get',
|
||||
fields=[]
|
||||
),
|
||||
'read': coreapi.Link(
|
||||
url='/example/{pk}/',
|
||||
action='get',
|
||||
fields=[
|
||||
coreapi.Field('pk', required=True, location='path')
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
print schema
|
||||
print expected
|
||||
self.assertEqual(schema, expected)
|
||||
|
||||
|
||||
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/'
|
||||
snippets = self.document._data['snippets']
|
||||
urls = [link.url for link in snippets._data.values()]
|
||||
|
||||
self.assertIn(expected, urls)
|
||||
|
|
Loading…
Reference in New Issue
Block a user