update tests

This commit is contained in:
2020-08-26 10:56:34 +02:00
parent c309ebbd56
commit ca101cf094
5 changed files with 151 additions and 47 deletions

View File

@ -59,7 +59,7 @@ class CallDispatcher:
mandatories = await config.value.mandatory()
if mandatories:
mand = [mand.split('.')[-1] for mand in mandatories]
raise ValueError(_(f'missing parameters in response: {mand} in message "{risotto_context.message}"'))
raise ValueError(_(f'missing parameters in response of the message "{risotto_context.version}.{risotto_context.message}": {mand} in message'))
try:
await config.value.dict()
except Exception as err:
@ -72,7 +72,9 @@ class CallDispatcher:
message: str,
old_risotto_context: Context,
check_role: bool=False,
**kwargs):
internal: bool=True,
**kwargs,
):
""" execute the function associate with specified uri
arguments are validate before
"""
@ -80,6 +82,10 @@ class CallDispatcher:
version,
message,
'rpc')
if version not in self.messages:
raise CallError(_(f'cannot find version of message "{version}"'))
if message not in self.messages[version]:
raise CallError(_(f'cannot find message "{version}.{message}"'))
function_objs = [self.messages[version][message]]
# do not start a new database connection
if hasattr(old_risotto_context, 'connection'):
@ -89,7 +95,9 @@ class CallDispatcher:
risotto_context,
check_role,
kwargs,
function_objs)
function_objs,
internal,
)
else:
try:
async with self.pool.acquire() as connection:
@ -106,7 +114,9 @@ class CallDispatcher:
risotto_context,
check_role,
kwargs,
function_objs)
function_objs,
internal,
)
except CallError as err:
raise err
except Exception as err:
@ -132,7 +142,9 @@ class PublishDispatcher:
message: str,
old_risotto_context: Context,
check_role: bool=False,
**kwargs) -> None:
internal: bool=True,
**kwargs,
) -> None:
risotto_context = self.build_new_context(old_risotto_context,
version,
message,
@ -149,7 +161,9 @@ class PublishDispatcher:
risotto_context,
check_role,
kwargs,
function_objs)
function_objs,
internal,
)
try:
async with self.pool.acquire() as connection:
await connection.set_type_codec(
@ -165,7 +179,9 @@ class PublishDispatcher:
risotto_context,
check_role,
kwargs,
function_objs)
function_objs,
internal,
)
except CallError as err:
raise err
except Exception as err:
@ -222,7 +238,9 @@ class Dispatcher(register.RegisterDispatcher,
risotto_context: Context,
uri: str,
kwargs: Dict,
check_role: bool):
check_role: bool,
internal: bool,
):
""" create a new Config et set values to it
"""
# create a new config
@ -232,13 +250,17 @@ class Dispatcher(register.RegisterDispatcher,
await config.option('message').value.set(risotto_context.message)
# store values
subconfig = config.option(risotto_context.message)
extra_parameters = {}
for key, value in kwargs.items():
try:
await subconfig.option(key).value.set(value)
except AttributeError:
if get_config()['global']['debug']:
print_exc()
raise ValueError(_(f'unknown parameter in "{uri}": "{key}"'))
if not internal or not key.startswith('_'):
try:
await subconfig.option(key).value.set(value)
except AttributeError:
if get_config()['global']['debug']:
print_exc()
raise ValueError(_(f'unknown parameter in "{uri}": "{key}"'))
else:
extra_parameters[key] = value
# check mandatories options
if check_role and get_config().get('global').get('check_role'):
await self.check_role(subconfig,
@ -250,7 +272,10 @@ class Dispatcher(register.RegisterDispatcher,
mand = [mand.split('.')[-1] for mand in mandatories]
raise ValueError(_(f'missing parameters in "{uri}": {mand}'))
# return complete an validated kwargs
return await subconfig.value.dict()
parameters = await subconfig.value.dict()
if extra_parameters:
parameters.update(extra_parameters)
return parameters
def get_service(self,
name: str):
@ -309,13 +334,17 @@ class Dispatcher(register.RegisterDispatcher,
risotto_context: Context,
check_role: bool,
kwargs: Dict,
function_objs: List) -> Optional[Dict]:
function_objs: List,
internal: bool,
) -> Optional[Dict]:
await self.check_message_type(risotto_context,
kwargs)
config_arguments = await self.load_kwargs_to_config(risotto_context,
f'{version}.{message}',
kwargs,
check_role)
check_role,
internal,
)
# config is ok, so send the message
for function_obj in function_objs:
function = function_obj['function']

View File

@ -82,7 +82,9 @@ async def handle(request):
message,
risotto_context,
check_role=True,
**kwargs)
internal=False,
**kwargs,
)
except NotAllowedError as err:
raise HTTPNotFound(reason=str(err))
except CallError as err:

View File

@ -136,6 +136,9 @@ class RegisterDispatcher:
function_args = self.get_function_args(function)
# compare message arguments with function parameter
# it must not have more or less arguments
for arg in function_args - message_args:
if arg.startswith('_'):
message_args.add(arg)
if message_args != function_args:
# raise if arguments are not equal
msg = []