Skip to content

Commit 3129d4a

Browse files
authored
Merge pull request #5 from cuenca-mx/asyncio
testing async methods
2 parents f961042 + 7e6a377 commit 3129d4a

File tree

11 files changed

+140
-4
lines changed

11 files changed

+140
-4
lines changed

mongoengine_plus/aio/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__all__ = ['AsyncDocument']
2+
3+
from .async_document import AsyncDocument
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from mongoengine import Document
2+
3+
from .async_query_set import AsyncQuerySet
4+
from .utils import create_awaitable
5+
6+
7+
class AsyncDocument(Document):
8+
meta = dict(
9+
allow_inheritance=True,
10+
queryset_class=AsyncQuerySet,
11+
)
12+
13+
async def async_save(self, *args, **kwargs):
14+
return await create_awaitable(self.save, *args, **kwargs)
15+
16+
async def async_reload(self, *fields, **kwargs):
17+
return await create_awaitable(self.reload, *fields, **kwargs)
18+
19+
async def async_delete(self, signal_kwargs=None, **write_concern):
20+
return await create_awaitable(
21+
self.delete, signal_kwargs, **write_concern
22+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from mongoengine import QuerySet
2+
3+
from .utils import create_awaitable
4+
5+
6+
class AsyncQuerySet(QuerySet):
7+
async def async_get(self, *q_objs, **query):
8+
return await create_awaitable(self.get, *q_objs, **query)
9+
10+
async def async_count(self, with_limit_and_skip=False):
11+
return await create_awaitable(self.count, with_limit_and_skip)
12+
13+
async def async_to_list(self):
14+
return await create_awaitable(list, self)

mongoengine_plus/aio/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import asyncio
2+
from functools import partial
3+
from typing import Any, Callable
4+
5+
6+
async def create_awaitable(func: Callable, *args, **kwargs) -> Any:
7+
loop = asyncio.get_running_loop()
8+
return await loop.run_in_executor(None, partial(func, *args, **kwargs))

mongoengine_plus/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.0.1'
1+
__version__ = '0.0.2'

requirements-test.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
pytest==6.2.*
2-
pytest-cov==2.11.*
31
black==20.8b1
4-
isort==5.8.*
52
flake8==3.9.*
3+
isort==5.8.*
64
mypy==0.812
75
mongomock==3.22.*
6+
pytest==6.2.*
7+
pytest-asyncio==0.15.1
8+
pytest-cov==2.11.*
89
pytest-freezegun==0.4.*

tests/aio/__init__.py

Whitespace-only changes.

tests/aio/cities.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from mongoengine import StringField
2+
3+
from mongoengine_plus.aio import AsyncDocument
4+
from mongoengine_plus.models import uuid_field
5+
6+
7+
class City(AsyncDocument):
8+
id = StringField(primary_key=True, default=uuid_field('C'))
9+
name = StringField()
10+
state = StringField()

tests/aio/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Generator, List
2+
3+
import pytest
4+
5+
from .cities import City
6+
7+
8+
@pytest.fixture
9+
def cities() -> Generator[List[City], None, None]:
10+
cities = [
11+
City(name='Villahermosa', state='Tabasco'),
12+
City(name='Ciudad de México', state='CDMX'),
13+
City(name='Monterrey', state='Nuevo León'),
14+
City(name='San Cristobal', state='Chiapas'),
15+
City(name='Tuxtla Gutiérrez', state='Chiapas'),
16+
]
17+
for city in cities:
18+
city.save()
19+
yield cities
20+
for city in cities:
21+
city.delete()

tests/aio/test_async_model.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
from mongoengine import DoesNotExist
3+
4+
from .cities import City
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_save():
9+
city = City(name='Tuxtla Gutierrez')
10+
await city.async_save()
11+
city = await City.objects.async_get()
12+
assert city.id is not None
13+
assert city.name == 'Tuxtla Gutierrez'
14+
await city.async_delete()
15+
with pytest.raises(DoesNotExist):
16+
await City.objects.async_get(id=city.id)
17+
18+
19+
@pytest.mark.asyncio
20+
async def test_reload():
21+
city = City(name='Tuxtla Gutierrez')
22+
await city.async_save()
23+
same_city = await City.objects.async_get(id=city.id)
24+
assert same_city.name == city.name
25+
city.name = 'Tuxtla'
26+
await city.async_save()
27+
await same_city.async_reload()
28+
assert same_city.id == city.id
29+
assert same_city.name == city.name
30+
await city.async_delete()

0 commit comments

Comments
 (0)