Browse Source

better fake_api test according to new feature and naming rule

Guénaël Muller 3 years ago
parent
commit
1dbb79c270

+ 19 - 0
example/fake_api/bottle_api.py View File

@@ -51,6 +51,24 @@ class BottleController(object):
51 51
         }
52 52
 
53 53
     @hapic.with_api_doc()
54
+    @hapic.output_body(UserSchema(
55
+        many=True,
56
+        only=('id', 'username', 'display_name', 'company')
57
+    ))
58
+    def get_users2(self):
59
+        """
60
+        Obtain users list.
61
+        """
62
+        return [
63
+            {
64
+                'id': 4,
65
+                'username': 'some_user',
66
+                'display_name': 'Damien Accorsi',
67
+                'company': 'Algoo',
68
+            }
69
+        ]
70
+
71
+    @hapic.with_api_doc()
54 72
     @hapic.input_path(UserPathSchema())
55 73
     @hapic.output_body(UserSchema())
56 74
     def get_user(self, id, hapic_data: HapicData):
@@ -99,6 +117,7 @@ class BottleController(object):
99 117
     def bind(self, app:bottle.Bottle):
100 118
         app.route('/about', callback=self.about)
101 119
         app.route('/users', callback=self.get_users)
120
+        app.route('/users2', callback=self.get_users2)
102 121
         app.route('/users/<id>', callback=self.get_user)
103 122
         app.route('/users/', callback=self.add_user,  method='POST')
104 123
         app.route('/users/<id>', callback=self.del_user, method='DELETE')

+ 21 - 0
example/fake_api/flask_api.py View File

@@ -49,6 +49,24 @@ class FlaskController(object):
49 49
         }
50 50
 
51 51
     @hapic.with_api_doc()
52
+    @hapic.output_body(UserSchema(
53
+        many=True,
54
+        only=('id', 'username', 'display_name', 'company')
55
+    ))
56
+    def get_users2(self):
57
+        """
58
+        Obtain users list.
59
+        """
60
+        return [
61
+            {
62
+                'id': 4,
63
+                'username': 'some_user',
64
+                'display_name': 'Damien Accorsi',
65
+                'company': 'Algoo',
66
+            }
67
+        ]
68
+
69
+    @hapic.with_api_doc()
52 70
     @hapic.input_path(UserPathSchema())
53 71
     @hapic.output_body(UserSchema())
54 72
     def get_user(self, id, hapic_data: HapicData):
@@ -104,6 +122,8 @@ class FlaskController(object):
104 122
                          view_func=self.about)
105 123
         app.add_url_rule('/users',
106 124
                          view_func=self.get_users)
125
+        app.add_url_rule('/users2',
126
+                         view_func=self.get_users2)
107 127
         app.add_url_rule('/users/<id>',
108 128
                          view_func=self.get_user)
109 129
         app.add_url_rule('/users/',
@@ -113,6 +133,7 @@ class FlaskController(object):
113 133
                          view_func=self.del_user,
114 134
                          methods=['DELETE'])
115 135
 
136
+
116 137
 if __name__ == "__main__":
117 138
     app = flask.Flask(__name__)
118 139
     controllers = FlaskController()

+ 21 - 0
example/fake_api/pyramid_api.py View File

@@ -49,6 +49,24 @@ class PyramidController(object):
49 49
         }
50 50
 
51 51
     @hapic.with_api_doc()
52
+    @hapic.output_body(UserSchema(
53
+        many=True,
54
+        only=('id', 'username', 'display_name', 'company')
55
+    ))
56
+    def get_users2(self, context, request):
57
+        """
58
+        Obtain users list.
59
+        """
60
+        return [
61
+            {
62
+                'id': 4,
63
+                'username': 'some_user',
64
+                'display_name': 'Damien Accorsi',
65
+                'company': 'Algoo',
66
+            }
67
+        ]
68
+
69
+    @hapic.with_api_doc()
52 70
     @hapic.input_path(UserPathSchema())
53 71
     @hapic.output_body(UserSchema())
54 72
     def get_user(self, context, request, hapic_data: HapicData):
