Browse Source

add test for schema_in_doc with only, exclude, many case

Guénaël Muller 6 years ago
parent
commit
a24a5f4112
1 changed files with 146 additions and 0 deletions
  1. 146 0
      tests/func/test_doc.py

+ 146 - 0
tests/func/test_doc.py View File

@@ -221,3 +221,149 @@ class TestDocGeneration(Base):
221 221
             .get('properties', {})\
222 222
             .get('category', {})\
223 223
             .get('enum')
224
+
225
+    def test_func__schema_in_doc__ok__nominal_case(self):
226
+        hapic = Hapic()
227
+        app = bottle.Bottle()
228
+        hapic.set_context(MyContext(app=app))
229
+
230
+        class MySchema(marshmallow.Schema):
231
+            name = marshmallow.fields.String(required=True)
232
+
233
+        @hapic.with_api_doc()
234
+        @hapic.input_body(MySchema())
235
+        def my_controller():
236
+            return {'name': 'test',}
237
+
238
+        app.route('/paper', method='POST', callback=my_controller)
239
+        doc = hapic.generate_doc()
240
+
241
+        assert doc.get('definitions', {}).get('MySchema', {})
242
+        schema_def = doc.get('definitions', {}).get('MySchema', {})
243
+        assert schema_def.get('properties', {}).get('name', {}).get('type')
244
+
245
+        assert doc.get('paths').get('/paper').get('post').get('parameters')[0]
246
+        schema_ref = doc.get('paths').get('/paper').get('post').get('parameters')[0]
247
+        assert schema_ref.get('in') == 'body'
248
+        assert schema_ref.get('name') == 'body'
249
+        assert schema_ref['schema']['$ref'] == '#/definitions/MySchema'
250
+
251
+    def test_func__schema_in_doc__ok__many_case(self):
252
+        hapic = Hapic()
253
+        app = bottle.Bottle()
254
+        hapic.set_context(MyContext(app=app))
255
+
256
+        class MySchema(marshmallow.Schema):
257
+            name = marshmallow.fields.String(required=True)
258
+
259
+        @hapic.with_api_doc()
260
+        @hapic.input_body(MySchema(many=True))
261
+        def my_controller():
262
+            return {'name': 'test'}
263
+
264
+        app.route('/paper', method='POST', callback=my_controller)
265
+        doc = hapic.generate_doc()
266
+
267
+        assert doc.get('definitions', {}).get('MySchema', {})
268
+        schema_def = doc.get('definitions', {}).get('MySchema', {})
269
+        assert schema_def.get('properties', {}).get('name', {}).get('type')
270
+
271
+        assert doc.get('paths').get('/paper').get('post').get('parameters')[0]
272
+        schema_ref = doc.get('paths').get('/paper').get('post').get('parameters')[0]
273
+        assert schema_ref.get('in') == 'body'
274
+        assert schema_ref.get('name') == 'body'
275
+        assert schema_ref['schema'] == {
276
+            'items': {'$ref': '#/definitions/MySchema'},
277
+            'type': 'array'
278
+        }
279
+
280
+    def test_func__schema_in_doc__ok__exclude_case(self):
281
+        hapic = Hapic()
282
+        app = bottle.Bottle()
283
+        hapic.set_context(MyContext(app=app))
284
+
285
+        class MySchema(marshmallow.Schema):
286
+            name = marshmallow.fields.String(required=True)
287
+            name2 = marshmallow.fields.String(required=True)
288
+
289
+        @hapic.with_api_doc()
290
+        @hapic.input_body(MySchema(exclude=('name2',)))
291
+        def my_controller():
292
+            return {'name': 'test',}
293
+
294
+        app.route('/paper', method='POST', callback=my_controller)
295
+        doc = hapic.generate_doc()
296
+
297
+        definitions = doc.get('definitions', {})
298
+        # TODO - G-M - Find better way to find our new schema
299
+        # Do Better test when we were able to set correctly schema name
300
+        # according to content
301
+        for elem in definitions.keys():
302
+            if elem != 'MySchema':
303
+                schema_name = elem
304
+                break
305
+        schema_def = definitions[schema_name]
306
+        assert schema_def.get('properties', {}).get('name', {}).get('type') == 'string'
307
+        assert doc.get('paths').get('/paper').get('post').get('parameters')[0]
308
+        schema_ref = doc.get('paths').get('/paper').get('post').get('parameters')[0]
309
+        assert schema_ref.get('in') == 'body'
310
+        assert schema_ref['schema']['$ref'] == '#/definitions/{}'.format(schema_name)
311
+
312
+        @hapic.with_api_doc()
313
+        @hapic.input_body(MySchema(only=('name',)))
314
+        def my_controller():
315
+            return {'name': 'test'}
316
+
317
+        app.route('/paper', method='POST', callback=my_controller)
318
+        doc = hapic.generate_doc()
319
+
320
+        # TODO - G-M - Find better way to find our new schema
321
+        # Do Better test when we were able to set correctly schema name
322
+        # according to content
323
+        definitions = doc.get('definitions', {})
324
+        for elem in definitions.keys():
325
+            if elem != 'MySchema':
326
+                schema_name = elem
327
+                break
328
+        assert schema_name
329
+        schema_def = definitions[schema_name]
330
+        assert schema_def.get('properties', {}).get('name', {}).get('type') == 'string'
331
+        assert doc.get('paths').get('/paper').get('post').get('parameters')[0]
332
+        schema_ref = doc.get('paths').get('/paper').get('post').get('parameters')[0]
333
+        assert schema_ref.get('in') == 'body'
334
+        assert schema_ref['schema']['$ref'] == '#/definitions/{}'.format(schema_name)
335
+
336
+    def test_func__schema_in_doc__ok__many_and_exclude_case(self):
337
+        hapic = Hapic()
338
+        app = bottle.Bottle()
339
+        hapic.set_context(MyContext(app=app))
340
+
341
+        class MySchema(marshmallow.Schema):
342
+            name = marshmallow.fields.String(required=True)
343
+            name2 = marshmallow.fields.String(required=True)
344
+
345
+        @hapic.with_api_doc()
346
+        @hapic.input_body(MySchema(exclude=('name2',), many=True))
347
+        def my_controller():
348
+            return {'name': 'test',}
349
+
350
+        app.route('/paper', method='POST', callback=my_controller)
351
+        doc = hapic.generate_doc()
352
+
353
+        definitions = doc.get('definitions', {})
354
+        # TODO - G-M - Find better way to find our new schema
355
+        # Do Better test when we were able to set correctly schema name
356
+        # according to content
357
+        for elem in definitions.keys():
358
+            if elem != 'MySchema':
359
+                schema_name = elem
360
+                break
361
+        schema_def = definitions[schema_name]
362
+        assert schema_def.get('properties', {}).get('name', {}).get('type') == 'string'
363
+        assert doc.get('paths').get('/paper').get('post').get('parameters')[0]
364
+        schema_ref = doc.get('paths').get('/paper').get('post').get('parameters')[0]
365
+        assert schema_ref.get('in') == 'body'
366
+        assert schema_ref['schema'] == {
367
+            'items': {'$ref': '#/definitions/{}'.format(schema_name)},
368
+            'type': 'array'
369
+        }