def test_to_synapse_annotations__called_convert_to_annotations_list(mock_convert_to_annotations_list): """Test the helper function _convert_to_annotations_list called properly""" a = Annotations('syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', {'foo': 'bar', 'zoo': ['zing', 'zaboo'], 'species': 'Platypus'}) to_synapse_annotations(a) mock_convert_to_annotations_list.assert_called_once_with({'foo': 'bar', 'zoo': ['zing', 'zaboo'], 'species': 'Platypus'})
def test_annotations(): """Test string annotations""" a = Annotations('syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', { 'foo': 'bar', 'zoo': ['zing', 'zaboo'], 'species': 'Platypus' }) sa = to_synapse_annotations(a) expected = { 'id': 'syn123', 'etag': '7bdb83e9-a50a-46e4-987a-4962559f090f', 'annotations': { 'foo': { 'value': ['bar'], 'type': 'STRING' }, 'zoo': { 'value': ['zing', 'zaboo'], 'type': 'STRING' }, 'species': { 'value': ['Platypus'], 'type': 'STRING' } } } assert expected == sa
def test_annotations_unicode(): a = Annotations( 'syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', { 'files': [u'文件.txt'], 'cacheDir': u'/Users/chris/.synapseCache/python/syn1809087', u'foo': 1266 }) sa = to_synapse_annotations(a) expected = { 'id': 'syn123', 'etag': '7bdb83e9-a50a-46e4-987a-4962559f090f', 'annotations': { 'cacheDir': { 'type': 'STRING', 'value': ['/Users/chris/.synapseCache/python/syn1809087'] }, 'files': { 'type': 'STRING', 'value': ['文件.txt'] }, 'foo': { 'type': 'LONG', 'value': ['1266'] } } } assert expected == sa
def test_to_synapse_annotations__empty(): python_client_annos = Annotations('syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', {}) assert ( { 'id': 'syn123', 'etag': '7bdb83e9-a50a-46e4-987a-4962559f090f', 'annotations': {} } == to_synapse_annotations(python_client_annos) )
def test_idempotent_annotations(): """test that to_synapse_annotations won't mess up a dictionary that's already in the synapse format""" a = Annotations('syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', {'species': 'Moose', 'n': 42, 'birthday': Datetime(1969, 4, 28), 'fact': True}) sa = to_synapse_annotations(a) a2 = {} a2.update(sa) sa2 = to_synapse_annotations(a2) assert sa == sa2
def test_mixed_annotations(): """test that to_synapse_annotations will coerce a list of mixed types to strings""" a = Annotations('syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', {'foo': [1, 'a', Datetime(1969, 4, 28, 11, 47)]}) sa = to_synapse_annotations(a) a2 = from_synapse_annotations(sa) assert a2['foo'][0] == '1' assert a2['foo'][1] == 'a' assert a2['foo'][2].find('1969') > -1 assert 'syn123' == a2.id assert '7bdb83e9-a50a-46e4-987a-4962559f090f' == a2.etag
def test_more_annotations(): """Test long, float and data annotations""" a = Annotations( 'syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', { 'foo': 1234, 'zoo': [123.1, 456.2, 789.3], 'species': 'Platypus', 'birthdays': [ Datetime(1969, 4, 28), Datetime(1973, 12, 8), Datetime(2008, 1, 3) ], 'test_boolean': True, 'test_mo_booleans': [False, True, True, False] }) sa = to_synapse_annotations(a) expected = { 'id': 'syn123', 'etag': '7bdb83e9-a50a-46e4-987a-4962559f090f', 'annotations': { 'foo': { 'value': ['1234'], 'type': 'LONG' }, 'zoo': { 'value': ['123.1', '456.2', '789.3'], 'type': 'DOUBLE' }, 'species': { 'value': ['Platypus'], 'type': 'STRING' }, 'birthdays': { 'value': ['-21427200000', '124156800000', '1199318400000'], 'type': 'TIMESTAMP_MS' }, 'test_boolean': { 'value': ['true'], 'type': 'STRING' }, 'test_mo_booleans': { 'value': ['false', 'true', 'true', 'false'], 'type': 'STRING' } } } assert expected == sa
def test_submission_status_annotations_round_trip(): april_28_1969 = Datetime(1969, 4, 28) a = Annotations( 'syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', { 'screen_name': 'Bullwinkle', 'species': 'Moose', 'lucky': 13, 'pi': pi, 'birthday': april_28_1969 }) sa = to_submission_status_annotations(a) assert {'screen_name', 'species'} == set([kvp['key'] for kvp in sa['stringAnnos']]) assert {'Bullwinkle', 'Moose'} == set([kvp['value'] for kvp in sa['stringAnnos']]) # test idempotence assert sa == to_submission_status_annotations(sa) assert {'lucky', 'birthday'} == set([kvp['key'] for kvp in sa['longAnnos']]) for kvp in sa['longAnnos']: key = kvp['key'] value = kvp['value'] if key == 'lucky': assert value == 13 if key == 'birthday': assert utils.from_unix_epoch_time(value) == april_28_1969 assert {'pi'} == set([kvp['key'] for kvp in sa['doubleAnnos']]) assert {pi} == set([kvp['value'] for kvp in sa['doubleAnnos']]) set_privacy(sa, key='screen_name', is_private=False) pytest.raises(KeyError, set_privacy, sa, key='this_key_does_not_exist', is_private=False) for kvp in sa['stringAnnos']: if kvp['key'] == 'screen_name': assert not kvp['isPrivate'] a2 = from_submission_status_annotations(sa) # TODO: is there a way to convert dates back from longs automatically? a2['birthday'] = utils.from_unix_epoch_time(a2['birthday']) assert a == a2 # test idempotence assert a == from_submission_status_annotations(a)
def test__None_id_and_etag(self): # in constuctor pytest.raises(ValueError, Annotations, 'syn123', None) pytest.raises(ValueError, Annotations, None, '0f2977b9-0261-4811-a89e-c13e37ce4604') # after constuction annot = Annotations('syn123', '0f2977b9-0261-4811-a89e-c13e37ce4604', {'asdf': 'qwerty'}) def change_id_to_None(): annot.id = None pytest.raises(ValueError, change_id_to_None) def change_etag_to_None(): annot.etag = None pytest.raises(ValueError, change_etag_to_None)
def _convert_to_annotation_cls( id: str, etag: str, values: typing.Union[Annotations, dict]) -> Annotations: """Convert synapse style annotation or dict to synapseclient.Annotation :param id: The id of the entity / submission :param etag: The etag of the entity / submission :param values: A synapseclient.Annotations or dict :returns: A synapseclient.Annotations """ if isinstance(values, Annotations): return values if is_synapse_annotations(values): values = from_synapse_annotations(values) else: values = Annotations(id=id, etag=etag, values=values) return values
def test_round_trip_annotations(): """Test that annotations can make the round trip from a simple dictionary to the synapse format and back""" a = Annotations( 'syn123', '7bdb83e9-a50a-46e4-987a-4962559f090f', { 'foo': [1234], 'zoo': [123.1, 456.2, 789.3], 'species': ['Moose'], 'birthdays': [ Datetime(1969, 4, 28), Datetime(1973, 12, 8), Datetime(2008, 1, 3), Datetime(2013, 3, 15) ] }) sa = to_synapse_annotations(a) a2 = from_synapse_annotations(sa) assert a == a2 assert a.id == a2.id assert a.etag == a2.etag
def test_annotations(self): annot = Annotations('syn123', '0f2977b9-0261-4811-a89e-c13e37ce4604', {'asdf': 'qwerty'}) assert 'syn123' == annot.id assert '0f2977b9-0261-4811-a89e-c13e37ce4604' == annot.etag assert {'asdf': 'qwerty'} == annot