From 42c8cfb2cb8897b82f165c42defc7dba69051a32 Mon Sep 17 00:00:00 2001 From: Edwin Jose Date: Mon, 11 Aug 2025 15:45:06 -0400 Subject: [PATCH] fix: Improve session fixture cleanup and engine handling (#9352) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improve session fixture cleanup and engine handling Refactors the session fixture to use an in-memory SQLite engine with proper disposal and ensures tables are dropped and the engine is disposed of after tests. This enhances resource management and test isolation. * Update src/backend/tests/conftest.py --------- Co-authored-by: Ítalo Johnny --- src/backend/tests/conftest.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/backend/tests/conftest.py b/src/backend/tests/conftest.py index c156442b6..d461029ec 100644 --- a/src/backend/tests/conftest.py +++ b/src/backend/tests/conftest.py @@ -198,11 +198,18 @@ async def async_client() -> AsyncGenerator: @pytest.fixture(name="session") def session_fixture(): - engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session - SQLModel.metadata.drop_all(engine) # Add this line to clean up tables + engine = create_engine( + "sqlite+pysqlite:///:memory:", + connect_args={"check_same_thread": False}, + poolclass=StaticPool, + ) + try: + SQLModel.metadata.create_all(engine) + with Session(engine) as session: + yield session + finally: + SQLModel.metadata.drop_all(engine) + engine.dispose() @pytest.fixture