Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ async def lifespan(app: FastAPI):
galleries.update_gallery_cache()
spawn_fastchat_controller_subprocess()
await db.init() # This now runs Alembic migrations internally
# create_db_and_tables() is deprecated - migrations are handled in db.init()
print("✅ SEED DATA")
# Initialize experiments
seed_default_experiments()
Expand Down
43 changes: 38 additions & 5 deletions api/test/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
os.environ["TRANSFORMERLAB_REFRESH_SECRET"] = "test-refresh-secret-for-testing-only"
os.environ["EMAIL_METHOD"] = "dev" # Use dev mode for tests (no actual email sending)

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

from api import app # noqa: E402

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


@pytest.fixture(scope="session", autouse=True)
def cleanup_test_db():
"""Clean up test database file after all tests complete"""
yield
# Clean up database file and related files (WAL, SHM)
test_db_path = os.path.join("test", "tmp", "db", "test_llmlab.sqlite3")
for ext in ["", "-wal", "-shm"]:
db_file = test_db_path + ext
if os.path.exists(db_file):
try:
os.remove(db_file)
except OSError:
pass # Ignore errors if file is locked or already removed


@pytest.fixture(scope="session")
def client():
# Initialize database tables for tests
from transformerlab.shared.models.user_model import create_db_and_tables # noqa: E402
# Initialize database tables for tests using Alembic migrations (same as production)
from transformerlab.db.session import run_alembic_migrations # noqa: E402
from transformerlab.services.experiment_init import seed_default_admin_user # noqa: E402

asyncio.run(create_db_and_tables())
# Ensure test database directory exists
test_db_dir = os.path.join("test", "tmp", "db")
os.makedirs(test_db_dir, exist_ok=True)

# Remove existing test database if it exists (start fresh)
test_db_path = os.path.join(test_db_dir, "test_llmlab.sqlite3")
for ext in ["", "-wal", "-shm"]:
db_file = test_db_path + ext
if os.path.exists(db_file):
try:
os.remove(db_file)
except OSError:
pass

# Run Alembic migrations to create database schema (matches production)
asyncio.run(run_alembic_migrations())
asyncio.run(seed_default_admin_user())
controller_log_dir = os.path.join("test", "tmp", "workspace", "logs")
os.makedirs(controller_log_dir, exist_ok=True)
Expand Down
113 changes: 0 additions & 113 deletions api/test/api/test_experiment_export.py

This file was deleted.

Loading
Loading