@@ -101,6 +119,9 @@ class PyramidController(object):
101 119
         configurator.add_route('get_users', '/users', request_method='GET')  # nopep8
102 120
         configurator.add_view(self.get_users, route_name='get_users', renderer='json')  # nopep8
103 121
 
122
+        configurator.add_route('get_users2', '/users2', request_method='GET')  # nopep8
123
+        configurator.add_view(self.get_users2, route_name='get_users2', renderer='json')  # nopep8
124
+
104 125
         configurator.add_route('get_user', '/users/{id}', request_method='GET')  # nopep8
105 126
         configurator.add_view(self.get_user, route_name='get_user', renderer='json')  # nopep8
106 127
 

+ 36 - 7
tests/func/fake_api/common.py View File

@@ -1,4 +1,6 @@
1 1
 from collections import OrderedDict
2
+
3
+
2 4
 SWAGGER_DOC_API = {
3 5
  'definitions': {
4 6
      'AboutResponseSchema': {'properties': {
@@ -13,7 +15,7 @@ SWAGGER_DOC_API = {
13 15
                                         'minimum': 0,
14 16
                                         'type': 'integer'},
15 17
                             'items': {
16
-                                'items': {'$ref': '#/definitions/UserSchema'},
18
+                                'items': {'$ref': '#/definitions/UserSchema_without_email_address_first_name_last_name'},
17 19
                                 'type': 'array'},
18 20
                             'pagination': {'$ref': '#/definitions/PaginationSchema'}},
19 21
                          'required': ['item_nb'],
@@ -41,10 +43,27 @@ SWAGGER_DOC_API = {
41 43
                                  'id',
42 44
                                  'last_name',
43 45
                                  'username'],
44
-                    'type': 'object'}},
45
- 'info': {'description': 'just an example of hapic API',
46
-          'title': 'Fake API',
47
-          'version': '1.0.0'},
46
+                    'type': 'object'},
47
+    'UserSchema_without_id': {
48
+        'properties': {
49
+            'username': {'type': 'string'},
50
+            'display_name': {'type': 'string'},
51
+            'company': {'type': 'string'},
52
+            'last_name': {'type': 'string'},
53
+            'email_address': {'format': 'email', 'type': 'string'},
54
+            'first_name': {'type': 'string'}},
55
+        'required': ['company', 'display_name', 'email_address', 'first_name',
56
+                     'last_name', 'username'], 'type': 'object'},
57
+    'UserSchema_without_email_address_first_name_last_name': {
58
+        'properties': {
59
+            'username': {'type': 'string'},
60
+            'id': {'format': 'int32', 'type': 'integer'},
61
+            'company': {'type': 'string'},
62
+            'display_name': {'type': 'string'}},
63
+        'required': ['company', 'display_name', 'id', 'username'], 'type': 'object'
64
+    },
65
+    },
66
+ 'info': {'description': 'just an example of hapic API', 'title': 'Fake API', 'version': '1.0.0'},
48 67
  'parameters': {},
49 68
  'paths': OrderedDict(
50 69
      [('/about', {
@@ -83,10 +102,20 @@ SWAGGER_DOC_API = {
83 102
              'description': 'Add new user',
84 103
              'parameters': [{'in': 'body',
85 104
                              'name': 'body',
86
-                             'schema': {'$ref': '#/definitions/UserSchema'}}],
105
+                             'schema': {'$ref': '#/definitions/UserSchema_without_id'}}],
87 106
              'responses': {200: {
88 107
                  'description': '200',
89
-                 'schema': {'$ref': '#/definitions/UserSchema'}}}}})]),
108
+                 'schema': {'$ref': '#/definitions/UserSchema'}}}}}),
109
+      ( '/users2', {
110
+          'get': {
111
+              'description': 'Obtain users list.',
112
+              'responses': {200: {
113
+                  'description': '200',
114
+                  'schema': {'type': 'array',
115
+                             'items': {'$ref': '#/definitions/UserSchema_without_email_address_first_name_last_name'}
116
+                             }
117
+          }}}}
118
+        )]),
90 119
  'swagger': '2.0',
91 120
  'tags': []
92 121
 }

+ 106 - 9
tests/func/fake_api/test_fake_api.py View File

@@ -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']