def test_entity_creation():
    props = {
        "id": "syn123456",
        "concreteType": "org.sagebionetworks.repo.model.Folder",
        "parentId": "syn445566",
        "name": "Testing123"
    }
    annos = {'testing': 123}
    folder = Entity.create(props, annos)

    assert folder.concreteType == 'org.sagebionetworks.repo.model.Folder'
    assert folder.__class__ == Folder
    assert folder.name == 'Testing123'
    assert folder.testing == 123

    # In case of unknown concreteType, fall back on generic Entity object
    props = {
        "id": "syn123456",
        "concreteType": "org.sagebionetworks.repo.model.DoesntExist",
        "parentId": "syn445566",
        "name": "Whatsits"
    }
    whatsits = Entity.create(props)

    assert whatsits.concreteType == 'org.sagebionetworks.repo.model.DoesntExist'
    assert whatsits.__class__ == Entity
Beispiel #2
0
def test_entityViewSchema__add_scope():
    entity_view = EntityViewSchema(parent="idk")
    entity_view.add_scope(Entity(parent="also idk", id=123))
    entity_view.add_scope(456)
    entity_view.add_scope("789")
    assert_equals([str(x) for x in ["123", "456", "789"]],
                  entity_view.scopeIds)
def test_EntityViewSchema__ignore_column_names_set_info_preserved():
    """
    tests that ignoredAnnotationColumnNames will be preserved after creating a new EntityViewSchema from properties,
    local_state, and annotations
    """
    ignored_names = {'a', 'b', 'c'}
    entity_view = EntityViewSchema("someName", parent="syn123", ignoredAnnotationColumnNames={'a', 'b', 'c'})
    properties, annotations, local_state = split_entity_namespaces(entity_view)
    entity_view_copy = Entity.create(properties, annotations, local_state)
    assert_equals(ignored_names, entity_view.ignoredAnnotationColumnNames)
    assert_equals(ignored_names, entity_view_copy.ignoredAnnotationColumnNames)
def test_EntityViewSchema__ignore_column_names_set_info_preserved():
    """
    tests that ignoredAnnotationColumnNames will be preserved after creating a new EntityViewSchema from properties,
    local_state, and annotations
    """
    ignored_names = {'a', 'b', 'c'}
    entity_view = EntityViewSchema("someName", parent="syn123", ignoredAnnotationColumnNames={'a', 'b', 'c'})
    properties, annotations, local_state = split_entity_namespaces(entity_view)
    entity_view_copy = Entity.create(properties, annotations, local_state)
    assert ignored_names == entity_view.ignoredAnnotationColumnNames
    assert ignored_names == entity_view_copy.ignoredAnnotationColumnNames
def test_split_entity_namespaces():
    """Test split_entity_namespaces"""

    e = {
        'concreteType': 'org.sagebionetworks.repo.model.Folder',
        'name': 'Henry',
        'color': 'blue',
        'foo': 1234,
        'parentId': 'syn1234'
    }
    (properties, annotations, local_state) = split_entity_namespaces(e)

    assert set(properties.keys()) == {'concreteType', 'name', 'parentId'}
    assert properties['name'] == 'Henry'
    assert set(annotations.keys()) == {'color', 'foo'}
    assert annotations['foo'] == 1234
    assert len(local_state) == 0

    e = {
        'concreteType': 'org.sagebionetworks.repo.model.FileEntity',
        'name': 'Henry',
        'color': 'blue',
        'foo': 1234,
        'parentId': 'syn1234',
        'dataFileHandleId': 54321,
        'cacheDir': '/foo/bar/bat',
        'files': ['foo.xyz'],
        'path': '/foo/bar/bat/foo.xyz'
    }
    (properties, annotations, local_state) = split_entity_namespaces(e)

    assert set(properties.keys()) == {
        'concreteType', 'name', 'parentId', 'dataFileHandleId'
    }
    assert properties['name'] == 'Henry'
    assert properties['dataFileHandleId'] == 54321
    assert set(annotations.keys()) == {'color', 'foo'}
    assert annotations['foo'] == 1234
    assert set(local_state.keys()) == {'cacheDir', 'files', 'path'}
    assert local_state['cacheDir'] == '/foo/bar/bat'

    f = Entity.create(properties, annotations, local_state)
    assert f.properties.dataFileHandleId == 54321
    assert f.properties.name == 'Henry'
    assert f.annotations.foo == 1234
    assert f.__dict__['cacheDir'] == '/foo/bar/bat'
    assert f.__dict__['path'] == '/foo/bar/bat/foo.xyz'
