61 lines
1.2 KiB
Python
61 lines
1.2 KiB
Python
# app/tests/conftest.py
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.pool import StaticPool
|
|
from sqlalchemy.orm import sessionmaker
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
from app.db.base import Base
|
|
from app.db.session import get_db
|
|
|
|
import os
|
|
os.environ["TESTING"] = "1"
|
|
|
|
# Use in-memory SQLite for tests
|
|
TEST_DATABASE_URL = "sqlite+pysqlite:///:memory:"
|
|
|
|
@pytest.fixture(scope="function")
|
|
def db_session():
|
|
engine = create_engine(
|
|
TEST_DATABASE_URL,
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
|
|
TestingSessionLocal = sessionmaker(
|
|
autocommit=False,
|
|
autoflush=False,
|
|
bind=engine,
|
|
)
|
|
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
db = TestingSessionLocal()
|
|
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def client(db_session):
|
|
"""
|
|
Overrides get_db dependency to use test database.
|
|
"""
|
|
|
|
def override_get_db():
|
|
try:
|
|
yield db_session
|
|
finally:
|
|
pass
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
with TestClient(app) as c:
|
|
yield c
|
|
|
|
app.dependency_overrides.clear() |