1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
from pydantic import Field
from pydantic.main import BaseModel
from tests.odm.models import DocumentTestModel
async def test_aggregate(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.aggregate(
[{"$group": {"_id": "$test_str", "total": {"$sum": "$test_int"}}}]
).to_list()
assert len(result) == 3
assert {"_id": "cuatro", "total": 0} in result
assert {"_id": "dos", "total": 1} in result
assert {"_id": "uno", "total": 6} in result
async def test_aggregate_with_filter(documents):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = (
await DocumentTestModel.find(DocumentTestModel.test_int >= 1)
.aggregate(
[{"$group": {"_id": "$test_str", "total": {"$sum": "$test_int"}}}]
)
.to_list()
)
assert len(result) == 2
assert {"_id": "dos", "total": 1} in result
assert {"_id": "uno", "total": 6} in result
async def test_aggregate_with_item_model(documents):
class OutputItem(BaseModel):
id: str = Field(None, alias="_id")
total: int
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
ids = []
async for i in DocumentTestModel.aggregate(
[{"$group": {"_id": "$test_str", "total": {"$sum": "$test_int"}}}],
projection_model=OutputItem,
):
if i.id == "cuatro":
assert i.total == 0
elif i.id == "dos":
assert i.total == 1
elif i.id == "uno":
assert i.total == 6
else:
raise KeyError
ids.append(i.id)
assert set(ids) == {"cuatro", "dos", "uno"}
async def test_aggregate_with_session(documents, session):
await documents(4, "uno")
await documents(2, "dos")
await documents(1, "cuatro")
result = await DocumentTestModel.aggregate(
[{"$group": {"_id": "$test_str", "total": {"$sum": "$test_int"}}}],
session=session,
).to_list()
assert len(result) == 3
assert {"_id": "cuatro", "total": 0} in result
assert {"_id": "dos", "total": 1} in result
assert {"_id": "uno", "total": 6} in result
|