Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions mongoengine_plus/aio/async_query_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,22 @@ async def async_delete(
return await create_awaitable(
self.delete, write_concern, _from_doc_delete, cascade_refs
)

async def async_modify(
self,
upsert=False,
full_response=False,
remove=False,
new=False,
array_filters=None,
**update,
):
return await create_awaitable(
self.modify,
upsert=upsert,
full_response=full_response,
remove=remove,
new=new,
array_filters=array_filters,
**update,
)
2 changes: 1 addition & 1 deletion mongoengine_plus/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.1'
__version__ = '1.1.0'
42 changes: 42 additions & 0 deletions tests/aio/test_async_query_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,45 @@ async def test_async_delete(cities):
await City.objects(state='CDMX').async_delete()
city = await City.objects(state='CDMX').async_first()
assert not city


@pytest.mark.asyncio
async def test_async_modify(cities):
# Test modifying a document and returning the updated version
city = await City.objects(name='San Cristobal').async_modify(
set__name='San Cristobal de las Casas', new=True
)
assert city.name == 'San Cristobal de las Casas'

# Verify the change was saved to the database
db_city = await City.objects(
name='San Cristobal de las Casas'
).async_first()
assert db_city is not None
assert db_city.name == 'San Cristobal de las Casas'

# Test modifying a document and returning the original version
city = await City.objects(name='Tuxtla Gutiérrez').async_modify(
set__name='Tuxtla', new=False
)
assert city.name == 'Tuxtla Gutiérrez'

# Verify the change was still made
db_city = await City.objects(name='Tuxtla').async_first()
assert db_city is not None

# Test upsert when document doesn't exist
new_city = await City.objects(name='Cancún').async_modify(
set__state='Quintana Roo', upsert=True, new=True
)
assert new_city is not None
assert new_city.name == 'Cancún'
assert new_city.state == 'Quintana Roo'

# Test remove option
city = await City.objects(name='Cancún').async_modify(remove=True)
assert city.name == 'Cancún'

# Verify the document was removed
db_city = await City.objects(name='Cancún').async_first()
assert db_city is None
Loading