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
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
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 == []