@@ -17,7 +17,7 @@ def app():
17
17
@pytest .fixture (scope = "function" , params = ("2.0" , "3.0.0" ))
18
18
def spec (request ):
19
19
return APISpec (
20
- title = "Test Suite " ,
20
+ title = "Swagger Petstore " ,
21
21
version = "1.0.0" ,
22
22
openapi_version = request .param ,
23
23
plugins = (FlaskPlugin (),),
@@ -26,223 +26,207 @@ def spec(request):
26
26
27
27
class TestFlaskPlugin :
28
28
def test_function_view (self , app , spec ):
29
- @app .route ("/hello " )
30
- def greeting ():
31
- return "hello "
29
+ @app .route ("/pet " )
30
+ def pet ():
31
+ return "Max "
32
32
33
33
operations = {
34
34
"get" : {
35
35
"parameters" : [],
36
- "responses" : {"200" : {"description" : "get a greeting " }},
36
+ "responses" : {"200" : {"description" : "get a pet " }},
37
37
}
38
38
}
39
- spec .path (view = greeting , operations = operations )
39
+ spec .path (view = pet , operations = operations )
40
40
paths = utils .get_paths (spec )
41
- assert "get" in paths ["/hello " ]
42
- assert paths ["/hello " ] == operations
41
+ assert "get" in paths ["/pet " ]
42
+ assert paths ["/pet " ] == operations
43
43
44
44
def test_method_view (self , app , spec ):
45
- class GreetingView (MethodView ):
45
+ class PetView (MethodView ):
46
46
"""The greeting view.
47
47
---
48
48
x-extension: global metadata
49
49
"""
50
50
51
51
def get (self ):
52
- """A greeting endpoint .
52
+ """Get a pet's name .
53
53
---
54
- description: get a greeting
54
+ description: get a pet's name
55
55
responses:
56
56
200:
57
- description: received greeting
57
+ description: the pet's name
58
58
"""
59
- return "hello "
59
+ return "Max "
60
60
61
61
def post (self ):
62
62
return {}
63
63
64
- method_view = GreetingView .as_view ("hello " )
65
- app .add_url_rule ("/hello " , view_func = method_view , methods = ("GET" , "POST" ))
64
+ method_view = PetView .as_view ("pet " )
65
+ app .add_url_rule ("/pet " , view_func = method_view , methods = ("GET" , "POST" ))
66
66
spec .path (view = method_view )
67
67
paths = utils .get_paths (spec )
68
- assert paths ["/hello " ]["get" ] == {
69
- "summary" : "A greeting endpoint ." ,
70
- "description" : "get a greeting " ,
71
- "responses" : {"200" : {"description" : "received greeting " }},
68
+ assert paths ["/pet " ]["get" ] == {
69
+ "summary" : "Get a pet's name ." ,
70
+ "description" : "get a pet's name " ,
71
+ "responses" : {"200" : {"description" : "the pet's name " }},
72
72
}
73
- assert paths ["/hello " ]["post" ] == {}
74
- assert paths ["/hello " ]["x-extension" ] == "global metadata"
73
+ assert paths ["/pet " ]["post" ] == {}
74
+ assert paths ["/pet " ]["x-extension" ] == "global metadata"
75
75
76
76
def test_path_with_multiple_methods (self , app , spec ):
77
- @app .route ("/hello " , methods = ["GET" , "POST" ])
78
- def greeting ():
79
- return "hello "
77
+ @app .route ("/pet " , methods = ["GET" , "POST" ])
78
+ def pet ():
79
+ return "Max "
80
80
81
81
spec .path (
82
- view = greeting ,
82
+ view = pet ,
83
83
operations = {
84
- "get" : {"description" : "get a greeting " , "responses" : {"200" : {}}},
85
- "post" : {"description" : "post a greeting " , "responses" : {"200" : {}}},
84
+ "get" : {"description" : "get a pet's name " , "responses" : {"200" : {}}},
85
+ "post" : {"description" : "register a pet " , "responses" : {"200" : {}}},
86
86
},
87
87
)
88
88
paths = utils .get_paths (spec )
89
- get_op = paths ["/hello " ]["get" ]
90
- post_op = paths ["/hello " ]["post" ]
91
- assert get_op ["description" ] == "get a greeting "
92
- assert post_op ["description" ] == "post a greeting "
89
+ get_op = paths ["/pet " ]["get" ]
90
+ post_op = paths ["/pet " ]["post" ]
91
+ assert get_op ["description" ] == "get a pet's name "
92
+ assert post_op ["description" ] == "register a pet "
93
93
94
94
def test_methods_from_rule (self , app , spec ):
95
- class GreetingView (MethodView ):
96
- """The greeting view."""
95
+ class PetView (MethodView ):
96
+ """A view for pets ."""
97
97
98
98
def get (self ):
99
- """A greeting endpoint .
99
+ """Get a pet's name .
100
100
---
101
- description: get a greeting
101
+ description: get a pet's name
102
102
responses:
103
103
200:
104
- description: received greeting
104
+ description: the pet's name
105
105
"""
106
- return "hello "
106
+ return "Max "
107
107
108
108
def post (self ):
109
109
return {}
110
110
111
111
def delete (self ):
112
112
return {}
113
113
114
- method_view = GreetingView .as_view ("hello " )
115
- app .add_url_rule ("/hello " , view_func = method_view , methods = ("GET" , "POST" ))
114
+ method_view = PetView .as_view ("pet " )
115
+ app .add_url_rule ("/pet " , view_func = method_view , methods = ("GET" , "POST" ))
116
116
spec .path (view = method_view )
117
117
paths = utils .get_paths (spec )
118
- assert "get" in paths ["/hello " ]
119
- assert "post" in paths ["/hello " ]
120
- assert "delete" not in paths ["/hello " ]
118
+ assert "get" in paths ["/pet " ]
119
+ assert "post" in paths ["/pet " ]
120
+ assert "delete" not in paths ["/pet " ]
121
121
122
122
def test_docstring_introspection (self , app , spec ):
123
- @app .route ("/hello " )
124
- def greeting ():
125
- """A greeting endpoint .
123
+ @app .route ("/pet " )
124
+ def pet ():
125
+ """Get a pet's name .
126
126
---
127
127
x-extension: value
128
128
get:
129
- description: get a greeting
129
+ description: get a pet's name
130
130
responses:
131
131
200:
132
- description: received greeting
132
+ description: the pet's name
133
133
post:
134
- description: post a greeting
134
+ description: register a pet
135
135
responses:
136
136
200:
137
- description: delivered greeting
137
+ description: the registered pet's name
138
138
foo:
139
139
description: not a valid operation
140
140
"""
141
- return "hello "
141
+ return "Max "
142
142
143
- spec .path (view = greeting )
143
+ spec .path (view = pet )
144
144
paths = utils .get_paths (spec )
145
- assert paths ["/hello " ]["x-extension" ] == "value"
146
- assert paths ["/hello " ]["get" ] == {
147
- "description" : "get a greeting " ,
148
- "responses" : {"200" : {"description" : "received greeting " }},
145
+ assert paths ["/pet " ]["x-extension" ] == "value"
146
+ assert paths ["/pet " ]["get" ] == {
147
+ "description" : "get a pet's name " ,
148
+ "responses" : {"200" : {"description" : "the pet's name " }},
149
149
}
150
- assert paths ["/hello " ]["post" ] == {
151
- "description" : "post a greeting " ,
152
- "responses" : {"200" : {"description" : "delivered greeting " }},
150
+ assert paths ["/pet " ]["post" ] == {
151
+ "description" : "register a pet " ,
152
+ "responses" : {"200" : {"description" : "the registered pet's name " }},
153
153
}
154
- assert "foo" not in paths ["/hello " ]
154
+ assert "foo" not in paths ["/pet " ]
155
155
156
156
def test_specs_from_decorator (self , app , spec ):
157
- class GreetingView (MethodView ):
157
+ class PetView (MethodView ):
158
158
@spec_from (
159
159
{
160
- "description" : "get a greeting " ,
161
- "responses" : {200 : {"description" : "received greeting " }},
160
+ "description" : "get a pet's name " ,
161
+ "responses" : {" 200" : {"description" : "the pet's name " }},
162
162
}
163
163
)
164
164
def get (self ):
165
- """A greeting endpoint ."""
166
- return "hello "
165
+ """Get a pet's name ."""
166
+ return "Max "
167
167
168
- method_view = GreetingView .as_view ("hello " )
169
- app .add_url_rule ("/hello " , view_func = method_view )
168
+ method_view = PetView .as_view ("pet " )
169
+ app .add_url_rule ("/pet " , view_func = method_view )
170
170
spec .path (view = method_view )
171
171
paths = utils .get_paths (spec )
172
- assert paths ["/hello " ]["get" ] == {
173
- "summary" : "A greeting endpoint ." ,
174
- "description" : "get a greeting " ,
175
- "responses" : {"200" : {"description" : "received greeting " }},
172
+ assert paths ["/pet " ]["get" ] == {
173
+ "summary" : "Get a pet's name ." ,
174
+ "description" : "get a pet's name " ,
175
+ "responses" : {"200" : {"description" : "the pet's name " }},
176
176
}
177
177
178
178
def test_path_is_translated_to_swagger_template (self , app , spec ):
179
- @app .route ("/hello/<user_id >" )
180
- def hello_user ( user_id ):
181
- return f"greeting sent to user { user_id } "
179
+ @app .route ("/pet/<name >" )
180
+ def pet ( name ):
181
+ return name
182
182
183
- spec .path (view = hello_user )
184
- assert "/hello/{user_id }" in utils .get_paths (spec )
183
+ spec .path (view = pet )
184
+ assert "/pet/{name }" in utils .get_paths (spec )
185
185
186
186
def test_explicit_app_kwarg (self , spec ):
187
187
app = Flask (__name__ )
188
188
189
- @app .route ("/bye " )
190
- def bye ():
191
- return "bye "
189
+ @app .route ("/pet " )
190
+ def pet ():
191
+ return "Max "
192
192
193
- spec .path (view = bye , app = app )
194
- assert "/bye " in utils .get_paths (spec )
193
+ spec .path (view = pet , app = app )
194
+ assert "/pet " in utils .get_paths (spec )
195
195
196
196
def test_auto_responses (self , app , spec ):
197
- class ResponsesView (MethodView ):
198
- """The greeting view."""
197
+ class PetView (MethodView ):
198
+ """A view for pets ."""
199
199
200
200
def get (self ):
201
- """A greeting endpoint .
201
+ """Get a pet's name .
202
202
---
203
- description: get a greeting
203
+ description: get a pet's name
204
204
responses:
205
205
200:
206
- description: received greeting
206
+ description: the pet's name
207
207
400:
208
208
404:
209
- description: greeting not found
209
+ description: pet not found
210
210
default:
211
211
description: unexpected error
212
212
"""
213
- return "hello "
213
+ return "Max "
214
214
215
- method_view = ResponsesView .as_view ("responses " )
216
- app .add_url_rule ("/hello " , view_func = method_view )
215
+ method_view = PetView .as_view ("pet " )
216
+ app .add_url_rule ("/pet " , view_func = method_view )
217
217
spec .path (view = method_view )
218
218
paths = utils .get_paths (spec )
219
219
220
- assert paths ["/hello " ]["get" ] == {
221
- "summary" : "A greeting endpoint ." ,
222
- "description" : "get a greeting " ,
220
+ assert paths ["/pet " ]["get" ] == {
221
+ "summary" : "Get a pet's name ." ,
222
+ "description" : "get a pet's name " ,
223
223
"responses" : {
224
- "200" : {"description" : "received greeting" },
225
- "400" : {
226
- "$ref" : "#/responses/BadRequest"
227
- if spec .openapi_version .major < 3
228
- else "#/components/responses/BadRequest"
229
- },
230
- "404" : {"description" : "greeting not found" },
224
+ "200" : {"description" : "the pet's name" },
225
+ "400" : utils .build_ref (spec , "response" , "BadRequest" ),
226
+ "404" : {"description" : "pet not found" },
231
227
"default" : {"description" : "unexpected error" },
232
228
},
233
229
}
234
230
235
- if spec .openapi_version .major < 3 :
236
- assert spec .to_dict ()["responses" ] == {
237
- "BadRequest" : {"schema" : {"$ref" : "#/definitions/HTTPResponse" }}
238
- }
239
- else :
240
- assert utils .get_responses (spec ) == {
241
- "BadRequest" : {
242
- "content" : {
243
- "application/json" : {
244
- "schema" : {"$ref" : "#/components/schemas/HTTPResponse" }
245
- }
246
- }
247
- },
248
- }
231
+ ref = utils .build_ref (spec , "schema" , "HTTPResponse" )
232
+ assert utils .get_schema (spec , utils .get_responses (spec )["BadRequest" ]) == ref
0 commit comments