def test_Entity():
    # Test the basics of creating and accessing properties on an entity
    for i in range(2):
        e = Entity(name='Test object',
                   description='I hope this works',
                   annotations=dict(foo=123,
                                    nerds=['chris', 'jen', 'janey'],
                                    annotations='How confusing!'),
                   properties=dict(
                       annotations='/repo/v1/entity/syn1234/annotations',
                       md5='cdef636522577fc8fb2de4d95875b27c',
                       parentId='syn1234'),
                   concreteType='org.sagebionetworks.repo.model.Data')

        # Should be able to create an Entity from an Entity
        if i == 1:
            e = Entity.create(e)

        assert e.parentId == 'syn1234'
        assert e['parentId'] == 'syn1234'
        assert e.properties['parentId'] == 'syn1234'
        assert e.properties.parentId == 'syn1234'

        assert e.foo == 123
        assert e['foo'] == 123
        assert e.annotations['foo'] == 123
        assert e.annotations.foo == 123

        assert hasattr(e, 'parentId')
        assert hasattr(e, 'foo')
        assert not hasattr(e, 'qwerqwer')

        # Annotations is a bit funny, because there is a property call
        # 'annotations', which will be masked by a member of the object
        # called 'annotations'. Because annotations are open-ended, we
        # might even have an annotations called 'annotations', which gets
        # really confusing.
        assert isinstance(e.annotations, collections.abc.Mapping)
        assert isinstance(e['annotations'], collections.abc.Mapping)
        assert e.properties[
            'annotations'] == '/repo/v1/entity/syn1234/annotations'
        assert e.properties.annotations == '/repo/v1/entity/syn1234/annotations'
        assert e.annotations.annotations == 'How confusing!'
        assert e.annotations['annotations'] == 'How confusing!'
        assert e.nerds == ['chris', 'jen', 'janey']
        assert all([
            k in e for k in [
                'name', 'description', 'foo', 'nerds', 'annotations', 'md5',
                'parentId'
            ]
        ])

        # Test modifying properties
        e.description = 'Working, so far'
        assert e['description'] == 'Working, so far'
        e['description'] = 'Wiz-bang flapdoodle'
        assert e.description == 'Wiz-bang flapdoodle'

        # Test modifying annotations
        e.foo = 999
        assert e.annotations['foo'] == 999
        e['foo'] = 12345
        assert e.annotations.foo == 12345

        # Test creating a new annotation
        e['bar'] = 888
        assert e.annotations['bar'] == 888
        e['bat'] = 7788
        assert e.annotations['bat'] == 7788

        # Test replacing annotations object
        e.annotations = {
            'splat': 'a totally new set of annotations',
            'foo': 456
        }
        assert e.foo == 456
        assert e['foo'] == 456
        assert isinstance(e.annotations, collections.abc.Mapping)
        assert isinstance(e['annotations'], collections.abc.Mapping)
        assert e.annotations.foo == 456
        assert e.properties[
            'annotations'] == '/repo/v1/entity/syn1234/annotations'
        assert e.properties.annotations == '/repo/v1/entity/syn1234/annotations'

        # test unicode properties
        e.train = '時刻表には記載されない 月への列車が来ると聞いて'
        e.band = "Motörhead"
        e.lunch = "すし"