Files
romm/backend/conftest.py
Georges-Antoine Assi 503c9b290b test(db): give worktree runs their own temporary test database (#5074)
* test(db): give worktree runs their own temporary test database

Checkouts share romm_test, so a worktree on another branch can migrate it
to a revision the current branch lacks, and every test then errors in
setup with "Can't locate revision". ROMM_TEST_DB_TAG gives a run its own
romm_test_<tag> databases (one per xdist worker) that are dropped when
the session ends. AGENTS.md tells worktree sessions to use it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(db): address Greptile review on temporary test databases

- Prefix tagged databases with tmp_ so they can never share a name with
  an untagged xdist worker database that teardown would then drop.
- Normalize the tag to [A-Za-z0-9_] so tags like my-worktree work.
- Drop the temporary database even when the migration fails.
- Shorten the new comment to two lines.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 11:28:24 -04:00

23 lines
1.2 KiB
Python

import os
import re
# Tests must never inherit DB_NAME from the ambient environment (e.g. a
# sourced .env pointing at a real dev/prod database) -- the autouse
# `clear_database` fixture in tests/conftest.py deletes every row in that
# database on every test. Always pin to a dedicated test database, made
# unique per pytest-xdist worker so parallel workers can't wipe rows another
# worker is mid-test with. This must run before any application module
# (config / database handlers) is imported, so the engine built at import
# time binds to the test name. As the rootdir conftest, this file is
# imported before `tests/conftest.py` (which imports those modules).
#
# The Redis cache needs no equivalent handling: under pytest it is an in-process
# FakeRedis, so each worker process is already isolated.
#
# ROMM_TEST_DB_TAG runs use temporary databases that tests/conftest.py drops at
# session end; the `tmp_` prefix keeps them apart from the untagged worker names.
_tag = re.sub(r"[^A-Za-z0-9_]", "_", os.environ.get("ROMM_TEST_DB_TAG", ""))
_base = f"romm_test_tmp_{_tag}" if _tag else "romm_test"
_xdist_worker = os.environ.get("PYTEST_XDIST_WORKER")
os.environ["DB_NAME"] = f"{_base}_{_xdist_worker}" if _xdist_worker else _base