Skip to content

Commit

Permalink
[#543] Add possibility to use different responses in EDI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillmakhonin committed Oct 22, 2018
1 parent 9d04510 commit 98d8bf4
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions legion/tests/legion_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,21 @@ def patch_environ(values, flush_existence=False):


@contextlib.contextmanager
def persist_swagger_function_response_to_file(function, response_code):
def persist_swagger_function_response_to_file(function, response_codes):
"""
Context managers
Persist result of invocation swagger client query data to disk
:param function: name of function
:type function: str
:param response_code: name of response code
:type response_code: str
:param response_codes: name of response code (no), e.g. two_models. Or list of response codes
:type response_codes: str or list[str]
"""
num = 0
if isinstance(response_codes, str):
response_codes = [response_codes]
num = -1

client = build_swagger_function_client(function)
origin = client.__class__.deserialize

Expand All @@ -115,6 +120,13 @@ def response_catcher(self, response, type_name):
call_stack_functions = [f.function for f in inspect.stack()]

if function_name in call_stack_functions:
nonlocal num
if num == -1:
response_code = response_codes[0]
else:
response_code = response_codes[num]
num += 1

path = '{}/{}.{}.{}.json'.format(TEST_RESPONSES_LOCATION, function,
type_name, response_code)
with open(path, 'w') as stream:
Expand Down Expand Up @@ -144,37 +156,53 @@ def build_swagger_function_client(function):
return module_api_client.api_client


def mock_swagger_function_response_from_file(function, response_code, *args, **kwargs):
def mock_swagger_function_response_from_file(function, response_codes):
"""
Mock swagger client function with response from file
:param function: name of function to mock, e.g. kubernetes.client.CoreV1Api.list_namespaced_service
:type function: str
:param response_code: name of response code (no), e.g. two_models
:type response_code: str
:param response_codes: name of response code (no), e.g. two_models. Or list of response codes
:type response_codes: str or list[str]
:return: Any -- response
"""
valid_files = glob.glob('{}/{}.*.{}.json'.format(TEST_RESPONSES_LOCATION, function, response_code))

if not valid_files:
raise Exception('Cannot find response example file for function {!r} with code {!r}'.format(
function, response_code
))

if len(valid_files) > 1:
raise Exception('Finded more then one file for function {!r} with code {!r}'.format(
function, response_code
))
num = 0
if isinstance(response_codes, str):
response_codes = [response_codes]
num = -1

response_files = []
for response_code in response_codes:
valid_files = glob.glob('{}/{}.*.{}.json'.format(TEST_RESPONSES_LOCATION, function, response_code))

if not valid_files:
raise Exception('Cannot find response example file for function {!r} with code {!r}'.format(
function, response_code
))

if len(valid_files) > 1:
raise Exception('Finded more then one file for function {!r} with code {!r}'.format(
function, response_code
))

path, filename = valid_files[0], os.path.basename(valid_files[0])
splits = filename.rsplit('.')
return_type = splits[-3]
response_files.append((path, return_type))

path, filename = valid_files[0], os.path.basename(valid_files[0])
splits = filename.rsplit('.')
return_type = splits[-3]
client = build_swagger_function_client(function)

with open(path) as response_file:
data = ResponseObjectType(data=response_file.read())

def response_catcher(*args, **kwargs):
nonlocal num
if num == -1:
path, return_type = response_files[0]
else:
path, return_type = response_files[num]
num += 1

with open(path) as response_file:
data = ResponseObjectType(data=response_file.read())

return client.deserialize(data, return_type)

return patch(function, side_effect=response_catcher)
Expand Down

0 comments on commit 98d8bf4

Please sign in to comment.