Skip to content

Commit 06b822c

Browse files
authored
Merge pull request #1169 from transformerlab/fix/pytests-new
Get rid of create_db_and_tables and fix tests to avoid in-memory dbs
2 parents 260df17 + b3bcb68 commit 06b822c

File tree

12 files changed

+71
-2836
lines changed

12 files changed

+71
-2836
lines changed

api/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ async def lifespan(app: FastAPI):
119119
galleries.update_gallery_cache()
120120
spawn_fastchat_controller_subprocess()
121121
await db.init() # This now runs Alembic migrations internally
122-
# create_db_and_tables() is deprecated - migrations are handled in db.init()
123122
print("✅ SEED DATA")
124123
# Initialize experiments
125124
seed_default_experiments()

api/test/api/conftest.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
os.environ["TRANSFORMERLAB_REFRESH_SECRET"] = "test-refresh-secret-for-testing-only"
2424
os.environ["EMAIL_METHOD"] = "dev" # Use dev mode for tests (no actual email sending)
2525

26-
# Use in-memory database for tests
27-
os.environ["DATABASE_URL"] = "sqlite+aiosqlite:///:memory:"
26+
# Use temporary file-based database for tests (easier to debug than in-memory)
27+
test_db_dir = os.path.join("test", "tmp", "db")
28+
os.makedirs(test_db_dir, exist_ok=True)
29+
test_db_path = os.path.join(test_db_dir, "test_llmlab.sqlite3")
30+
os.environ["DATABASE_URL"] = f"sqlite+aiosqlite:///{test_db_path}"
2831

2932
from api import app # noqa: E402
3033

@@ -72,13 +75,43 @@ def request(self, method, url, **kwargs):
7275
return super().request(method, url, **kwargs)
7376

7477

78+
@pytest.fixture(scope="session", autouse=True)
79+
def cleanup_test_db():
80+
"""Clean up test database file after all tests complete"""
81+
yield
82+
# Clean up database file and related files (WAL, SHM)
83+
test_db_path = os.path.join("test", "tmp", "db", "test_llmlab.sqlite3")
84+
for ext in ["", "-wal", "-shm"]:
85+
db_file = test_db_path + ext
86+
if os.path.exists(db_file):
87+
try:
88+
os.remove(db_file)
89+
except OSError:
90+
pass # Ignore errors if file is locked or already removed
91+
92+
7593
@pytest.fixture(scope="session")
7694
def client():
77-
# Initialize database tables for tests
78-
from transformerlab.shared.models.user_model import create_db_and_tables # noqa: E402
95+
# Initialize database tables for tests using Alembic migrations (same as production)
96+
from transformerlab.db.session import run_alembic_migrations # noqa: E402
7997
from transformerlab.services.experiment_init import seed_default_admin_user # noqa: E402
8098

81-
asyncio.run(create_db_and_tables())
99+
# Ensure test database directory exists
100+
test_db_dir = os.path.join("test", "tmp", "db")
101+
os.makedirs(test_db_dir, exist_ok=True)
102+
103+
# Remove existing test database if it exists (start fresh)
104+
test_db_path = os.path.join(test_db_dir, "test_llmlab.sqlite3")
105+
for ext in ["", "-wal", "-shm"]:
106+
db_file = test_db_path + ext
107+
if os.path.exists(db_file):
108+
try:
109+
os.remove(db_file)
110+
except OSError:
111+
pass
112+
113+
# Run Alembic migrations to create database schema (matches production)
114+
asyncio.run(run_alembic_migrations())
82115
asyncio.run(seed_default_admin_user())
83116
controller_log_dir = os.path.join("test", "tmp", "workspace", "logs")
84117
os.makedirs(controller_log_dir, exist_ok=True)

api/test/api/test_experiment_export.py

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)