Skip to content

tests: make the DB-free list hermetic - #129

Merged
gfrmin merged 1 commit into
masterfrom
tests/hermetic-db-free
Nov 12, 2026
Merged

gfrmin merged 1 commit into
masterfrom
tests/hermetic-db-free

Conversation

@gfrmin

@gfrmin gfrmin commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

The DB-free unit list (the unit checks (no database) step in tests.yml) passed in CI but failed on a developer machine, and the checks that failed changed from run to run. Each failure came from state on the machine, not from the code under test.

Root causes

  1. test_sentry_regressions: disHKcaltype/incHKcaltype "filters before ORDER BY/LIMIT" and the boardcomp/advbyrole timeout_s=25 checks. These sections stub webbsite.db.execute_query. watermarks.enigma_version imports execute_query from webbsite.db when it is called, so it gets the stub too. The stub's empty enigma.log is a readable data version. (market falls back to the frozen-archive watermarks.) So precomputed() saved the stubbed result under the default cache root, ~/.cache/webbsite/pc. On the next run with the same code fingerprint on the same day, the page was served from that file and issued no SQL, so the checks that inspect the SQL found none. The first run after a code change passed and later runs failed, which is why the results looked random. In CI the cache is always empty, so the checks always passed there.
    Fix: run() now runs the checks against a fresh temporary cache directory, and clears the 5-minute watermark memo before and after, so a stub left by another module does not decide whether this run caches.
  2. Host environment and .env reached the tests. The modules used os.environ.setdefault("DATABASE_URL", stub), so a DATABASE_URL set in the shell won, and the tests would talk to that database. That breaks, for example, the "real engine against a closed port" Sentry check. Several modules set nothing at all. webbsite.config also calls load_dotenv(), which walks up from the package and loads any .env it finds, including one from a parent checkout. That .env supplied DATABASE_URL, SECRET_KEY and others. Operator settings (overlay dir, site name, PostHog/Sentry keys) would change renders or send test events out.
    Fix: new tests/hermetic.py. Each DB-free module calls isolate() before it imports webbsite. isolate() assigns the stub URL, sets PYTHON_DOTENV_DISABLED=1, drops the operator settings webbsite.config reads, and points WEBBSITE_CACHE_DIR at one temporary directory for the session. DB-backed tests (*_pg.py, check_all_routes.py) are untouched.
  3. test_precompute_routes::test_every_sort_renders_from_one_compute errored 22 times when the list was run with -p no:logging (fixture 'caplog' not found). The "connection refused" lines around those errors were log output from other tests' simulated outages, not the cause. The test now collects ERROR records with its own handler on the root logger, so it works however pytest is invoked. A planted unstubbed module still fails it under -p no:logging.

No production code changed.

Verification

  • The full list from tests.yml, run 3 times in a row with the same ~/.cache state: 198 passed each time. Before the fix, every run after the first failed test_sentry_regressions::test_all.
  • Under hostile host state, all 198 still pass: WEBBSITE_CACHE_DIR set to the stale real cache, DATABASE_URL set to a local Postgres, operator branding and telemetry variables set, an overlay dir that does not exist, and -p no:logging.
  • python tests/test_sentry_regressions.py, run twice against the stale cache: all checks passed both times.
  • The files that were changed pass when run alone. The planted-failure self-proof loop is still red for each file, and the same files are green when run directly without the planted failure.
  • uv run ruff check .: all checks passed.

🤖 Generated with Claude Code

The CI unit list failed locally, differently from run to run, because the
tests read state from the machine they ran on.

- test_sentry_regressions stubs webbsite.db.execute_query, which also
  answers watermarks.enigma_version, so the stub's empty enigma.log is a
  readable data version and precomputed pages saved their stubbed results
  in the default cache, ~/.cache/webbsite/pc. The next run (same code, same
  day) served those files without a query, and the "filters before ORDER
  BY/LIMIT" (disHKcaltype, incHKcaltype) and "timeout_s=25" (boardcomp,
  advbyrole) checks saw no SQL. run() now uses a fresh cache directory and
  clears the watermark memo on the way in and out.
- Every DB-free module now calls tests/hermetic.isolate() before importing
  webbsite: the stub DATABASE_URL (assigned, not setdefault, so a shell's
  DATABASE_URL cannot point the tests at a real database), no .env loading
  (webbsite.config walks up from the package and found a parent checkout's
  .env), no operator settings (overlay, branding, Sentry/PostHog keys), and
  a temporary WEBBSITE_CACHE_DIR for the session.
- test_precompute_routes collected logged errors through caplog, which does
  not exist under `-p no:logging`: its 22 sort tests ERRORed when run that
  way. It now uses its own handler on the root logger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gfrmin
gfrmin merged commit ed5952d into master Nov 12, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant