From 7aa2997b99d2ca2aa058620901e722842937eeb1 Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 26 Apr 2020 20:51:10 -0700 Subject: [PATCH] add get upa from object method --- lib/SampleService/core/api_translation.py | 24 +++++++++++++++---- test/core/api_translation_test.py | 28 ++++++++++++++++++++++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/SampleService/core/api_translation.py b/lib/SampleService/core/api_translation.py index fd5f74e9..d1f20ae9 100644 --- a/lib/SampleService/core/api_translation.py +++ b/lib/SampleService/core/api_translation.py @@ -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. ''' @@ -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) @@ -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) diff --git a/test/core/api_translation_test.py b/test/core/api_translation_test.py index 8ae2643e..1c3a5822 100644 --- a/test/core/api_translation_test.py +++ b/test/core/api_translation_test.py @@ -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 ( @@ -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'}, @@ -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