mongomock_motor icon indicating copy to clipboard operation
mongomock_motor copied to clipboard

It doesn't find by relation entity id

Open resulyrt93 opened this issue 1 year ago • 3 comments

I'm using your excellent tool with beanie but i can't find a record by relation id. You can produce problem with following code block.

Reproduce Code

import asyncio

from beanie import init_beanie, Document, Link
from mongomock_motor import AsyncMongoMockClient


class School(Document):
    name: str

    class Settings:
        name = "school"


class Student(Document):
    name: str
    surname: str
    school: Link[School]

    class Settings:
        name = "student"


async def start_env():
    client = AsyncMongoMockClient()
    await init_beanie(database=client["test-db"], document_models=[School, Student])

    school = School(name="Harvard")
    await school.insert()

    student = Student(name="Barack", surname="Obama", school=school.id)
    await student.insert()

    harvard_students = await Student.find(Student.school.id == school.id).to_list()
    print("Harvard students: ", harvard_students)


if __name__ == "__main__":
    asyncio.run(start_env())

Output

> Harvard students: []

requirements.txt

beanie==1.17.0
mongomock_motor==0.0.21

when I fetch all students or all schools i can see my insertions and relations between them.

Thank you.

resulyrt93 avatar Aug 28 '23 11:08 resulyrt93

Looks like duplicate of #5 and #20

michaelkryukov avatar Aug 28 '23 15:08 michaelkryukov

So do you have any plan or suggestion to fix that issue? I checked duplicate issues but i couldn't find a solution :(

resulyrt93 avatar Aug 28 '23 18:08 resulyrt93

It's an issue with mongomock (https://github.com/mongomock/mongomock/issues/759) that prevents beanie's Link from working. I don't really want to mock anything in this library, so this issue will persisnt until https://github.com/mongomock/mongomock/issues/759 will be fixed. I might consider creating PR into mongomock, but I dont really have time for that right now

michaelkryukov avatar Aug 28 '23 23:08 michaelkryukov