Example #1
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,
        )
Example #2
0
def test_add_by_primary_key():
    class User(Entity):
        id: int = Field(pk=True)

    storage = Storage()
    john = User(
        data={'id': 1},
        storage=storage,
    )

    assert storage.get(User.id, 1) == john
Example #3
0
def test_multiple_primary_keys():
    class User(Entity):
        id: int = Field(pk=True)
        guid: str = Field(pk=True)
        name: str

    storage = Storage()
    user = User({'id': 1, 'guid': '1234', 'name': 'john'}, storage)

    assert storage.get(User.id, 1) == user
    assert storage.get(User.guid, '1234') == user
    assert storage.get(User.name, 'john') is None
Example #4
0
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]
Example #5
0
def test_set_value_many():
    class Address(Entity):
        street: str

    class User(Entity):
        id: int
        addresses: t.List[Address] = Nested(entity_type=Address, many=True)

    storage = Storage()
    john = User(
        data={
            'id': 1,
            'name': 'John',
            'description': 'john smith',
            'addresses': [{
                'street': 'kirova',
            }],
        },
        storage=storage,
    )

    address = Address(data={'street': 'lenina'}, storage=storage)
    john.addresses = [address]

    assert john.addresses == [address]
    assert john.dict() == {
        'id': 1,
        'name': 'John',
        'description': 'john smith',
        'addresses': [{
            'street': 'lenina',
        }],
    }
Example #6
0
def test_make_back_relationship():
    class SomeEntity(Entity):
        id: int = Field(pk=True)
        name: str
        holder: 'EntityHolder' = Relationship('EntityHolder')

    class EntityHolder(Entity):
        entities: t.List[SomeEntity] = KeyNested(
            related_entity_field=SomeEntity.id,
            origin='entity_ids',
            many=True,
            back_relation=True,
        )

    storage = Storage()
    entity = SomeEntity({'id': 123, 'name': 'entity'}, storage=storage)
    holder = EntityHolder({'entity_ids': [123, 456]}, storage=storage)
    delayed_entity = SomeEntity(
        {
            'id': 456,
            'name': 'delayed entity',
        },
        storage=storage,
    )

    assert holder.entities == [entity, delayed_entity]
    assert entity.holder == holder
    assert delayed_entity.holder == holder

    assert holder.dict({'entity_ids': [123, 456]})
    assert entity.dict() == {'id': 123, 'name': 'entity'}
    assert delayed_entity.dict() == {'id': 456, 'name': 'delayed entity'}
Example #7
0
def test_base_entity():
    class User(Entity):
        id: int
        name: str = Field(default='Bob')
        description: str = 'some text'

    storage = Storage()
    john = User(
        data={
            'id': 1,
            'name': 'John',
            'address': 'First st. 1',
        },
        storage=storage,
    )

    assert john.id == 1
    assert john.name == 'John'
    assert john.description == 'some text'

    john.name = 'Not John'

    assert john.dict(strip_none=True) == {
        'id': 1,
        'name': 'Not John',
        'description': 'some text',
        'address': 'First st. 1',
    }

    with pytest.raises(ValueError):
        User(
            data={'name': 'Bob'},
            storage=storage,
        )
Example #8
0
def test_change_field_value():
    class User(Entity):
        id: int = Field(pk=True, mode=AccessMode.GET_LOAD_DUMP)
        guid: str = Field(pk=True)
        name: str = Field(mode=AccessMode.GET_LOAD_DUMP)
        description: str

    storage = Storage()
    user = User(
        {
            'id': 1,
            'guid': '1234',
            'name': 'john',
            'description': '',
        },
        storage,
    )

    with pytest.raises(ValueError):
        user.id = 2

    with pytest.raises(ValueError):
        user.name = 'Bob'

    user.guid = '4321'
    user.description = 'cool guy'

    assert user.dict() == {
        'id': 1,
        'guid': '4321',
        'name': 'john',
        'description': 'cool guy',
    }
Example #9
0
def test_set_value():
    class Address(Entity):
        street: str

    class User(Entity):
        id: int
        address: Address = Nested(entity_type=Address)

    storage = Storage()
    john = User(
        data={
            'id': 1,
            'name': 'John',
            'description': 'john smith',
            'address': {
                'street': 'kirova',
            },
        },
        storage=storage,
    )

    address = Address(data={'street': 'lenina'}, storage=storage)
    john.address = address

    assert john.address is address
    assert john.dict() == {
        'id': 1,
        'name': 'John',
        'description': 'john smith',
        'address': {
            'street': 'lenina',
        },
    }
