test(db): harden the DB lane per code-review (R6 S3)

/code-review high on S3 — 3 low findings, 2 fixed, 1 no-change:
- _db_reachable() now probes via a throwaway engine with connect_timeout=3, so a
  slow/firewalled DATABASE_URL can't hang the whole suite at import (the app engine
  has no connect timeout).
- test_plex_facets sets the "configured" gate flag via monkeypatch (auto-restored)
  instead of poking state._configured_cache in a manual finally — no leak risk, and
  the now-unnecessary mark_configured DB write is dropped.
No change: test_plex_jsonb_normalize copying migration 0060's SQL is deliberate —
migrations must stay self-contained (no app imports) and a shipped migration is
immutable, so there's no real drift to guard against.

Gate: ruff clean; DB lane 176 passed; pure-logic 172 passed / 4 skipped (fast).
This commit is contained in:
2026-07-26 15:09:25 +02:00
parent 39f726a5e6
commit 921c2bf5a4
2 changed files with 13 additions and 6 deletions
+9 -2
View File
@@ -15,19 +15,26 @@ tests that COMMIT (the whole point of this lane — transaction-ownership behavi
import pytest
from alembic import command
from alembic.config import Config
from sqlalchemy import text
from sqlalchemy import create_engine, text
import app.models # noqa: F401 — register every model on Base.metadata
from app.config import settings
from app.db import Base, SessionLocal, engine
def _db_reachable() -> bool:
# A throwaway engine with a bounded connect_timeout so a slow/firewalled DATABASE_URL can't
# hang the whole suite at import (the app engine has no connect timeout). The compose lane
# gates on `service_healthy`, but this keeps a manual run honest too.
probe = create_engine(settings.database_url, connect_args={"connect_timeout": 3})
try:
with engine.connect() as conn:
with probe.connect() as conn:
conn.execute(text("SELECT 1"))
return True
except Exception:
return False
finally:
probe.dispose()
_DB_REACHABLE = _db_reachable()