5
5
from django .test import RequestFactory , override_settings
6
6
from rest_framework .request import Request
7
7
8
+ from rest_framework_json_api .management .commands .generateschema import Command
8
9
from rest_framework_json_api .schemas .openapi import AutoSchema , SchemaGenerator
9
- from rest_framework_json_api .views import ModelViewSet
10
10
11
- from example import models , serializers , views
11
+ from example import views
12
+ from example .tests import TestBase
12
13
13
14
14
15
def create_request (path ):
@@ -17,12 +18,6 @@ def create_request(path):
17
18
return request
18
19
19
20
20
- def create_view (view_cls , method , request ):
21
- generator = SchemaGenerator ()
22
- view = generator .create_view (view_cls .as_view (), method , request )
23
- return view
24
-
25
-
26
21
def create_view_with_kw (view_cls , method , request , initkwargs ):
27
22
generator = SchemaGenerator ()
28
23
view = generator .create_view (view_cls .as_view (initkwargs ), method , request )
@@ -132,45 +127,51 @@ def test_schema_construction():
132
127
assert 'components' in schema
133
128
134
129
135
- # TODO: figure these out
136
- def test_schema_related ():
137
- class AuthorBioViewSet (ModelViewSet ):
138
- queryset = models .AuthorBio .objects .all ()
139
- serializer_class = serializers .AuthorBioSerializer
140
-
141
- patterns = [
142
- url (r'^authors/(?P<pk>[^/.]+)/(?P<related_field>\w+)/$' ,
143
- views .AuthorViewSet .as_view ({'get' : 'retrieve_related' }),
144
- name = 'author-related' ),
145
- url (r'^bios/(?P<pk>[^/.]+)/$' ,
146
- AuthorBioViewSet ,
147
- name = 'author-bio' )
148
- ]
149
- generator = SchemaGenerator (patterns = patterns )
150
-
151
- request = create_request ('/authors/123/bio/' )
152
- schema = generator .get_schema (request = request )
153
- # TODO: finish this test
154
- print (schema )
155
-
156
- # def test_retrieve_relationships():
157
- # path = '/authors/{id}/relationships/bio/'
158
- # method = 'GET'
159
- #
160
- # view = create_view_with_kw(
161
- # views.AuthorViewSet,
162
- # method,
163
- # create_request(path),
164
- # {'get': 'retrieve_related'}
165
- # )
166
- # inspector = AutoSchema()
167
- # inspector.view = view
168
- #
169
- # operation = inspector.get_operation(path, method)
170
- # assert 'responses' in operation
171
- # assert '200' in operation['responses']
172
- # resp = operation['responses']['200']['content']
173
- # data = resp['application/vnd.api+json']['schema']['properties']['data']
174
- # assert data['type'] == 'object'
175
- # assert data['required'] == ['type', 'id']
176
- # assert data['properties']['type'] == {'$ref': '#/components/schemas/type'}
130
+ def test_generateschema_command ():
131
+ command = Command ()
132
+ assert command .get_generator_class () == SchemaGenerator
133
+
134
+
135
+ class TestSchemaRelatedField (TestBase ):
136
+ def test_schema_related_serializers (self ):
137
+ """
138
+ Confirm that paths are generated for related fields. For example:
139
+ url path '/authors/{pk}/{related_field>}/' generates:
140
+ /authors/{id}/comments/
141
+ /authors/{id}/entries/
142
+ /authors/{id}/first_entry/
143
+ and confirm that the schema for the related field is properly rendered
144
+ """
145
+ generator = SchemaGenerator ()
146
+ request = create_request ('/' )
147
+ schema = generator .get_schema (request = request )
148
+ assert '/authors/{id}/comments/' in schema ['paths' ]
149
+ assert '/authors/{id}/entries/' in schema ['paths' ]
150
+ assert '/authors/{id}/first_entry/' in schema ['paths' ]
151
+ first_get = schema ['paths' ]['/authors/{id}/first_entry/' ]['get' ]['responses' ]['200' ]
152
+ first_schema = first_get ['content' ]['application/vnd.api+json' ]['schema' ]
153
+ first_props = first_schema ['properties' ]['data' ]['properties' ]['attributes' ]['properties' ]
154
+ assert 'headline' in first_props
155
+ assert first_props ['headline' ] == {'type' : 'string' , 'maxLength' : 255 }
156
+
157
+ # def test_retrieve_relationships(self):
158
+ # path = '/authors/{id}/relationships/bio/'
159
+ # method = 'GET'
160
+ #
161
+ # view = create_view_with_kw(
162
+ # views.AuthorViewSet,
163
+ # method,
164
+ # create_request(path),
165
+ # {'get': 'retrieve_related'}
166
+ # )
167
+ # inspector = AutoSchema()
168
+ # inspector.view = view
169
+ #
170
+ # operation = inspector.get_operation(path, method)
171
+ # assert 'responses' in operation
172
+ # assert '200' in operation['responses']
173
+ # resp = operation['responses']['200']['content']
174
+ # data = resp['application/vnd.api+json']['schema']['properties']['data']
175
+ # assert data['type'] == 'object'
176
+ # assert data['required'] == ['type', 'id']
177
+ # assert data['properties']['type'] == {'$ref': '#/components/schemas/type'}
0 commit comments