Example #10
0
def test_same_pk():
    class User(Entity):
        id: int = Field(pk=True)

    storage = Storage()
    User({'id': 1}, storage)

    with pytest.raises(ValueError):
        User({'id': 1}, storage)
Example #11
0
def test_self_related():
    class Item(Entity):
        id: int = Field(pk=True)
        items: t.List['Item'] = Nested(  # noqa: F821
            entity_type='Item',
            many=True,
            back_relation=True,
            default=list,
        )
        parent: 'Item' = Relationship(entity_type='Item')  # noqa: F821

    storage = Storage()
    item1 = Item(
        {
            'id': 1,
            'items': [
                {
                    'id': 2,
                    'items': [{
                        'id': 4,
                    }],
                },
                {
                    'id': 3,
                },
            ],
        },
        storage,
    )
    item2, item3 = item1.items
    item4 = storage.get(Item.id, 4)

    assert item1.id == 1
    assert item1.parent is None

    assert item2.id == 2
    assert item2.parent == item1
    assert item2.items == [item4]

    assert item3.id == 3
    assert item3.parent == item1
    assert item3.items == []
Example #12
0
def test_set_value_many_with_back_relationship():
    class Address(Entity):
        street: str
        user: '******' = Relationship(entity_type='User')

    class User(Entity):
        id: int
        addresses: t.List[Address] = Nested(
            entity_type=Address,
            back_relation=True,
            many=True,
        )

    storage = Storage()
    john = User(
        data={
            'id': 1,
            'name': 'John',
            'description': 'john smith',
            'addresses': [
                {
                    'street': 'kirova 1',
                },
                {
                    'street': 'kirova 2',
                },
            ],
        },
        storage=storage,
    )

    old_address1, old_address2 = john.addresses

    address1 = Address(data={'street': 'lenina 1'}, storage=storage)
    address2 = Address(data={'street': 'lenina 2'}, storage=storage)
    john.addresses = [address1, address2]

    assert old_address1.user is None
    assert old_address2.user is None
    assert address1.user is john
    assert address2.user is john
    assert john.dict() == {
        'id': 1,
        'name': 'John',
        'description': 'john smith',
        'addresses': [
            {
                'street': 'lenina 1',
            },
            {
                'street': 'lenina 2',
            },
        ],
    }
Example #13
0
def test_nested_key():
    class SomeEntity(Entity):
        id: int = Field(pk=True)
        name: str

    class EntityHolder(Entity):
        entity: SomeEntity = KeyNested(
            related_entity_field=SomeEntity.id,
            origin='entity_id',
        )

    storage = Storage()
    entity = SomeEntity({'id': 123, 'name': 'entity'}, storage=storage)
    holder = EntityHolder({'entity_id': 123}, storage=storage)

    assert holder.entity == entity
    assert holder.dict() == {'entity_id': 123}

    class ManyEntityHolder(Entity):
        entities: t.List[SomeEntity] = KeyNested(
            related_entity_field=SomeEntity.id,
            origin='entity_ids',
            many=True,
        )

    storage = Storage()
    entity1 = SomeEntity({'id': 123, 'name': 'entity1'}, storage=storage)
    entity2 = SomeEntity({'id': 321, 'name': 'entity2'}, storage=storage)
    holder = ManyEntityHolder({'entity_ids': [123, 321]}, storage=storage)

    assert holder.entities == [entity1, entity2]
    assert holder.dict() == {'entity_ids': [123, 321]}

    storage = Storage()
    SomeEntity({'id': 123, 'name': 'entity1'}, storage=storage)
    SomeEntity({'id': 321, 'name': 'entity2'}, storage=storage)
    holder = ManyEntityHolder({'entity_ids': [123, 99999]}, storage=storage)

    with pytest.raises(ValueError):
        holder.entities
Example #14
0
def test_default_values():
    class Data(Entity):
        in_place = 1
        in_place_callable = list
        in_field = Field(default=2)
        in_field_callable = Field(default=list)

    data = Data(data={}, storage=Storage())

    assert data.in_place == 1
    assert data.in_place_callable == list  # NOTICE: expected behaviour
    assert data.in_field == 2
    assert data.in_field_callable == []
