mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-08-19 16:44:28 +00:00
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
|
from typing import Generator
|
||
|
|
||
|
import pytest
|
||
|
from fastapi.testclient import TestClient
|
||
|
from sqlalchemy import orm
|
||
|
|
||
|
from app.database import Base
|
||
|
from app.database import engine
|
||
|
from app.database import get_db
|
||
|
from app.main import app
|
||
|
|
||
|
_Session = orm.sessionmaker(bind=engine, autocommit=False, autoflush=False)
|
||
|
|
||
|
|
||
|
def _get_db_for_testing() -> Generator[orm.Session, None, None]:
|
||
|
session = _Session()
|
||
|
try:
|
||
|
yield session
|
||
|
finally:
|
||
|
session.close()
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def db() -> Generator:
|
||
|
Base.metadata.create_all(bind=engine)
|
||
|
yield orm.scoped_session(orm.sessionmaker(bind=engine))
|
||
|
try:
|
||
|
Base.metadata.drop_all(bind=engine)
|
||
|
except Exception:
|
||
|
# XXX: for some reason, the teardown occasionally fails because of this
|
||
|
pass
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def exclude_fastapi_middleware():
|
||
|
"""Workaround for https://github.com/encode/starlette/issues/472"""
|
||
|
user_middleware = app.user_middleware.copy()
|
||
|
app.user_middleware = []
|
||
|
app.middleware_stack = app.build_middleware_stack()
|
||
|
yield
|
||
|
app.user_middleware = user_middleware
|
||
|
app.middleware_stack = app.build_middleware_stack()
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def client(db, exclude_fastapi_middleware) -> Generator:
|
||
|
app.dependency_overrides[get_db] = _get_db_for_testing
|
||
|
with TestClient(app) as c:
|
||
|
yield c
|