Skip to content

Commit c277980

Browse files
committed
Adding unit tests to improve code converage test
1 parent 6ba7927 commit c277980

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

connexion/operations/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def _get_body_argument(self, body, arguments, has_kwargs, sanitize):
313313
# could have individual arguments to receive all the body's parameters, or
314314
# they may have a **kwargs, arguments to receive anything. So, the question
315315
# arises that if kwargs is given, do we pass to the handler a single body
316-
# argument, or the broken out arguments, or both.
316+
# argument, or the broken out arguments, or both?
317317
#
318318
# #1 If 'x-body-arg' is explicitly given and it exists in [arguments], then the
319319
# body, as a whole, will be passed to the handler with that name. STOP.

tests/api/test_parameters.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ def test_nullable_parameter(simple_app):
352352
resp = app_client.put('/v1.0/nullable-parameters', data="None", headers=headers)
353353
assert json.loads(resp.data.decode('utf-8', 'replace')) == 'it was None'
354354

355+
resp = app_client.put('/v1.0/nullable-parameters-noargs', data="None", headers=headers)
356+
assert json.loads(resp.data.decode('utf-8', 'replace')) == 'hello'
357+
355358

356359
def test_args_kwargs(simple_app):
357360
app_client = simple_app.app.test_client()
@@ -363,6 +366,16 @@ def test_args_kwargs(simple_app):
363366
assert resp.status_code == 200
364367
assert json.loads(resp.data.decode('utf-8', 'replace')) == {'foo': 'a'}
365368

369+
if simple_app._spec_file == 'openapi.yaml':
370+
body = { 'foo': 'a', 'bar': 'b' }
371+
resp = app_client.post(
372+
'/v1.0/body-params-as-kwargs',
373+
data=json.dumps(body),
374+
headers={'Content-Type': 'application/json'})
375+
assert resp.status_code == 200
376+
# having only kwargs and no explicit x-body-name, the handler would have been passed 'body' and the individual params from body
377+
assert json.loads(resp.data.decode('utf-8', 'replace')) == {'body': {'foo': 'a', 'bar': 'b'}, 'foo': 'a', 'bar': 'b'}
378+
366379

367380
def test_param_sanitization(simple_app):
368381
app_client = simple_app.app.test_client()

tests/fakeapi/hello.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ def test_nullable_param_put(contents):
403403
return 'it was None'
404404
return contents
405405

406+
def test_nullable_param_put_noargs(dummy=''):
407+
return 'hello'
408+
406409

407410
def test_custom_json_response():
408411
return {'theResult': DummyClass()}, 200
@@ -460,6 +463,9 @@ def optional_auth(**kwargs):
460463
def test_args_kwargs(*args, **kwargs):
461464
return kwargs
462465

466+
def test_args_kwargs_post(*args, **kwargs):
467+
return kwargs
468+
463469

464470
def test_param_sanitization(query=None, form=None):
465471
result = {}

tests/fixtures/simple/openapi.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,24 @@ paths:
842842
responses:
843843
'200':
844844
description: OK
845+
/nullable-parameters-noargs:
846+
put:
847+
operationId: fakeapi.hello.test_nullable_param_put_noargs
848+
responses:
849+
'200':
850+
description: OK
851+
requestBody:
852+
content:
853+
application/json:
854+
schema:
855+
nullable: true
856+
x-body-name: contents
857+
type: object
858+
properties:
859+
name:
860+
type: string
861+
description: Just a testing parameter.
862+
required: true
845863
/custom-json-response:
846864
get:
847865
operationId: fakeapi.hello.test_custom_json_response
@@ -893,6 +911,25 @@ paths:
893911
application/json:
894912
schema:
895913
type: object
914+
/body-params-as-kwargs:
915+
post:
916+
operationId: fakeapi.hello.test_args_kwargs_post
917+
requestBody:
918+
content:
919+
application/json:
920+
schema:
921+
type: object
922+
properties:
923+
foo:
924+
type: string
925+
additionalProperties: true
926+
responses:
927+
'200':
928+
description: Return kwargs
929+
content:
930+
application/json:
931+
schema:
932+
type: object
896933
/text-request:
897934
post:
898935
operationId: fakeapi.hello.get_data_as_text

tests/fixtures/simple/swagger.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,26 @@ paths:
692692
200:
693693
description: OK
694694

695+
/nullable-parameters-noargs:
696+
put:
697+
operationId: fakeapi.hello.test_nullable_param_put_noargs
698+
produces:
699+
- application/json
700+
parameters:
701+
- name: contents
702+
description: Just a testing parameter.
703+
in: body
704+
x-nullable: true
705+
required: true
706+
schema:
707+
type: object
708+
properties:
709+
name:
710+
type: string
711+
responses:
712+
200:
713+
description: OK
714+
695715
/custom-json-response:
696716
get:
697717
operationId: fakeapi.hello.test_custom_json_response
@@ -746,6 +766,30 @@ paths:
746766
schema:
747767
type: object
748768

769+
/body-params-as-kwargs:
770+
post:
771+
operationId: fakeapi.hello.test_args_kwargs_post
772+
produces:
773+
- application/json
774+
parameters:
775+
- name: $body
776+
description: Just a testing parameter in the body
777+
in: body
778+
required: true
779+
schema:
780+
type: object
781+
properties:
782+
foo:
783+
type: string
784+
bar:
785+
type: string
786+
additionalProperties: true
787+
responses:
788+
200:
789+
description: Return kwargs
790+
schema:
791+
type: object
792+
749793
/text-request:
750794
post:
751795
operationId: fakeapi.hello.get_data_as_text

0 commit comments

Comments
 (0)