File tree Expand file tree Collapse file tree 2 files changed +29
-5
lines changed
Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 2626)
2727
2828
29+ def pytest_sessionfinish (session , exitstatus ):
30+ """Clean up async engine after all tests complete."""
31+ import asyncio
32+ # Dispose of the async engine to close all connections and threads
33+ try :
34+ # Try to dispose using the current event loop if available
35+ try :
36+ loop = asyncio .get_event_loop ()
37+ if not loop .is_closed () and not loop .is_running ():
38+ loop .run_until_complete (test_engine .dispose ())
39+ else :
40+ # Create a new event loop for cleanup
41+ asyncio .run (test_engine .dispose ())
42+ except RuntimeError :
43+ # No event loop exists, create one
44+ asyncio .run (test_engine .dispose ())
45+ except Exception :
46+ # Fallback: try to close the underlying sync engine
47+ try :
48+ test_engine .sync_engine .dispose (close = True )
49+ except Exception :
50+ pass
51+
52+
2953@pytest .fixture (scope = "function" )
3054async def async_db_session () -> AsyncGenerator [AsyncSession , None ]:
3155 """Create a fresh database for each test."""
Original file line number Diff line number Diff line change @@ -116,17 +116,16 @@ def test_get_user_preferences_empty(authenticated_client):
116116 assert data ["preferences" ] == {}
117117
118118
119- def test_get_user_preferences_with_data (authenticated_client , async_db_session , test_user ):
119+ async def test_get_user_preferences_with_data (authenticated_client , async_db_session , test_user ):
120120 """Test getting user preferences with saved data."""
121121 import json
122122
123123 # Set preferences on user
124124 test_user .preferences = json .dumps ({"theme" : "dark" , "fontSize" : 14 })
125125 async_db_session .add (test_user )
126126
127- # Use sync over async to commit
128- import asyncio
129- asyncio .get_event_loop ().run_until_complete (async_db_session .commit ())
127+ # Commit the changes
128+ await async_db_session .commit ()
130129
131130 response = authenticated_client .get ("/user/preferences" )
132131 assert response .status_code == 200
@@ -413,9 +412,10 @@ def test_create_snapshot_cleanup_old(authenticated_client):
413412 )
414413
415414 # Create 12 snapshots
415+ import time
416416 for i in range (12 ):
417417 authenticated_client .post ("/documents/snapshot/cleanup.json" )
418- asyncio .sleep (0.01 ) # Small delay to ensure different timestamps
418+ time .sleep (0.01 ) # Small delay to ensure different timestamps
419419
420420 # List snapshots
421421 response = authenticated_client .get ("/documents/snapshots/cleanup.json" )
You can’t perform that action at this time.
0 commit comments