Skip to content

Commit

Permalink
Merge pull request #45 from mohamadkhalaj/main
Browse files Browse the repository at this point in the history
Improve group
  • Loading branch information
seyed-dev authored Nov 6, 2023
2 parents 1112d4a + 4649492 commit a6d08bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 4 additions & 6 deletions aggify/aggify.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,10 @@ def project(self, **kwargs: QueryParams) -> "Aggify":
@last_out_stage_check
def group(self, expression: Union[str, None] = "id") -> "Aggify":
if expression:
check_fields_exist(self.base_model, [expression])
expression = (
get_db_field(self.base_model, expression, add_dollar_sign=True)
if expression
else None
)
try:
expression = "$" + self.get_field_name_recursively(expression)
except InvalidField:
pass
self.pipelines.append({"$group": {"_id": expression}})
return self

Expand Down
4 changes: 4 additions & 0 deletions tests/test_aggify.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,7 @@ def test_aggify_get_item_slice_negative_start(self):
with pytest.raises(MongoIndexError):
# noinspection PyUnusedLocal
var = aggify.filter(name=1)[slice(-5, -1)]

def test_group_invalid_field(self):
thing = list(Aggify(BaseModel).group("invalid").annotate("name", "first", 2))
assert thing[0]["$group"] == {"_id": "invalid", "name": {"$first": 2}}
10 changes: 10 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ class ParameterTestCase:
),
expected_query=[{"$group": {"_id": "$owner_id", "sss": {"$first": "sss"}}}],
),
ParameterTestCase(
compiled_query=(
Aggify(PostDocument)
.group("stat__like_count")
.annotate("sss", "first", "sss")
),
expected_query=[
{"$group": {"_id": "$stat.like_count", "sss": {"$first": "sss"}}}
],
),
]


Expand Down

0 comments on commit a6d08bd

Please sign in to comment.