Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test when simple view is interpreted as list view #20

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest
from django.urls import path

from drf_spectacular.validation import validate_schema

Expand All @@ -20,13 +21,19 @@ def assert_schema(schema, reference_file):
validate_schema(schema)


def generate_schema(route, viewset):
def generate_schema(route, viewset=None, view=None):
from rest_framework import routers
from drf_spectacular.openapi import SchemaGenerator

router = routers.SimpleRouter()
router.register(route, viewset, basename=route)
generator = SchemaGenerator(patterns=router.urls)
patterns = []
if viewset:
router = routers.SimpleRouter()
router.register(route, viewset, basename=route)
patterns = router.urls
if view:
patterns = [path(route, view.as_view())]

generator = SchemaGenerator(patterns=patterns)
return generator.get_schema(request=None, public=True)


Expand Down
24 changes: 22 additions & 2 deletions tests/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework import serializers, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView

from drf_spectacular.openapi import AutoSchema
from drf_spectacular.types import OpenApiTypes
Expand Down Expand Up @@ -168,8 +169,27 @@ def non_required_body(self, request):
return Response([])


def test_extend_schema(no_warnings):
with mock.patch('rest_framework.settings.api_settings.DEFAULT_SCHEMA_CLASS', AutoSchema):
class TestApiView(APIView):
authentication_classes = []

@extend_schema(
parameters=[OpenApiParameter('id', OpenApiTypes.INT, OpenApiParameter.PATH)],
responses={200: AlphaSerializer},
)
def get(self, request):
return Response([])


def test_extend_schema_viewset(no_warnings):
assert_schema(
generate_schema('doesitall', DoesItAllViewset),
generate_schema('doesitall', viewset=DoesItAllViewset),
'tests/test_extend_schema.yml'
)


def test_extend_schema_view(no_warnings):
assert_schema(
generate_schema('{id}/test/', view=TestApiView),
'tests/test_extend_schema_view.yml'
)
37 changes: 37 additions & 0 deletions tests/test_extend_schema_view.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: 3.0.3
info:
title: ''
version: 0.0.0
paths:
/{id}/test/:
get:
operationId: test_retrieve
description: ''
parameters:
- in: path
name: id
schema:
type: integer
required: true
tags:
- test
security:
- {}
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Alpha'
description: ''
schemas:
Alpha:
type: object
properties:
field_a:
type: string
field_b:
type: integer
required:
- field_a
- field_b