Eu dou mock no session do banco de dados, estou usando Pytest. Vai precisar de um banco de dados para teste, mas achei essa a melhor abordagem.
A fixture fica assim:
from sqlalchemy.orm import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
@pytest.fixture
def client(session):
def get_session_override():
return session
app.dependency_overrides[get_session] = get_session_override
with TestClient(app) as client:
yield client
app.dependency_overrides.clear()
@pytest.fixture
def session():
engine = create_engine(settings.DATABASE_URL_TEST)
Session = sessionmaker(bind=engine)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
session = Session()
yield session
session.close()
Base.metadata.drop_all(engine)