mirror of
https://github.com/sstent/FitTrack_ReportGenerator.git
synced 2025-12-06 08:01:40 +00:00
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from api.main import app
|
|
from src.db.session import get_db, Base
|
|
|
|
# Use an in-memory SQLite database for testing
|
|
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
|
|
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
|
)
|
|
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def session_db():
|
|
Base.metadata.create_all(bind=engine)
|
|
yield
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
@pytest.fixture(name="db_session")
|
|
def db_session_fixture(session_db):
|
|
db = TestingSessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@pytest.fixture(name="client")
|
|
def client_fixture(db_session):
|
|
def override_get_db():
|
|
try:
|
|
yield db_session
|
|
finally:
|
|
db_session.close()
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
with TestClient(app) as client:
|
|
yield client
|
|
app.dependency_overrides.clear() |