Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lib/SampleService/core/api_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from SampleService.core.errors import MissingParameterError as _MissingParameterError
from SampleService.core.errors import UnauthorizedError as _UnauthorizedError
from SampleService.core.user import UserID as _UserID
from SampleService.core.workspace import DataUnitID, UPA as _UPA
from SampleService.core.workspace import DataUnitID, UPA

ID = 'id'
''' The ID of a sample. '''
Expand Down Expand Up @@ -374,6 +374,7 @@ def create_data_link_params(params: Dict[str, Any]) -> Tuple[DataUnitID, SampleN
1) The data unit ID that is the target of the link,
2) The sample node that is the target of the link,
3) A boolean that indicates whether the link should be updated if it already exists.
:raises MissingParameterError: if any of the required arguments are missing.
:raises IllegalParameterError: if any of the arguments are illegal.
'''
_check_params(params)
Expand All @@ -384,15 +385,30 @@ def create_data_link_params(params: Dict[str, Any]) -> Tuple[DataUnitID, SampleN
_cast(str, _check_string(params, 'node', True))
)
duid = DataUnitID(
_UPA(_cast(str, _check_string(params, 'upa', True))),
get_upa_from_object(params),
_check_string(params, 'dataid'))
return (duid, sna, bool(params.get('update')))


def get_upa_from_object(params: Dict[str, Any]) -> UPA:
'''
Get an UPA from a parameter object. Expects the UPA in the key 'upa'.
:params params: the parameters.
:returns: the UPA.
:raises MissingParameterError: if the UPA is missing.
:raises IllegalParameterError: if the UPA is illegal.
'''
_check_params(params)
return UPA(_cast(str, _check_string(params, 'upa', True)))


def _check_string(params: Dict[str, Any], key: str, required=False) -> Optional[str]:
v = params.get(key)
if v is None and not required:
return None
if v is None:
if required:
raise _MissingParameterError(key)
else:
return None
if type(v) != str:
raise _IllegalParameterError(f'{key} key is not a string as required')
return _cast(str, v)
Expand Down
28 changes: 27 additions & 1 deletion test/core/api_translation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
get_static_key_metadata_params,
create_data_link_params,
get_datetime_from_epochmilliseconds_in_object,
links_to_dicts
links_to_dicts,
get_upa_from_object
)
from SampleService.core.data_link import DataLink
from SampleService.core.sample import (
Expand Down Expand Up @@ -644,12 +645,18 @@ def test_create_data_link_params_fail_bad_args():
IllegalParameterError('Illegal version argument: -1'))
_create_data_link_params_fail(
{'id': id_, 'version': 1},
MissingParameterError('node'))
_create_data_link_params_fail(
{'id': id_, 'version': 1, 'node': {'a': 'b'}},
IllegalParameterError('node key is not a string as required'))
_create_data_link_params_fail(
{'id': id_, 'version': 1, 'node': 'foo\tbar'},
IllegalParameterError('node contains control characters'))
_create_data_link_params_fail(
{'id': id_, 'version': 1, 'node': 'm'},
MissingParameterError('upa'))
_create_data_link_params_fail(
{'id': id_, 'version': 1, 'node': 'm', 'upa': 3.4},
IllegalParameterError('upa key is not a string as required'))
_create_data_link_params_fail(
{'id': id_, 'version': 1, 'node': 'm', 'upa': '1/0/1'},
Expand All @@ -668,6 +675,25 @@ def _create_data_link_params_fail(params, expected):
assert_exception_correct(got.value, expected)


def test_get_upa_from_object():
assert get_upa_from_object({'upa': '1/1/1'}) == UPA('1/1/1')
assert get_upa_from_object({'upa': '8/3/2'}) == UPA('8/3/2')


def test_get_upa_from_object_fail_bad_args():
_get_upa_from_object_fail(None, ValueError('params cannot be None'))
_get_upa_from_object_fail({}, MissingParameterError('upa'))
_get_upa_from_object_fail({'upa': '1/0/1'}, IllegalParameterError('1/0/1 is not a valid UPA'))
_get_upa_from_object_fail({'upa': 82}, IllegalParameterError(
'upa key is not a string as required'))


def _get_upa_from_object_fail(params, expected):
with raises(Exception) as got:
get_upa_from_object(params)
assert_exception_correct(got.value, expected)


def test_get_datetime_from_epochmilliseconds_in_object():
gt = get_datetime_from_epochmilliseconds_in_object
assert gt({}, 'foo') is None
Expand Down