|
7 | 7 | connect, |
8 | 8 | connect_async, |
9 | 9 | disconnect, |
| 10 | + disconnect_all_async, |
10 | 11 | disconnect_async, |
11 | 12 | get_async_db, |
12 | 13 | is_async_connection, |
@@ -178,3 +179,71 @@ async def test_reconnect_async_different_settings(self): |
178 | 179 |
|
179 | 180 | # Clean up |
180 | 181 | await disconnect_async("reconnect_test2") |
| 182 | + |
| 183 | + @pytest.mark.asyncio |
| 184 | + async def test_disconnect_all_async(self): |
| 185 | + """Test disconnect_all_async only disconnects async connections.""" |
| 186 | + # Create mix of sync and async connections |
| 187 | + connect(db="sync_db1", alias="sync1") |
| 188 | + connect(db="sync_db2", alias="sync2") |
| 189 | + await connect_async(db="async_db1", alias="async1") |
| 190 | + await connect_async(db="async_db2", alias="async2") |
| 191 | + await connect_async(db="async_db3", alias="async3") |
| 192 | + |
| 193 | + # Verify connections exist |
| 194 | + assert not is_async_connection("sync1") |
| 195 | + assert not is_async_connection("sync2") |
| 196 | + assert is_async_connection("async1") |
| 197 | + assert is_async_connection("async2") |
| 198 | + assert is_async_connection("async3") |
| 199 | + |
| 200 | + from mongoengine.connection import _connections |
| 201 | + |
| 202 | + assert len(_connections) == 5 |
| 203 | + |
| 204 | + # Disconnect all async connections |
| 205 | + await disconnect_all_async() |
| 206 | + |
| 207 | + # Verify only async connections were disconnected |
| 208 | + assert "sync1" in _connections |
| 209 | + assert "sync2" in _connections |
| 210 | + assert "async1" not in _connections |
| 211 | + assert "async2" not in _connections |
| 212 | + assert "async3" not in _connections |
| 213 | + |
| 214 | + # Verify sync connections still work |
| 215 | + assert not is_async_connection("sync1") |
| 216 | + assert not is_async_connection("sync2") |
| 217 | + |
| 218 | + # Clean up remaining sync connections |
| 219 | + disconnect("sync1") |
| 220 | + disconnect("sync2") |
| 221 | + |
| 222 | + @pytest.mark.asyncio |
| 223 | + async def test_disconnect_all_async_empty(self): |
| 224 | + """Test disconnect_all_async when no connections exist.""" |
| 225 | + # Should not raise any errors |
| 226 | + await disconnect_all_async() |
| 227 | + |
| 228 | + @pytest.mark.asyncio |
| 229 | + async def test_disconnect_all_async_only_sync(self): |
| 230 | + """Test disconnect_all_async when only sync connections exist.""" |
| 231 | + # Create only sync connections |
| 232 | + connect(db="sync_db1", alias="sync1") |
| 233 | + connect(db="sync_db2", alias="sync2") |
| 234 | + |
| 235 | + from mongoengine.connection import _connections |
| 236 | + |
| 237 | + assert len(_connections) == 2 |
| 238 | + |
| 239 | + # Disconnect all async (should do nothing) |
| 240 | + await disconnect_all_async() |
| 241 | + |
| 242 | + # Verify sync connections still exist |
| 243 | + assert len(_connections) == 2 |
| 244 | + assert "sync1" in _connections |
| 245 | + assert "sync2" in _connections |
| 246 | + |
| 247 | + # Clean up |
| 248 | + disconnect("sync1") |
| 249 | + disconnect("sync2") |
0 commit comments