Skip to content

Commit eeed51c

Browse files
committed
fixed issue preventing unit tests from completing.
1 parent 6614e83 commit eeed51c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@
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")
3054
async def async_db_session() -> AsyncGenerator[AsyncSession, None]:
3155
"""Create a fresh database for each test."""

tests/test_main_coverage.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff 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")

0 commit comments

Comments
 (0)