-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_ext_flask.py
More file actions
359 lines (295 loc) · 10.7 KB
/
test_ext_flask.py
File metadata and controls
359 lines (295 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
import pytest
from flask import Flask
from flask.views import MethodView
from apispec import APISpec
from apispec_webframeworks.flask import FlaskPlugin, DocumentedBlueprint
from .utils import get_paths
@pytest.fixture(params=('2.0', '3.0.0'))
def spec(request):
return APISpec(
title='Swagger Petstore',
version='1.0.0',
openapi_version=request.param,
plugins=(FlaskPlugin(),),
)
@pytest.yield_fixture()
def app():
_app = Flask(__name__)
ctx = _app.test_request_context()
ctx.push()
yield _app
ctx.pop()
class TestPathHelpers:
def test_path_from_view(self, app, spec):
@app.route('/hello')
def hello():
return 'hi'
spec.path(
view=hello,
operations={'get': {'parameters': [], 'responses': {'200': {}}}},
)
paths = get_paths(spec)
assert '/hello' in paths
assert 'get' in paths['/hello']
expected = {'parameters': [], 'responses': {'200': {}}}
assert paths['/hello']['get'] == expected
def test_path_from_method_view(self, app, spec):
class HelloApi(MethodView):
"""Greeting API.
---
x-extension: global metadata
"""
def get(self):
"""A greeting endpoint.
---
description: get a greeting
responses:
200:
description: said hi
"""
return 'hi'
def post(self):
return 'hi'
method_view = HelloApi.as_view('hi')
app.add_url_rule('/hi', view_func=method_view, methods=('GET', 'POST'))
spec.path(view=method_view)
expected = {
'description': 'get a greeting',
'responses': {200: {'description': 'said hi'}},
}
paths = get_paths(spec)
assert paths['/hi']['get'] == expected
assert paths['/hi']['post'] == {}
assert paths['/hi']['x-extension'] == 'global metadata'
def test_path_with_multiple_methods(self, app, spec):
@app.route('/hello', methods=['GET', 'POST'])
def hello():
return 'hi'
spec.path(
view=hello, operations=dict(
get={'description': 'get a greeting', 'responses': {'200': {}}},
post={'description': 'post a greeting', 'responses': {'200': {}}},
),
)
paths = get_paths(spec)
get_op = paths['/hello']['get']
post_op = paths['/hello']['post']
assert get_op['description'] == 'get a greeting'
assert post_op['description'] == 'post a greeting'
def test_methods_from_rule(self, app, spec):
class HelloApi(MethodView):
"""Greeting API.
---
x-extension: global metadata
"""
def get(self):
"""A greeting endpoint.
---
description: get a greeting
responses:
200:
description: said hi
"""
return 'hi'
def post(self):
return 'hi'
def delete(self):
return 'hi'
method_view = HelloApi.as_view('hi')
app.add_url_rule('/hi', view_func=method_view, methods=('GET', 'POST'))
spec.path(view=method_view)
paths = get_paths(spec)
assert 'get' in paths['/hi']
assert 'post' in paths['/hi']
assert 'delete' not in paths['/hi']
def test_integration_with_docstring_introspection(self, app, spec):
@app.route('/hello')
def hello():
"""A greeting endpoint.
---
x-extension: value
get:
description: get a greeting
responses:
200:
description: a pet to be returned
schema:
$ref: #/definitions/Pet
post:
description: post a greeting
responses:
200:
description: some data
foo:
description: not a valid operation
responses:
200:
description:
more junk
"""
return 'hi'
spec.path(view=hello)
paths = get_paths(spec)
get_op = paths['/hello']['get']
post_op = paths['/hello']['post']
extension = paths['/hello']['x-extension']
assert get_op['description'] == 'get a greeting'
assert post_op['description'] == 'post a greeting'
assert 'foo' not in paths['/hello']
assert extension == 'value'
def test_path_is_translated_to_swagger_template(self, app, spec):
@app.route('/pet/<pet_id>')
def get_pet(pet_id):
return 'representation of pet {pet_id}'.format(pet_id=pet_id)
spec.path(view=get_pet)
assert '/pet/{pet_id}' in get_paths(spec)
class TestDocumentedBlueprint:
def test_document_document_true(self, app, spec):
documented_blueprint = DocumentedBlueprint('test', __name__, spec)
@documented_blueprint.route('/test', documented=True)
def test():
return 'Hello'
app.register_blueprint(documented_blueprint)
assert '/test' in get_paths(spec)
def test_document_document_false(self, app, spec):
documented_blueprint = DocumentedBlueprint('test', __name__, spec)
@documented_blueprint.route('/test', documented=False)
def test():
return 'Hello'
app.register_blueprint(documented_blueprint)
assert '/test' not in get_paths(spec)
def test_docstring_introspection(self, app, spec):
documented_blueprint = DocumentedBlueprint('test', __name__, spec)
@documented_blueprint.route('/test')
def test():
"""A test endpoint.
---
get:
description: Test description
responses:
200:
description: Test OK answer
"""
return 'Test'
app.register_blueprint(documented_blueprint)
paths = get_paths(spec)
assert '/test' in paths
get_op = paths['/test']['get']
assert get_op['description'] == 'Test description'
def test_docstring_introspection_multiple_routes(self, app, spec):
documented_blueprint = DocumentedBlueprint('test', __name__, spec)
@documented_blueprint.route('/test')
def test_get():
"""A test endpoint.
---
get:
description: Get test description
responses:
200:
description: Test OK answer
"""
return 'Test'
@documented_blueprint.route('/test', methods=['POST'])
def test_post():
"""A test endpoint.
---
post:
description: Post test description
responses:
200:
description: Test OK answer
"""
return 'Test'
app.register_blueprint(documented_blueprint)
paths = get_paths(spec)
assert '/test' in paths
get_op = paths['/test']['get']
post_op = paths['/test']['post']
assert get_op['description'] == 'Get test description'
assert post_op['description'] == 'Post test description'
def test_docstring_introspection_multiple_http_methods(self, app, spec):
documented_blueprint = DocumentedBlueprint('test', __name__, spec)
@documented_blueprint.route('/test', methods=['GET', 'POST'])
def test_get():
"""A test endpoint.
---
get:
description: Get test description
responses:
200:
description: Test OK answer
post:
description: Post test description
responses:
200:
description: Test OK answer
"""
return 'Test'
app.register_blueprint(documented_blueprint)
paths = get_paths(spec)
assert '/test' in paths
get_op = paths['/test']['get']
post_op = paths['/test']['post']
assert get_op['description'] == 'Get test description'
assert post_op['description'] == 'Post test description'
def test_docstring_introspection_add_url_rule(self, app, spec):
documented_blueprint = DocumentedBlueprint('test', __name__, spec)
@documented_blueprint.route('/')
def index():
"""
Gist detail view.
---
x-extension: metadata
get:
description: Get gist detail
responses:
200:
schema:
$ref: '#/definitions/Gist'
"""
return 'index'
documented_blueprint.add_url_rule('/', view_func=index, methods=['POST'])
app.register_blueprint(documented_blueprint)
paths = get_paths(spec)
assert '/' in paths
get_op = paths['/']['get']
assert get_op['description'] == 'Get gist detail'
def test_docstring_introspection_methodview(self, app, spec):
documented_blueprint = DocumentedBlueprint('test', __name__, spec)
class Crud(MethodView):
"""
Crud methodview.
---
x-extension: global metadata
"""
def get(self):
"""
Crud get view.
---
description: Crud get view.
responses:
200:
schema:
$ref: '#/definitions/Crud'
"""
return 'crud_get'
def post(self):
"""
Crud post view.
---
description: Crud post view.
responses:
200:
schema:
$ref: '#/definitions/Crud'
"""
return 'crud_get'
documented_blueprint.add_url_rule('/crud', view_func=Crud.as_view('crud_view'))
app.register_blueprint(documented_blueprint)
paths = get_paths(spec)
assert '/crud' in paths
get_op = paths['/crud']['get']
post_op = paths['/crud']['post']
assert get_op['description'] == 'Crud get view.'
assert post_op['description'] == 'Crud post view.'