From 55ba9b62d6dbc535afa75d3648d23eebbf6ce1ea Mon Sep 17 00:00:00 2001 From: Alex Blank Date: Mon, 11 May 2026 21:25:35 +0200 Subject: [PATCH] fix: improve test isolation and configuration - Use in-memory SQLite for tests to prevent conflicts - Add try/finally for robust test cleanup - Make database URL configurable via env var - Make CORS origins configurable via env var - Make SQL echo configurable via env var --- backend/app/database.py | 5 +++-- backend/app/main.py | 3 ++- backend/tests/conftest.py | 23 ++++++++++++----------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/backend/app/database.py b/backend/app/database.py index bbd3772..4695d8b 100644 --- a/backend/app/database.py +++ b/backend/app/database.py @@ -1,9 +1,10 @@ +import os from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy.orm import declarative_base -DATABASE_URL = "sqlite+aiosqlite:///./backup_tool.db" +DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite+aiosqlite:///./backup_tool.db") -engine = create_async_engine(DATABASE_URL, echo=True) +engine = create_async_engine(DATABASE_URL, echo=os.environ.get("SQL_ECHO", "false").lower() == "true") AsyncSessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) Base = declarative_base() diff --git a/backend/app/main.py b/backend/app/main.py index dd449f2..2db80ae 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,3 +1,4 @@ +import os from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager @@ -18,7 +19,7 @@ app = FastAPI( app.add_middleware( CORSMiddleware, - allow_origins=["http://localhost:3000"], + allow_origins=os.environ.get("CORS_ORIGINS", "http://localhost:3000").split(","), allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 73ff134..8551487 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -5,21 +5,22 @@ from app.database import Base, get_db from app.main import app from httpx import AsyncClient -TEST_DATABASE_URL = "sqlite+aiosqlite:///./test.db" +TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:" @pytest_asyncio.fixture async def db(): engine = create_async_engine(TEST_DATABASE_URL, echo=False) - async with engine.begin() as conn: - await conn.run_sync(Base.metadata.create_all) - - async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) - async with async_session() as session: - yield session - - async with engine.begin() as conn: - await conn.run_sync(Base.metadata.drop_all) - await engine.dispose() + try: + async with engine.begin() as conn: + await conn.run_sync(Base.metadata.create_all) + + async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) + async with async_session() as session: + yield session + finally: + async with engine.begin() as conn: + await conn.run_sync(Base.metadata.drop_all) + await engine.dispose() @pytest_asyncio.fixture async def client(db):