put db engine construction inside handler to avoid import errors in test

This commit is contained in:
Daniel Roth 2026-06-09 15:04:12 +00:00
parent 236f33c25f
commit dcd5204b54
2 changed files with 11 additions and 7 deletions

View file

@ -15,9 +15,6 @@ from orchestration.audit_generator_orchestrator import AuditGeneratorOrchestrato
from orchestration.audit_generator_unit_of_work import AuditGeneratorUnitOfWork
from utilities.aws_lambda.subtask_handler import subtask_handler
_engine = make_engine(PostgresConfig.from_env(os.environ))
@subtask_handler()
def handler(body: dict[str, Any], context: Any) -> None:
trigger = AuditGeneratorTriggerRequest.model_validate(body)
@ -27,8 +24,10 @@ def handler(body: dict[str, Any], context: Any) -> None:
bucket = os.environ["S3_BUCKET_NAME"]
s3_client = S3Client(boto_s3_client=boto_s3, bucket=bucket)
engine = make_engine(PostgresConfig.from_env(os.environ))
def session_factory() -> Any:
return make_session(_engine)
return make_session(engine)
def uow_factory() -> AuditGeneratorUnitOfWork:
return AuditGeneratorUnitOfWork(session_factory)

View file

@ -11,9 +11,8 @@ from applications.audit_generator.handler import handler
_ENV = {
"DATABASE_URL": "postgresql+psycopg://user:pass@localhost/db",
"S3_BUCKET_NAME": "test-bucket",
# PostgresConfig.from_env also reads these individual vars; DATABASE_URL is
# used directly when constructing the engine in the handler module scope, so
# we patch make_engine instead of the env.
# Tests patch PostgresConfig and make_engine to avoid needing the individual
# POSTGRES_* vars that PostgresConfig.from_env would otherwise require.
}
_VALID_BODY: dict[str, Any] = {
@ -50,6 +49,8 @@ def test_handler_passes_hubspot_deal_id_from_body_to_orchestrator() -> None:
# Act
with patch("applications.audit_generator.handler.os.environ", _ENV), \
patch("applications.audit_generator.handler.PostgresConfig"), \
patch("applications.audit_generator.handler.make_engine"), \
patch("applications.audit_generator.handler.S3Client") as MockS3, \
patch("applications.audit_generator.handler.AuditGeneratorOrchestrator", return_value=mock_orch) as MockOrch:
MockS3.return_value = MagicMock()
@ -67,6 +68,8 @@ def test_handler_passes_bucket_from_env_to_s3_client() -> None:
# Act
with patch("applications.audit_generator.handler.os.environ", _ENV), \
patch("applications.audit_generator.handler.PostgresConfig"), \
patch("applications.audit_generator.handler.make_engine"), \
patch("applications.audit_generator.handler.S3Client") as MockS3, \
patch("applications.audit_generator.handler.AuditGeneratorOrchestrator", return_value=mock_orch):
_call(_VALID_BODY)
@ -86,6 +89,8 @@ def test_handler_returns_none_on_success() -> None:
# Act
with patch("applications.audit_generator.handler.os.environ", _ENV), \
patch("applications.audit_generator.handler.PostgresConfig"), \
patch("applications.audit_generator.handler.make_engine"), \
patch("applications.audit_generator.handler.S3Client"), \
patch("applications.audit_generator.handler.AuditGeneratorOrchestrator", return_value=mock_orch):
result = _call(_VALID_BODY)