Example #15
0
def test_change_key_value():
    class SomeEntity(Entity):
        id: int = Field(pk=True)
        name: str

    class EntityHolder(Entity):
        entity: SomeEntity = KeyNested(
            related_entity_field=SomeEntity.id,
            origin='entity_id',
        )

    storage = Storage()
    entity = SomeEntity({'id': 123, 'name': 'entity'}, storage=storage)
    holder = EntityHolder({'entity_id': 123}, storage=storage)

    entity.id = 321

    assert holder.entity == entity
    assert holder.dict() == {'entity_id': 321}
Example #16
0
def test_set_value_with_back_relationship():
    class Address(Entity):
        street: str
        user: '******' = Relationship(entity_type='User')

    class User(Entity):
        id: int
        address: Address = Nested(
            entity_type=Address,
            back_relation=True,
        )

    storage = Storage()
    john = User(
        data={
            'id': 1,
            'name': 'John',
            'description': 'john smith',
            'address': {
                'street': 'kirova',
            },
        },
        storage=storage,
    )

    old_address = john.address

    assert old_address.user is john

    address = Address(data={'street': 'lenina'}, storage=storage)
    john.address = address

    assert old_address.user is None
    assert address.user == john
    assert john.address is address
    assert john.dict() == {
        'id': 1,
        'name': 'John',
        'description': 'john smith',
        'address': {
            'street': 'lenina',
        },
    }
Example #17
0
def test_complex_key():
    class SomeEntity(Entity):
        id: int = Field(pk=True)
        name: str

    class EntityKeyManager(KeyManager):
        def get(self, data):
            return data['id']

        def prepare_to_set(self, entity: SomeEntity) -> t.Any:
            return {'id': entity.id}

    class ManyEntityHolder(Entity):
        entities: t.List[SomeEntity] = KeyNested(
            related_entity_field=SomeEntity.id,
            origin='entity_ids',
            many=True,
            key_manager=EntityKeyManager(),
        )

    storage = Storage()
    entity1 = SomeEntity({'id': 123, 'name': 'entity1'}, storage=storage)
    entity2 = SomeEntity({'id': 321, 'name': 'entity2'}, storage=storage)
    holder = ManyEntityHolder(
        {
            'entity_ids': [
                {
                    'id': 123,
                },
                {
                    'id': 321,
                },
            ],
        },
        storage=storage,
    )

    assert holder.entities == [entity1, entity2]
    assert holder.dict() == {'entity_ids': [{'id': 123}, {'id': 321}]}
Example #18
0
def test_origin_destination_setting():
    class Data(Entity):
        attr1 = Field(origin='_attr1', destination='attr1_')
        attr2 = Field(origin='_attr2')
        attr3 = Field(destination='attr3_')

    storage = Storage()
    data = Data(
        data={
            '_attr1': 1,
            '_attr2': 2,
            'attr3': 3,
        },
        storage=storage,
    )

    assert data.attr1 == 1
    assert data.attr2 == 2
    assert data.attr3 == 3
    assert data.dict() == {
        'attr1_': 1,
        '_attr2': 2,
        'attr3_': 3,
    }
Example #19
0
def test_nested():
    storage = Storage()

    class Address(Entity):
        street: str

    class User(Entity):
        id: int
        name: str = Field(default='Bob')
        address: Address = Nested(entity_type=Address)

    john = User(
        data={
            'id': 1,
            'name': 'John',
            'description': 'john smith',
            'address': {
                'street': 'kirova',
            },
        },
        storage=storage,
    )

    assert john.address.street == 'kirova'
Example #20
0
def test_change_values():
    class SomeEntity(Entity):
        id: int = Field(pk=True)
        name: str
        holder: 'EntityHolder' = Relationship(entity_type='EntityHolder')
        many_holder: 'ManyEntityHolder' = Relationship(
            entity_type='ManyEntityHolder', )

    class EntityKeyManager(KeyManager):
        def get(self, data):
            return data['id']

        def prepare_to_set(self, entity: SomeEntity) -> t.Any:
            return {'id': entity.id}

    class EntityHolder(Entity):
        entity: SomeEntity = KeyNested(
            related_entity_field=SomeEntity.id,
            origin='entity_id',
            key_manager=EntityKeyManager(),
            back_relation=True,
        )

    class ManyEntityHolder(Entity):
        entities: t.List[SomeEntity] = KeyNested(
            related_entity_field=SomeEntity.id,
            origin='entity_ids',
            many=True,
            back_relation=True,
            key_manager=EntityKeyManager(),
        )

    storage = Storage()
    entity1 = SomeEntity({'id': 123, 'name': 'entity1'}, storage=storage)
    entity2 = SomeEntity({'id': 321, 'name': 'entity2'}, storage=storage)
    holder = EntityHolder(
        {
            'entity_id': {
                'id': 123,
            },
        },
        storage=storage,
    )

    holder.entity = entity2

    assert entity1.holder is None
    assert entity2.holder is holder
    assert holder.entity is entity2
    assert holder.dict() == {'entity_id': {'id': 321}}

    many_holder = ManyEntityHolder(
        {
            'entity_ids': [{
                'id': 123,
            }],
        },
        storage=storage,
    )

    many_holder.entities = [entity2]

    assert many_holder.entities == [entity2]
    assert entity1.many_holder is None
    assert entity2.many_holder is many_holder
    assert many_holder.dict() == {
        'entity_ids': [{
            'id': 321,
        }],
    }
