From 12bdad9ec6fb033abccac8bb4d4b75404260b07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Thu, 16 Mar 2023 12:34:10 -0600 Subject: [PATCH 1/2] async bulk insert --- mongoengine_plus/aio/async_query_set.py | 11 +++++++++++ tests/aio/test_async_query_set.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/mongoengine_plus/aio/async_query_set.py b/mongoengine_plus/aio/async_query_set.py index fd3e1b6..35d072f 100644 --- a/mongoengine_plus/aio/async_query_set.py +++ b/mongoengine_plus/aio/async_query_set.py @@ -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 + ) diff --git a/tests/aio/test_async_query_set.py b/tests/aio/test_async_query_set.py index d82062b..eb80916 100644 --- a/tests/aio/test_async_query_set.py +++ b/tests/aio/test_async_query_set.py @@ -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)) From 349814d747705b7a3f2e9309f95dcd2a21117e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Thu, 16 Mar 2023 12:35:52 -0600 Subject: [PATCH 2/2] bulk insert --- mongoengine_plus/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongoengine_plus/version.py b/mongoengine_plus/version.py index 156d6f9..b794fd4 100644 --- a/mongoengine_plus/version.py +++ b/mongoengine_plus/version.py @@ -1 +1 @@ -__version__ = '0.0.4' +__version__ = '0.1.0'