Skip to content

Commit

Permalink
#292 add some first test for MockResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Sep 29, 2016
1 parent 1f8b4d2 commit f206d68
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions connexion/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def mock_operation(self, operation, *args, **kwargs):
# simply use the first/lowest status code, this is probably 200 or 201
status_code = sorted(response_definitions.keys())[0]
response_definition = response_definitions.get(status_code, {})
try:
status_code = int(status_code)
except ValueError:
status_code = 200
response_definition = operation.resolve_reference(response_definition)
examples = response_definition.get('examples')
if examples:
Expand Down
82 changes: 82 additions & 0 deletions tests/test_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

from connexion.operation import Operation
from connexion.mock import MockResolver


def test_mock_resolver():
resolver = MockResolver(mock_all=True)

responses = {
'default': {
'examples': {
'application/json': {
'foo': 'bar'
}
}
}
}

operation = Operation(method='GET',
path='endpoint',
path_parameters=[],
operation={
'responses': responses
},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions={},
resolver=resolver)
assert operation.operation_id == 'mock-1'

response, status_code = resolver.mock_operation(operation)
assert status_code == 200
assert response == {'foo': 'bar'}


def test_mock_resolver_no_examples():
resolver = MockResolver(mock_all=True)

responses = {
'418': {}
}

operation = Operation(method='GET',
path='endpoint',
path_parameters=[],
operation={
'responses': responses
},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions={},
resolver=resolver)
assert operation.operation_id == 'mock-1'

response, status_code = resolver.mock_operation(operation)
assert status_code == 418
assert response == 'No example response was defined.'


def test_mock_resolver_notimplemented():
resolver = MockResolver(mock_all=False)

operation = Operation(method='GET',
path='endpoint',
path_parameters=[],
operation={
'operationId': 'fakeapi.hello.get'
},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions={},
resolver=resolver)
assert operation.operation_id == 'fakeapi.hello.get'

0 comments on commit f206d68

Please sign in to comment.