|
@@ -1,25 +1,68 @@
|
|
1
|
+import pytest
|
|
2
|
+from flask import Flask
|
|
3
|
+from pyramid.config import Configurator
|
1
|
4
|
from webtest import TestApp
|
2
|
5
|
from bottle import Bottle
|
3
|
6
|
|
|
7
|
+from example.fake_api.flask_api import FlaskController
|
|
8
|
+from example.fake_api.pyramid_api import PyramidController
|
4
|
9
|
from hapic.ext.bottle import BottleContext
|
5
|
10
|
from example.fake_api.bottle_api import BottleController
|
|
11
|
+from hapic.ext.flask import FlaskContext
|
|
12
|
+from hapic.ext.pyramid import PyramidContext
|
6
|
13
|
from tests.func.fake_api.common import SWAGGER_DOC_API
|
7
|
|
-from example.fake_api.bottle_api import hapic
|
8
|
14
|
|
9
|
15
|
|
10
|
|
-def test_func_bottle_fake_api():
|
|
16
|
+def get_bottle_context():
|
|
17
|
+ from example.fake_api.bottle_api import hapic as h
|
11
|
18
|
bottle_app = Bottle()
|
|
19
|
+ h.reset_context()
|
|
20
|
+ h.set_context(BottleContext(bottle_app))
|
12
|
21
|
controllers = BottleController()
|
13
|
22
|
controllers.bind(bottle_app)
|
|
23
|
+ return {
|
|
24
|
+ 'hapic': h,
|
|
25
|
+ 'app': bottle_app,
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+def get_flask_context():
|
|
30
|
+ from example.fake_api.flask_api import hapic as h
|
|
31
|
+ flask_app = Flask(__name__)
|
|
32
|
+ controllers = FlaskController()
|
|
33
|
+ controllers.bind(flask_app)
|
|
34
|
+ h.reset_context()
|
|
35
|
+ h.set_context(FlaskContext(flask_app))
|
|
36
|
+ return {
|
|
37
|
+ 'hapic': h,
|
|
38
|
+ 'app': flask_app,
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+def get_pyramid_context():
|
|
43
|
+ from example.fake_api.pyramid_api import hapic as h
|
|
44
|
+ configurator = Configurator(autocommit=True)
|
|
45
|
+ controllers = PyramidController()
|
|
46
|
+ controllers.bind(configurator)
|
|
47
|
+ h.reset_context()
|
|
48
|
+ h.set_context(PyramidContext(configurator))
|
|
49
|
+ pyramid_app = configurator.make_wsgi_app()
|
|
50
|
+ return {
|
|
51
|
+ 'hapic': h,
|
|
52
|
+ 'app': pyramid_app,
|
|
53
|
+ }
|
14
|
54
|
|
15
|
|
- hapic.set_context(BottleContext(bottle_app))
|
16
|
|
- app = TestApp(bottle_app)
|
17
|
|
- doc = hapic.generate_doc(
|
18
|
|
- title='Fake API',
|
19
|
|
- description='just an example of hapic API'
|
20
|
|
- )
|
21
|
55
|
|
22
|
|
- assert doc == SWAGGER_DOC_API
|
|
56
|
+@pytest.mark.parametrize('context',
|
|
57
|
+ [
|
|
58
|
+ get_bottle_context(),
|
|
59
|
+ get_flask_context(),
|
|
60
|
+ get_pyramid_context()
|
|
61
|
+ ])
|
|
62
|
+def test_func__test_fake_api_endpoints_ok__all_framework(context):
|
|
63
|
+ hapic = context['hapic']
|
|
64
|
+ app = context['app']
|
|
65
|
+ app = TestApp(app)
|
23
|
66
|
resp = app.get('/about')
|
24
|
67
|
assert resp.status_int == 200
|
25
|
68
|
assert resp.json == {'datetime': '2017-12-07T10:55:08.488996+00:00',
|
|
@@ -44,6 +87,17 @@ def test_func_bottle_fake_api():
|
44
|
87
|
'item_nb': 1,
|
45
|
88
|
}
|
46
|
89
|
|
|
90
|
+ resp = app.get('/users2')
|
|
91
|
+ assert resp.status_int == 200
|
|
92
|
+ assert resp.json == [
|
|
93
|
+ {
|
|
94
|
+ 'username': 'some_user',
|
|
95
|
+ 'id': 4,
|
|
96
|
+ 'display_name': 'Damien Accorsi',
|
|
97
|
+ 'company': 'Algoo'
|
|
98
|
+ }
|
|
99
|
+ ]
|
|
100
|
+
|
47
|
101
|
resp = app.get('/users/1')
|
48
|
102
|
assert resp.status_int == 200
|
49
|
103
|
assert resp.json == {
|
|
@@ -93,3 +147,46 @@ def test_func_bottle_fake_api():
|
93
|
147
|
resp = app.delete('/users/1', status='*')
|
94
|
148
|
assert resp.status_int == 204
|
95
|
149
|
|
|
150
|
+
|
|
151
|
+@pytest.mark.parametrize('context',
|
|
152
|
+ [
|
|
153
|
+ get_bottle_context(),
|
|
154
|
+ get_flask_context(),
|
|
155
|
+ get_pyramid_context()
|
|
156
|
+ ])
|
|
157
|
+def test_func__test_fake_api_doc_ok__all_framework(context):
|
|
158
|
+ hapic = context['hapic']
|
|
159
|
+ app = context['app']
|
|
160
|
+ app = TestApp(app)
|
|
161
|
+ doc = hapic.generate_doc(
|
|
162
|
+ title='Fake API',
|
|
163
|
+ description='just an example of hapic API'
|
|
164
|
+ )
|
|
165
|
+
|
|
166
|
+ assert doc['info'] == SWAGGER_DOC_API['info']
|
|
167
|
+ assert doc['tags'] == SWAGGER_DOC_API['tags']
|
|
168
|
+ assert doc['swagger'] == SWAGGER_DOC_API['swagger']
|
|
169
|
+ assert doc['parameters'] == SWAGGER_DOC_API['parameters']
|
|
170
|
+ assert doc['paths']['/about'] == SWAGGER_DOC_API['paths']['/about']
|
|
171
|
+ assert doc['paths']['/users'] == SWAGGER_DOC_API['paths']['/users']
|
|
172
|
+ assert doc['paths']['/users/{id}'] == SWAGGER_DOC_API['paths']['/users/{id}']
|
|
173
|
+ assert doc['paths']['/users/'] == SWAGGER_DOC_API['paths']['/users/']
|
|
174
|
+ assert doc['paths']['/users2'] == SWAGGER_DOC_API['paths']['/users2']
|
|
175
|
+
|
|
176
|
+ assert doc['definitions']['AboutResponseSchema'] == \
|
|
177
|
+ SWAGGER_DOC_API['definitions']['AboutResponseSchema']
|
|
178
|
+ assert doc['definitions']['ListsUserSchema'] == \
|
|
179
|
+ SWAGGER_DOC_API['definitions']['ListsUserSchema']
|
|
180
|
+ assert doc['definitions']['NoContentSchema'] == \
|
|
181
|
+ SWAGGER_DOC_API['definitions']['NoContentSchema']
|
|
182
|
+ assert doc['definitions']['PaginationSchema'] == \
|
|
183
|
+ SWAGGER_DOC_API['definitions']['PaginationSchema']
|
|
184
|
+ assert doc['definitions']['UserSchema'] == \
|
|
185
|
+ SWAGGER_DOC_API['definitions']['UserSchema']
|
|
186
|
+ assert doc['definitions']['UserSchema'] == \
|
|
187
|
+ SWAGGER_DOC_API['definitions']['UserSchema']
|
|
188
|
+
|
|
189
|
+ assert doc['definitions']['UserSchema_without_id'] == \
|
|
190
|
+ SWAGGER_DOC_API['definitions']['UserSchema_without_id']
|
|
191
|
+ assert doc['definitions']['UserSchema_without_email_address_first_name_last_name'] == \
|
|
192
|
+ SWAGGER_DOC_API['definitions']['UserSchema_without_email_address_first_name_last_name']
|