Example #21
0
from corm import Storage, Entity, KeyNested, Field


class SomeEntity(Entity):
    id: int = Field(pk=True)
    name: str


class EntityHolder(Entity):
    name: str
    entity: SomeEntity = KeyNested(
        related_entity_field=SomeEntity.id,
        origin='entity_id',
    )


storage = Storage()
entity = SomeEntity({'id': 123, 'name': 'entity'}, storage=storage)
holder = EntityHolder({'entity_id': 123}, storage=storage)

assert holder.entity == entity
Example #22
0
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]
Example #23
0
def test_change_back_relationship_when_many():
    class Address(Entity):
        street: str
        user: '******' = Relationship(entity_type='User')

    class User(Entity):
        id: int
        addresses: t.List[Address] = Nested(
            entity_type=Address,
            back_relation=True,
            many=True,
        )

    storage = Storage()
    john = User(
        data={
            'id': 1,
            'name': 'John',
            'description': 'john smith',
            'addresses': [
                {
                    'street': 'kirova 1',
                },
                {
                    'street': 'kirova 2',
                },
            ],
        },
        storage=storage,
    )

    old_address1, old_address2 = john.addresses

    john.addresses.remove(old_address1)

    assert old_address1.user is None
    assert old_address2.user is john

    john.addresses.clear()

    assert old_address1.user is None
    assert old_address2.user is None
    assert john.addresses == []

    address1 = Address(data={'street': 'lenina 1'}, storage=storage)
    address2 = Address(data={'street': 'lenina 2'}, storage=storage)

    john.addresses.append(address1)

    assert address1.user is john
    assert john.addresses == [address1]

    john.addresses.extend([address2])

    assert address2.user is john
    assert john.addresses == [address1, address2]
    assert john.dict() == {
        'id': 1,
        'name': 'John',
        'description': 'john smith',
        'addresses': [
            {
                'street': 'lenina 1',
            },
            {
                'street': 'lenina 2',
            },
        ],
    }

    with pytest.raises(ValueError):
        john.addresses = None
Example #24
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
Example #25
0
def test_assign_new_value_to_relationship():
    class SomeEntity(Entity):
        name: str

    class EntityHolder(Entity):
        name: str
        entity: SomeEntity = Relationship(entity_type=SomeEntity)

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

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

    holder.entity = entity1

    assert holder.entity is entity1
    assert storage.get_related_entities(
        holder,
        SomeEntity,
        RelationType.RELATED,
    ) == [entity1]

    holder.entity = entity2

    assert holder.entity is entity2
    assert storage.get_related_entities(
        holder,
        SomeEntity,
        RelationType.RELATED,
    ) == [entity2]

    holder.entity = None

    assert holder.entity is None
    assert storage.get_related_entities(
        holder,
        SomeEntity,
        RelationType.RELATED,
    ) == []

    many_holder = ManyEntityHolder(
        data={'name': 'many holder'},
        storage=storage,
    )
    many_holder.entities = [entity1]

    assert many_holder.entities == [entity1]
    assert storage.get_related_entities(
        many_holder,
        SomeEntity,
        RelationType.RELATED,
    ) == [entity1]

    many_holder.entities.append(entity2)

    assert many_holder.entities == [entity1, entity2]
    assert storage.get_related_entities(
        many_holder,
        SomeEntity,
        RelationType.RELATED,
    ) == [entity1, entity2]

    many_holder.entities.remove(entity2)

    assert many_holder.entities == [entity1]
    assert storage.get_related_entities(
        many_holder,
        SomeEntity,
        RelationType.RELATED,
    ) == [entity1]

    many_holder.entities = []

    assert many_holder.entities == []
    assert storage.get_related_entities(
        many_holder,
        SomeEntity,
        RelationType.RELATED,
    ) == []