Skip to content

Commit c75b088

Browse files
authored
add async version of modify (#92)
* adding async_modify * version
1 parent 66eae5c commit c75b088

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

mongoengine_plus/aio/async_query_set.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,22 @@ async def async_delete(
3636
return await create_awaitable(
3737
self.delete, write_concern, _from_doc_delete, cascade_refs
3838
)
39+
40+
async def async_modify(
41+
self,
42+
upsert=False,
43+
full_response=False,
44+
remove=False,
45+
new=False,
46+
array_filters=None,
47+
**update,
48+
):
49+
return await create_awaitable(
50+
self.modify,
51+
upsert=upsert,
52+
full_response=full_response,
53+
remove=remove,
54+
new=new,
55+
array_filters=array_filters,
56+
**update,
57+
)

mongoengine_plus/version.py

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

tests/aio/test_async_query_set.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,45 @@ async def test_async_delete(cities):
6666
await City.objects(state='CDMX').async_delete()
6767
city = await City.objects(state='CDMX').async_first()
6868
assert not city
69+
70+
71+
@pytest.mark.asyncio
72+
async def test_async_modify(cities):
73+
# Test modifying a document and returning the updated version
74+
city = await City.objects(name='San Cristobal').async_modify(
75+
set__name='San Cristobal de las Casas', new=True
76+
)
77+
assert city.name == 'San Cristobal de las Casas'
78+
79+
# Verify the change was saved to the database
80+
db_city = await City.objects(
81+
name='San Cristobal de las Casas'
82+
).async_first()
83+
assert db_city is not None
84+
assert db_city.name == 'San Cristobal de las Casas'
85+
86+
# Test modifying a document and returning the original version
87+
city = await City.objects(name='Tuxtla Gutiérrez').async_modify(
88+
set__name='Tuxtla', new=False
89+
)
90+
assert city.name == 'Tuxtla Gutiérrez'
91+
92+
# Verify the change was still made
93+
db_city = await City.objects(name='Tuxtla').async_first()
94+
assert db_city is not None
95+
96+
# Test upsert when document doesn't exist
97+
new_city = await City.objects(name='Cancún').async_modify(
98+
set__state='Quintana Roo', upsert=True, new=True
99+
)
100+
assert new_city is not None
101+
assert new_city.name == 'Cancún'
102+
assert new_city.state == 'Quintana Roo'
103+
104+
# Test remove option
105+
city = await City.objects(name='Cancún').async_modify(remove=True)
106+
assert city.name == 'Cancún'
107+
108+
# Verify the document was removed
109+
db_city = await City.objects(name='Cancún').async_first()
110+
assert db_city is None

0 commit comments

Comments
 (0)