Skip to content
Prev Previous commit
Next Next commit
Clean up schema tests
  • Loading branch information
lovelydinosaur committed Sep 30, 2016
commit 4a0bf19d555e56c7aa4cd3002e01a1ac79e01223
96 changes: 42 additions & 54 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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
Expand Down Expand Up @@ -55,24 +54,11 @@ def get_serializer(self, *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.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')
Expand Down Expand Up @@ -192,60 +178,62 @@ def test_authenticated_request(self):
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)