Skip to content

Commit

Permalink
🐛 Fix class QueryOperators types tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-andre committed May 23, 2024
1 parent f127f02 commit 1b53e79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pyodmongo/models/query_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _LogicalOperator(QueryOperator):
"""

operator: str
operators: tuple[ComparisonOperator, ...]
operators: tuple[Any, ...]

def to_dict(self):
acu_list = []
Expand All @@ -83,7 +83,7 @@ class LogicalOperator(_LogicalOperator):
intricate query logic necessary for advanced database operations.
"""

operators: tuple[ComparisonOperator | _LogicalOperator, ...]
operators: tuple[Any, ...]


class ElemMatchOperator(QueryOperator):
Expand All @@ -102,7 +102,7 @@ class ElemMatchOperator(QueryOperator):
"""

field: Any
operators: tuple[ComparisonOperator | _LogicalOperator, ...]
operators: tuple[Any, ...]

def to_dict(self):
elem_match = {}
Expand Down
6 changes: 3 additions & 3 deletions pyodmongo/queries/query_string.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ..models.db_model import DbModel
from ..models.db_field_info import DbField
from ..models.query_operators import LogicalOperator, ComparisonOperator
from ..models.query_operators import QueryOperator
from .operators import and_, sort
from typing import Type
from datetime import datetime
Expand Down Expand Up @@ -69,8 +69,8 @@ def js_regex_to_python(js_regex_str):
def mount_query_filter(
Model: Type[DbModel],
items: dict,
initial_comparison_operators: list[ComparisonOperator],
) -> LogicalOperator:
initial_comparison_operators: list[QueryOperator],
) -> QueryOperator:
"""
Constructs a MongoDB query filter from a dictionary of conditions and initializes
additional comparison operators based on the Model's field definitions.
Expand Down
36 changes: 36 additions & 0 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,39 @@ class MyModel(DbModel):
}
}
}


def test_mount_query_filter_with_elem_match():
class MyBaseModel(MainBaseModel):
attr_1: int
attr_2: str

class MyModel(DbModel):
attr_3: str
attr_4: list[MyBaseModel]
_collection: ClassVar = "my_model"

dict_input = {"attr_3_eq": "value_3"}
elem_match_operator = elem_match(
MyBaseModel.attr_1 == "value_1",
MyBaseModel.attr_2 == "value_2",
field=MyModel.attr_4,
)
query, _ = mount_query_filter(
Model=MyModel,
items=dict_input,
initial_comparison_operators=[elem_match_operator],
)
assert query.to_dict() == {
"$and": [
{
"attr_4": {
"$elemMatch": {
"attr_1": {"$eq": "value_1"},
"attr_2": {"$eq": "value_2"},
}
}
},
{"attr_3": {"$eq": "value_3"}},
]
}

0 comments on commit 1b53e79

Please sign in to comment.