From 8e3844c246a2df37ff8cdadb8d5449c08b64ddef Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Thu, 14 Sep 2017 16:00:47 +0200 Subject: [PATCH] Add tests for schema exclusions --- tests/test_schemas.py | 79 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/tests/test_schemas.py b/tests/test_schemas.py index f8a63aa89..f67dfda4f 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -8,12 +8,13 @@ from django.test import TestCase, override_settings from rest_framework import filters, pagination, permissions, serializers from rest_framework.compat import coreapi, coreschema -from rest_framework.decorators import detail_route, list_route +from rest_framework.decorators import detail_route, list_route, api_view, schema from rest_framework.request import Request from rest_framework.routers import DefaultRouter from rest_framework.schemas import ( AutoSchema, ManualSchema, SchemaGenerator, get_schema_view ) +from rest_framework.schemas.generators import EndpointEnumerator from rest_framework.test import APIClient, APIRequestFactory from rest_framework.utils import formatting from rest_framework.views import APIView @@ -613,3 +614,79 @@ def test_docstring_is_not_stripped_by_get_description(): descr = schema.get_description('example', 'get') # the first and last character are '\n' correctly removed by get_description assert descr == formatting.dedent(ExampleDocstringAPIView.__doc__[1:][:-1]) + + +# Views for SchemaGenerationExclusionTests +class ExcludedAPIView(APIView): + exclude_from_schema = True + + def get(self, request, *args, **kwargs): + pass + + +@api_view(['GET'], exclude_from_schema=True) +def excluded_fbv(request): + pass + + +@api_view(['GET']) +def included_fbv(request): + pass + + +@unittest.skipUnless(coreapi, 'coreapi is not installed') +class SchemaGenerationExclusionTests(TestCase): + def setUp(self): + self.patterns = [ + url('^excluded-cbv/?$', ExcludedAPIView.as_view()), + url('^excluded-fbv/?$', excluded_fbv), + url('^included-fbv/?$', included_fbv), + ] + + def test_schema_generator_excludes_correctly(self): + """Schema should not include excluded views""" + generator = SchemaGenerator(title='Exclusions', patterns=self.patterns) + schema = generator.get_schema() + expected = coreapi.Document( + url='', + title='Exclusions', + content={ + 'included-fbv': { + 'list': coreapi.Link(url='/included-fbv/', action='get') + } + } + ) + + assert len(schema.data) == 1 + assert 'included-fbv' in schema.data + assert schema == expected + + def test_endpoint_enumerator_excludes_correctly(self): + """It is responsibility of EndpointEnumerator to exclude views""" + inspector = EndpointEnumerator(self.patterns) + endpoints = inspector.get_api_endpoints() + + assert len(endpoints) == 1 + path, method, callback = endpoints[0] + assert path == '/included-fbv/' + + + def test_should_include_endpoint_excludes_correctly(self): + """This is the specific method that should handle the exclusion""" + inspector = EndpointEnumerator(self.patterns) + + pairs = [ + (inspector.get_path_from_regex(pattern.regex.pattern), pattern.callback) + for pattern in self.patterns + ] + + should_include = [ + inspector.should_include_endpoint(*pair) for pair in pairs + ] + + expected = [False, False, True] + + assert should_include == expected + + def test_deprecations(self): + pass \ No newline at end of file