Skip to content

Commit

Permalink
Merge pull request #82 from cuenca-mx/bulk-insert
Browse files Browse the repository at this point in the history
async bulk insert
  • Loading branch information
felipao-mx authored Mar 16, 2023
2 parents 1908356 + 349814d commit f6128be
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions mongoengine_plus/aio/async_query_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ async def async_to_list(self):

async def async_update(self, *u_objs, **query):
return await create_awaitable(self.update, *u_objs, **query)

async def async_insert(
self,
doc_or_docs,
load_bulk=True,
write_concern=None,
signal_kwargs=None,
):
return await create_awaitable(
self.insert, doc_or_docs, load_bulk, write_concern, signal_kwargs
)
2 changes: 1 addition & 1 deletion mongoengine_plus/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.4'
__version__ = '0.1.0'
17 changes: 17 additions & 0 deletions tests/aio/test_async_query_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ async def test_update(cities):
)
sancris = await City.objects.async_get(name__contains='San Cristobal')
assert sancris.name == 'San Cristobal de las Casas'


@pytest.mark.asyncio
async def test_bulk_insert():
cities = [
City(name='Villahermosa', state='Tabasco'),
City(name='Ciudad de México', state='CDMX'),
City(name='Monterrey', state='Nuevo León'),
City(name='San Cristobal', state='Chiapas'),
City(name='Tuxtla Gutiérrez', state='Chiapas'),
]
cities.sort(key=lambda c: c.name)
await City.objects.async_insert(cities, load_bulk=False)

cities_db = list(await City.objects.order_by('+name').async_to_list())
assert len(cities_db) == len(cities)
assert all(a.name == b.name for a, b in zip(cities, cities_db))

0 comments on commit f6128be

Please sign in to comment.