fix: Improve session fixture cleanup and engine handling (#9352)

* 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 <italojohnnydosanjos@gmail.com>
This commit is contained in:
Edwin Jose 2025-08-11 15:45:06 -04:00 committed by GitHub
commit 42c8cfb2cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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