procurement-ingest/conftest.py
Adam Moussa f5c09757f3
feat(lambda): report unhandled Lambda errors to Sentry (PLAT-138) (#203)
* feat(lambda): report unhandled Lambda errors to Sentry

* fix(lambda): strip Sentry stack-frame locals

* style(tests): wrap long lines in sentry_init tests
2026-08-29 20:02:54 +00:00

115 lines
4.5 KiB
Python

"""Repo-root pytest configuration -- the ONE loader of session invariants.
pytest's rootdir is pinned by pytest.ini at the repo root, so THIS conftest
loads for EVERY invocation, including a standalone
``pytest lambdas/po/email_processor/tests`` run: rootdir conftests load at
session start, before any collection import. ``tests/conftest.py`` is NOT an
ancestor of the pipeline test roots, which is exactly why it cannot carry the
session invariants below.
The Lambda handlers create boto3 clients lazily on first use, but the moto
stubber hook must still be registered before any client is ever constructed, so
this conftest sets the dummy AWS env and imports moto BEFORE anything loads a
handler.
"""
import sys
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parent
# The handlers build boto3 clients on first call; give boto3 a region and dummy
# credentials so client construction works offline / under CI regardless of the
# ambient environment.
import os # noqa: E402
os.environ.setdefault("AWS_DEFAULT_REGION", "us-east-1")
os.environ.setdefault("AWS_ACCESS_KEY_ID", "testing")
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "testing")
os.environ.setdefault("AWS_SESSION_TOKEN", "testing")
# Handlers import sentry_init at module load. Drop a developer-shell DSN so
# pytest never talks to Sentry (init_sentry no-ops when unset).
os.environ.pop("SENTRY_DSN", None)
# Imported for its side effect and DELIBERATELY BEFORE the handler modules are
# loaded below: moto registers its botocore stubber hook into botocore's
# BUILTIN_HANDLERS at import time, and boto3 sessions only pick the hook up if
# they are created AFTER that registration. This conftest chain is imported at
# pytest session start -- before tests/test_po_merge.py gets a chance to import
# moto -- so without this import the handler's module-level boto3 clients would
# be created un-stubbable and the moto-backed suites would hit real AWS.
import moto # noqa: F401,E402
# Put the repo root on sys.path so ``tests.support`` resolves as a PEP-420
# namespace package from every invocation directory.
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
# The single loader lives in tests/support/loader.py; the root conftest exposes
# it (constraint 1). Re-exported rather than reimplemented -- `from conftest
# import ...` is unsafe with three conftest.py files across the roots.
from tests.support.loader import load_lambda_module # noqa: E402,F401
@pytest.fixture(scope="session")
def po_handler():
"""The PO email processor handler module."""
return load_lambda_module("po", "email_processor/handler")
@pytest.fixture(scope="session")
def wo_handler():
"""The WO email processor handler module."""
return load_lambda_module("wo", "email_processor/handler")
@pytest.fixture(scope="session")
def po_persistence(po_handler):
"""The PO persistence sibling (owns PO_TABLE + save_* after Phase 5).
load_lambda_module registers each sibling under ``<handler>__<sibling>`` in
sys.modules (only the bare-name binding is restored afterwards), so the
persistence module stays reachable here by its unique name. test_po_merge.py
keys its moto table on ``po_persistence.PO_TABLE`` rather than a literal.
"""
return sys.modules["po_email_processor_handler__persistence"]
@pytest.fixture(scope="session")
def po_enrichment(po_handler):
"""The PO enrichment sibling (owns pad_zip after Phase 5).
pad_zip is PURE and NOT re-exported by handler (handler's body never calls
it -- it runs inside enrich_parsed), so test_pad_zip.py dereferences it on
the owning module instead of po_handler.
"""
return sys.modules["po_email_processor_handler__enrichment"]
@pytest.fixture(scope="session")
def wo_persistence(wo_handler):
"""The WO persistence sibling (owns save_work_order / save_event).
Reachable by its unique sys.modules name for test_wo_merge symmetry with
po_persistence.
"""
return sys.modules["wo_email_processor_handler__persistence"]
@pytest.fixture(params=["po_handler", "wo_handler"])
def email_handler(request):
"""Parametrized fixture yielding each email processor handler module."""
return request.getfixturevalue(request.param)
@pytest.fixture
def ses_auth():
"""The single-sourced ses_auth module (lambdas/shared/, Phase 3).
Previously parameterized over the two per-pipeline copies to prove they
stayed byte-identical; now there is exactly one copy, so this loads it
once -- halving the test_ses_auth run.
"""
return load_lambda_module("shared", "ses_auth")