Skip to content

Commit

Permalink
initial try
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-andre committed Mar 13, 2024
1 parent 56e3dd4 commit 424444e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyodmongo/services/model_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def resolve_ref_pipeline(cls: BaseModel, pipeline: list, path: list):
),
is_reference_list=db_field_info.is_list,
)
else:
resolve_ref_pipeline(cls=db_field_info.field_type, pipeline=pipeline)
pipeline += resolve_project_pipeline(cls=cls)
return pipeline

Expand Down
53 changes: 53 additions & 0 deletions tests/test_reference_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,56 @@ class MyModel2(DbModel):

assert MyModel1._pipeline == []
assert MyModel2._pipeline == ["manual pipeline here"]


def test_recursive_reference_pipeline():
from pprint import pprint

class A(DbModel):
attr_1: str = "One"
_collection: ClassVar = "a"

class B(BaseModel):
attr_2: str = "Two"
a: A | Id = A()

class C(DbModel):
attr_3: str = "Three"
b: B = B()
a: A | Id = A()
_collection: ClassVar = "c"

expected = [
{
"$lookup": {
"as": "a",
"foreignField": "_id",
"from": "a",
"localField": "a",
"pipeline": [
{
"$project": {
"_id": True,
"attr_1": True,
"created_at": True,
"updated_at": True,
}
}
],
}
},
{"$set": {"a": {"$arrayElemAt": ["$a", 0]}}},
{
"$project": {
"_id": True,
"a": True,
"attr_3": True,
"b": True,
"created_at": True,
"updated_at": True,
}
},
]

print()
pprint(C._reference_pipeline)

0 comments on commit 424444e

Please sign in to comment.