"""Pytest fixtures for lambda integration tests.""" from collections.abc import Iterator from contextlib import contextmanager import logging from aws_testing_utils import lambda_handler from aws_testing_utils import s3_handler import pytest from sqlalchemy.orm import scoped_session from sqlalchemy.orm import Session from tests.src.database import get_session_factory logger = logging.getLogger(__name__) @contextmanager def session_scope() -> Iterator[Session]: """Provide a scoped transactional session for tests.""" factory: scoped_session[Session] = get_session_factory() session: Session = factory() try: session.expire_all() # Prevent stale reads yield session session.commit() except Exception: session.rollback() raise finally: session.close() factory.remove() @pytest.fixture def db_session() -> Iterator[Session]: """Pytest fixture to provide a database session scoped to a test function.""" with session_scope() as session: yield session @pytest.fixture def lambda_client() -> lambda_handler.LambdaHandler: return lambda_handler.LambdaHandler() @pytest.fixture def s3_client() -> s3_handler.S3Handler: return s3_handler.S3Handler()