Fix testing harness
This commit is contained in:
@@ -2,38 +2,31 @@ import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlmodel import Session, create_engine, SQLModel
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.main import app
|
||||
from app.model.models import UserDB, Card, GroupDB, AccessAuthorizationDB, Timetable, AaGroupLink
|
||||
from app.services.database import get_session
|
||||
|
||||
# Use in-memory SQLite for testing
|
||||
TEST_SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
|
||||
|
||||
engine = create_engine(TEST_SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
||||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
TEST_SQLALCHEMY_DATABASE_URL = "sqlite://"
|
||||
|
||||
engine = create_engine(TEST_SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session():
|
||||
"""Create a fresh database session for each test."""
|
||||
SQLModel.metadata.create_all(engine)
|
||||
session = TestingSessionLocal()
|
||||
try:
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
SQLModel.metadata.drop_all(engine)
|
||||
SQLModel.metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def client(db_session):
|
||||
"""Create a test client with a database session override."""
|
||||
def override_get_session():
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
pass
|
||||
yield db_session
|
||||
|
||||
app.dependency_overrides[get_session] = override_get_session
|
||||
with TestClient(app) as test_client:
|
||||
|
||||
Reference in New Issue
Block a user