예제 #1
0
파일: test_storage.py 프로젝트: angru/corm
def test_make_relation():
    class User(Entity):
        id: int

    class Address(Entity):
        id: int

    storage = Storage()
    user = User(data={'id': 1}, storage=storage)
    address1 = Address(data={'id': 1}, storage=storage)
    address2 = Address(data={'id': 2}, storage=storage)

    storage.make_relation(
        from_=user,
        to_=address1,
        relation_type=RelationType.RELATED,
    )
    storage.make_relation(
        from_=user,
        to_=address2,
        relation_type=RelationType.CHILD,
    )

    assert storage.get_one_related_entity(
        user,
        Address,
        RelationType.RELATED,
    ) == address1
    assert storage.get_related_entities(
        user,
        Address,
        RelationType.RELATED,
    ) == [address1]
예제 #2
0
def test_relationship():
    class SomeEntity(Entity):
        name: str

    class EntityHolder(Entity):
        name: str
        entity: SomeEntity = Relationship(
            entity_type=SomeEntity,
            relation_type=RelationType.CHILD,
        )

    storage = Storage()
    holder = EntityHolder(data={'name': 'holder'}, storage=storage)
    entity1 = SomeEntity(data={'name': 'entity1'}, storage=storage)

    assert holder.entity is None

    storage.make_relation(
        from_=holder,
        to_=entity1,
        relation_type=RelationType.CHILD,
    )

    assert holder.entity == entity1

    class ManyEntityHolder(Entity):
        name: str
        entities: t.List[SomeEntity] = Relationship(
            entity_type=SomeEntity,
            many=True,
            relation_type=RelationType.CHILD,
        )

    holder = ManyEntityHolder(data={'name': 'many holder'}, storage=storage)
    entity2 = SomeEntity(data={'name': 'entity2'}, storage=storage)

    storage.make_relation(
        from_=holder,
        to_=entity1,
        relation_type=RelationType.CHILD,
    )
    storage.make_relation(
        from_=holder,
        to_=entity2,
        relation_type=RelationType.CHILD,
    )

    assert holder.entities == [entity1, entity2]

    with pytest.raises(ValueError):
        storage.make_relation(
            from_=holder,
            to_=entity2,
            relation_type=RelationType.CHILD,
        )
예제 #3
0
파일: test_storage.py 프로젝트: angru/corm
def test_remove_relations():
    class User(Entity):
        id: int

    class Address(Entity):
        id: int

    storage = Storage()
    user = User(data={'id': 1}, storage=storage)
    address1 = Address(data={'id': 1}, storage=storage)
    address2 = Address(data={'id': 2}, storage=storage)
    address3 = Address(data={'id': 3}, storage=storage)

    storage.make_relation(
        from_=user,
        to_=address1,
        relation_type=RelationType.RELATED,
    )
    storage.make_relation(
        from_=user,
        to_=address2,
        relation_type=RelationType.RELATED,
    )
    storage.make_relation(
        from_=user,
        to_=address3,
        relation_type=RelationType.PARENT,
    )

    assert storage.get_related_entities(
        user,
        Address,
        RelationType.RELATED,
    ) == [
        address1,
        address2,
    ]

    storage.remove_relations(user, Address, RelationType.RELATED)

    assert storage.get_related_entities(
        user,
        Address,
        RelationType.RELATED,
    ) == []
    assert storage.get_related_entities(
        user,
        Address,
        RelationType.PARENT,
    ) == [address3]
예제 #4
0
from corm import Storage, Entity, Relationship, RelationType

storage = Storage()


class Address(Entity):
    street: str
    number: int


class User(Entity):
    name: str
    address: Address = Relationship(
        entity_type=Address,
        relation_type=RelationType.PARENT,
    )


address = Address({'street': 'First', 'number': 1}, storage)
john = User({'name': 'John'}, storage)

storage.make_relation(
    from_=john,
    to_=address,
    relation_type=RelationType.PARENT,
)

assert john.address == address