async def test_preload_has_many(repo): user = await repo.insert(User()) await repo.insert(Pet(owner_id=user.id)) await repo.preload(user, "pets") assert len(user.pets) == 1 await repo.insert(Pet(owner_id=user.id)) await repo.preload(user, "pets") assert len(user.pets) == 2
async def test_select_dict(repo): user = await repo.insert(User()) pet = await repo.insert(Pet(owner_id=user.id)) query = Query(User).join("pets", "p").select({ "user_id": "id", "pet_id": "p__id" }) assert await repo.one(query) == {"user_id": user.id, "pet_id": pet.id}
async def test_preload_multiple(repo): user = await repo.insert(User()) home = await repo.insert(Home(owner_id=user.id)) pet = await repo.insert(Pet(owner_id=user.id)) await repo.preload(user, ["home", "pets"]) assert user.home.id == home.id assert user.pets[0].id == pet.id
async def test_select_nested(repo): user = await repo.insert(User()) pet = await repo.insert(Pet(owner_id=user.id)) query = Query(User).join("pets", "p").select(("id", { "pet": [{ "id": "p__id" }] })) assert await repo.one(query) == (user.id, {"pet": [{"id": pet.id}]})
async def test_preload_nested(repo): user = await repo.insert(User()) home = await repo.insert(Home(owner_id=user.id)) pet = await repo.insert(Pet(owner_id=user.id)) await repo.preload(pet, "owner.home.owner") assert pet.owner.id == user.id assert pet.owner.home.id == home.id assert pet.owner.home.owner.id == user.id
def test_join_association_nested() -> None: join = Join(User, ["pets", "owner"]) assoc = join.find_association() assert assoc == Pet.association("owner")
async def test_select_tuple(repo): user = await repo.insert(User()) pet = await repo.insert(Pet(owner_id=user.id)) query = Query(User).join("pets", "p").select(("id", "p__id")) assert await repo.one(query) == (user.id, pet.id)