Move fixtures to conftest.py

This commit is contained in:
linuskmr 2021-12-23 18:27:49 +01:00
parent 0de01ac270
commit b9996a9fe6

View file

@ -11,34 +11,6 @@ from src.main import app
from src.models.project import Project
@pytest.fixture(name="session")
def session_fixture():
"""Creates a mock session, that access an in-memory temporary database."""
engine = create_engine(
"sqlite://", # In memory database
connect_args={"check_same_thread": False}, # FastAPI's async functions may execute on different threads
poolclass=StaticPool # All threads should access shared memory
)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
yield session
@pytest.fixture(name="client")
def client_fixture(session: Session):
"""Creates a FastAPI TestClient with a mocked session dependency. This causes FastAPI to inject the fake session
into path operation functions, so that we have full control over the mocked database."""
def get_session_override():
return session
app.dependency_overrides[database.get_session] = get_session_override
client = TestClient(app)
yield client
app.dependency_overrides.clear()
def test_project_create(client: TestClient):
project = {"name": "Uni"}
response = client.post("/projects/", json=project)