mirror of
https://github.com/sstent/AICyclingCoach.git
synced 2026-01-25 16:41:58 +00:00
36 lines
1006 B
Python
36 lines
1006 B
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from app.main import app
|
|
from app.database import get_db, Base
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
TEST_DATABASE_URL = "postgresql://postgres:postgres@localhost:5432/test_db"
|
|
|
|
@pytest.fixture(scope="session")
|
|
def test_engine():
|
|
engine = create_engine(TEST_DATABASE_URL)
|
|
Base.metadata.create_all(bind=engine)
|
|
yield engine
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
@pytest.fixture
|
|
def db_session(test_engine):
|
|
connection = test_engine.connect()
|
|
transaction = connection.begin()
|
|
session = sessionmaker(autocommit=False, autoflush=False, bind=connection)()
|
|
yield session
|
|
session.close()
|
|
transaction.rollback()
|
|
connection.close()
|
|
|
|
@pytest.fixture
|
|
def client(db_session):
|
|
def override_get_db():
|
|
try:
|
|
yield db_session
|
|
finally:
|
|
db_session.close()
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
return TestClient(app) |