def test_submission_status_annotations_round_trip(): april_28_1969 = Datetime(1969, 4, 28) a = dict(screen_name='Bullwinkle', species='Moose', lucky=13, pi=pi, birthday=april_28_1969) sa = to_submission_status_annotations(a) print sa assert set(['screen_name', 'species']) == set([kvp['key'] for kvp in sa['stringAnnos']]) assert set(['Bullwinkle', 'Moose']) == set([kvp['value'] for kvp in sa['stringAnnos']]) ## test idempotence assert sa == to_submission_status_annotations(sa) assert set(['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 set(['pi']) == set([kvp['key'] for kvp in sa['doubleAnnos']]) assert set([pi]) == set([kvp['value'] for kvp in sa['doubleAnnos']]) set_privacy(sa, key='screen_name', is_private=False) assert_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 kvp['isPrivate'] == False 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_more_annotations(): """Test long, float and data annotations""" a = dict(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) print sa assert sa['longAnnotations']['foo'] == [1234] assert sa['doubleAnnotations']['zoo'] == [123.1, 456.2, 789.3] assert sa['stringAnnotations']['species'] == ['Platypus'] assert sa['stringAnnotations']['test_boolean'] == ['true'] assert sa['stringAnnotations']['test_mo_booleans'] == [ 'false', 'true', 'true', 'false' ] ## this part of the test is kinda fragile. It it breaks again, it should be removed bdays = [ utils.from_unix_epoch_time(t) for t in sa['dateAnnotations']['birthdays'] ] assert all([ t in bdays for t in [Datetime(1969, 4, 28), Datetime(1973, 12, 8), Datetime(2008, 1, 3)] ])
def cast_values(values, headers): """ Convert a row of table query results from strings to the correct column type. See: http://rest.synapse.org/org/sagebionetworks/repo/model/table/ColumnType.html """ if len(values) != len(headers): raise ValueError('Each field in the row must have a matching column header. %d fields, %d headers' % (len(values), len(headers))) result = [] for header, field in izip(headers, values): columnType = header.get('columnType', 'STRING') ## convert field to column type if field is None or field=='': result.append(None) elif columnType in ['STRING', 'ENTITYID', 'FILEHANDLEID']: result.append(field) elif columnType=='DOUBLE': result.append(float(field)) elif columnType=='INTEGER': result.append(int(field)) elif columnType=='BOOLEAN': result.append(to_boolean(field)) elif columnType=='DATE': result.append(utils.from_unix_epoch_time(field)) else: raise ValueError("Unknown column type: %s" % columnType) return result
def cast_values(values, headers): """ Convert a row of table query results from strings to the correct column type. See: http://rest.synapse.org/org/sagebionetworks/repo/model/table/ColumnType.html """ if len(values) != len(headers): raise ValueError( 'Each field in the row must have a matching column header. %d fields, %d headers' % (len(values), len(headers))) result = [] for header, field in izip(headers, values): columnType = header.get('columnType', 'STRING') ## convert field to column type if field is None or field == '': result.append(None) elif columnType in ['STRING', 'ENTITYID', 'FILEHANDLEID']: result.append(field) elif columnType == 'DOUBLE': result.append(float(field)) elif columnType == 'INTEGER': result.append(int(field)) elif columnType == 'BOOLEAN': result.append(to_boolean(field)) elif columnType == 'DATE': result.append(utils.from_unix_epoch_time(field)) else: raise ValueError("Unknown column type: %s" % columnType) return result
def test_more_annotations(): """Test long, float and data annotations""" a = dict(foo=1234, zoo=[123.1, 456.2, 789.3], species='Platypus', birthdays=[Datetime(1969,4,28), Datetime(1973,12,8), Datetime(2008,1,3)]) sa = to_synapse_annotations(a) # print sa assert sa['longAnnotations']['foo'] == [1234] assert sa['doubleAnnotations']['zoo'] == [123.1, 456.2, 789.3] assert sa['stringAnnotations']['species'] == ['Platypus'] ## this part of the test is kinda fragile. It it breaks again, it should be removed bdays = [utils.from_unix_epoch_time(t) for t in sa['dateAnnotations']['birthdays']] assert all([t in bdays for t in [Datetime(1969,4,28), Datetime(1973,12,8), Datetime(2008,1,3)]])
def test_submission_status_annotations_round_trip(): from math import pi april_28_1969 = Datetime(1969,4,28) a = dict(screen_name='Bullwinkle', species='Moose', lucky=13, pi=pi, birthday=april_28_1969) sa = to_submission_status_annotations(a) print sa assert set(['screen_name','species']) == set([kvp['key'] for kvp in sa['stringAnnos']]) assert set(['Bullwinkle','Moose']) == set([kvp['value'] for kvp in sa['stringAnnos']]) ## test idempotence assert sa == to_submission_status_annotations(sa) assert set(['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 set(['pi']) == set([kvp['key'] for kvp in sa['doubleAnnos']]) set([pi]) == set([kvp['value'] for kvp in sa['doubleAnnos']]) set_privacy(sa, key='screen_name', is_private=False) assert_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 kvp['isPrivate'] == False 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)