Skip to content

Commit

Permalink
Add AioAggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzsochi committed Dec 28, 2017
1 parent 8f0c4f3 commit 70141e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/tests_aio/test_aggregate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from random import randint

import pytest

from yadm import Document
from yadm import fields
# from yadm.aio.aggregation import AioAggregator


class Doc(Document):
__collection__ = 'docs'
i = fields.IntegerField()


@pytest.fixture(scope='function')
def docs(loop, db):
async def gen_docs():
docs = []
for n in range(randint(10, 20)):
doc = Doc(i=randint(-666, 666))
await db.insert(doc)
docs.append(doc)

return docs

return loop.run_until_complete(gen_docs())


def test_async_for(loop, db, docs):
async def test():
agg = db.aggregate(Doc).match(i={'$gt': 0}).project(n='$i')
count = 0

async for item in agg:
assert item['n'] > 0
count += 1

assert count == len([d.i for d in docs if d.i > 0])

loop.run_until_complete(test())
6 changes: 6 additions & 0 deletions yadm/aio/aggregation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from yadm.aggregation import BaseAggregator


class AioAggregator(BaseAggregator):
async def __aiter__(self):
return self._cursor
1 change: 1 addition & 0 deletions yadm/aio/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from yadm.serialize import to_mongo, from_mongo

from .queryset import AioQuerySet
from .aggregation import AioAggregator

PYMONGO_VERSION = pymongo.version_tuple

Expand Down

0 comments on commit 70141e8

Please sign in to comment.