@@ -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