Skip to content

Commit c124c7a

Browse files
committed
Add test for ReadOnlyField to nullable field
1 parent 0dea78c commit c124c7a

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

tests/test_readonlyfield.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import uuid
2+
3+
from django.db import models
4+
from rest_framework import serializers, viewsets
5+
6+
from tests import assert_schema, generate_schema
7+
8+
9+
class Album(models.Model):
10+
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
11+
12+
13+
class Song(models.Model):
14+
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
15+
album = models.ForeignKey(Album, on_delete=models.CASCADE, null=True)
16+
17+
18+
class SongSerializer(serializers.ModelSerializer):
19+
20+
album_id = serializers.ReadOnlyField(source='album.id')
21+
22+
class Meta:
23+
fields = ['id', 'album_id']
24+
model = Song
25+
26+
27+
class SongModelViewset(viewsets.ModelViewSet):
28+
serializer_class = SongSerializer
29+
queryset = Song.objects.none()
30+
31+
32+
def test_readonlyfield(no_warnings, django_transforms):
33+
assert_schema(
34+
generate_schema('songs', SongModelViewset),
35+
'tests/test_readonlyfield.yml',
36+
transforms=django_transforms,
37+
)

tests/test_readonlyfield.yml

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
openapi: 3.0.3
2+
info:
3+
title: ''
4+
version: 0.0.0
5+
paths:
6+
/songs/:
7+
get:
8+
operationId: songs_list
9+
tags:
10+
- songs
11+
security:
12+
- cookieAuth: []
13+
- basicAuth: []
14+
- {}
15+
responses:
16+
'200':
17+
content:
18+
application/json:
19+
schema:
20+
type: array
21+
items:
22+
$ref: '#/components/schemas/Song'
23+
description: ''
24+
post:
25+
operationId: songs_create
26+
tags:
27+
- songs
28+
requestBody:
29+
content:
30+
application/json:
31+
schema:
32+
$ref: '#/components/schemas/Song'
33+
application/x-www-form-urlencoded:
34+
schema:
35+
$ref: '#/components/schemas/Song'
36+
multipart/form-data:
37+
schema:
38+
$ref: '#/components/schemas/Song'
39+
security:
40+
- cookieAuth: []
41+
- basicAuth: []
42+
- {}
43+
responses:
44+
'201':
45+
content:
46+
application/json:
47+
schema:
48+
$ref: '#/components/schemas/Song'
49+
description: ''
50+
/songs/{id}/:
51+
get:
52+
operationId: songs_retrieve
53+
parameters:
54+
- in: path
55+
name: id
56+
schema:
57+
type: string
58+
format: uuid
59+
description: A UUID string identifying this song.
60+
required: true
61+
tags:
62+
- songs
63+
security:
64+
- cookieAuth: []
65+
- basicAuth: []
66+
- {}
67+
responses:
68+
'200':
69+
content:
70+
application/json:
71+
schema:
72+
$ref: '#/components/schemas/Song'
73+
description: ''
74+
put:
75+
operationId: songs_update
76+
parameters:
77+
- in: path
78+
name: id
79+
schema:
80+
type: string
81+
format: uuid
82+
description: A UUID string identifying this song.
83+
required: true
84+
tags:
85+
- songs
86+
requestBody:
87+
content:
88+
application/json:
89+
schema:
90+
$ref: '#/components/schemas/Song'
91+
application/x-www-form-urlencoded:
92+
schema:
93+
$ref: '#/components/schemas/Song'
94+
multipart/form-data:
95+
schema:
96+
$ref: '#/components/schemas/Song'
97+
security:
98+
- cookieAuth: []
99+
- basicAuth: []
100+
- {}
101+
responses:
102+
'200':
103+
content:
104+
application/json:
105+
schema:
106+
$ref: '#/components/schemas/Song'
107+
description: ''
108+
patch:
109+
operationId: songs_partial_update
110+
parameters:
111+
- in: path
112+
name: id
113+
schema:
114+
type: string
115+
format: uuid
116+
description: A UUID string identifying this song.
117+
required: true
118+
tags:
119+
- songs
120+
requestBody:
121+
content:
122+
application/json:
123+
schema:
124+
$ref: '#/components/schemas/PatchedSong'
125+
application/x-www-form-urlencoded:
126+
schema:
127+
$ref: '#/components/schemas/PatchedSong'
128+
multipart/form-data:
129+
schema:
130+
$ref: '#/components/schemas/PatchedSong'
131+
security:
132+
- cookieAuth: []
133+
- basicAuth: []
134+
- {}
135+
responses:
136+
'200':
137+
content:
138+
application/json:
139+
schema:
140+
$ref: '#/components/schemas/Song'
141+
description: ''
142+
delete:
143+
operationId: songs_destroy
144+
parameters:
145+
- in: path
146+
name: id
147+
schema:
148+
type: string
149+
format: uuid
150+
description: A UUID string identifying this song.
151+
required: true
152+
tags:
153+
- songs
154+
security:
155+
- cookieAuth: []
156+
- basicAuth: []
157+
- {}
158+
responses:
159+
'204':
160+
description: No response body
161+
components:
162+
schemas:
163+
PatchedSong:
164+
type: object
165+
properties:
166+
id:
167+
type: string
168+
format: uuid
169+
readOnly: true
170+
album_id:
171+
type: string
172+
format: uuid
173+
readOnly: true
174+
nullable: true
175+
Song:
176+
type: object
177+
properties:
178+
id:
179+
type: string
180+
format: uuid
181+
readOnly: true
182+
album_id:
183+
type: string
184+
format: uuid
185+
readOnly: true
186+
nullable: true
187+
required:
188+
- id
189+
- album_id
190+
securitySchemes:
191+
basicAuth:
192+
type: http
193+
scheme: basic
194+
cookieAuth:
195+
type: apiKey
196+
in: cookie
197+
name: sessionid

0 commit comments

Comments
 (0)