Full-suite pytest collision: RLS tests incompatible with function-scoped test_db fixture #145
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
UPDATE (2026-04-24 post-mortem)
The original framing of this issue focused on the
test_rls_isolation.pyfixture collision. That's real but secondary. The primary bug was much more dangerous and has been fixed in commitdab740d.What actually happened
Running
pytest tests/inside theresolutionflow_backendcontainer at Phase 9 Task 14 silently DROPPED THE DEV DATABASE SCHEMA. The backend crashed on startup withrelation "users" does not existand docker never restarted it — the dev environment was unusable until a manual container restart. Root cause was two layered bugs:Bug 1 —
conftest.TEST_DATABASE_URLpreferredDATABASE_URL:Inside the backend container,
DATABASE_URLpoints at the dev DB. So pytest treated the dev DB as the test DB. Thetest_dbfixture'sDROP SCHEMA public CASCADEthen wiped it.Bug 2 —
get_admin_dbwas not overridden inapp.dependency_overrides.conftest.pyoverrodeget_dbbut notget_admin_db. Endpoints usingget_admin_db(register, admin routes, service accounts) bypassed the test session and hit the real admin DB. Before Bug 1 was fixed, both engines happened to point at the same dev DB, so this worked by accident.Fix (shipped in
dab740d)conftest.pynow honors ONLYDATABASE_TEST_URL(or the localhost fallback).DATABASE_URLis never consulted for the test DB.TEST_DATABASE_URL's database name doesn't contain "test":conftest.pynow also overridesget_admin_dbto yield the same test session (RLS isn't enabled in the create_all-managed test schema, so sharing is safe).docker-compose.dev.ymlnow setsDATABASE_TEST_URL=postgresql+asyncpg://postgres:postgres@db:5432/resolutionflow_testso pytest in the container works out of the box (requiresCREATE DATABASE resolutionflow_test;once — the DB has been created).What remains open in this issue
The original full-suite
pytest tests/collision between function-scopedtest_dband module-scoped_ensure_rls_schemaintest_rls_isolation.pyis still unresolved. With the DB-isolation fix in place, it is no longer catastrophic —pytest tests/can no longer nuke the dev DB — but it still produces noisy failure reports when run end-to-end (the RLS tests error on role/policy mismatch against acreate_all-only schema, and cross-module state can leak).Options from the original issue still apply:
test_rls_isolation.pyfrom the default collection; run it as a separate CI step.conftest.test_dbto usealembic upgrade headinstead ofBase.metadata.create_all.Keeping this issue open for whoever takes that on. Title unchanged; severity is now lower.
Closing — both layered bugs from the post-mortem are fixed in main, and the remaining RLS-collision concern was resolved by Option 1 from this issue.
dab740d— DATABASE_TEST_URL only (DATABASE_URL no longer consulted), module-load assertion that the DB name must containtest,get_admin_dboverride added.b14a16a— RLS tests gated behindRUN_RLS_TESTS=1and auto-deselected from the default collection. This is Option 1 from the issue.Full suite is now a valid regression gate.