Repository navigation
tests: make the DB-free list hermetic - #129
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The DB-free unit list (the
unit checks (no database)step intests.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
test_sentry_regressions: disHKcaltype/incHKcaltype "filters before ORDER BY/LIMIT" and the boardcomp/advbyroletimeout_s=25checks. These sections stubwebbsite.db.execute_query.watermarks.enigma_versionimportsexecute_queryfromwebbsite.dbwhen it is called, so it gets the stub too. The stub's emptyenigma.logis a readable data version. (marketfalls back to the frozen-archive watermarks.) Soprecomputed()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..envreached the tests. The modules usedos.environ.setdefault("DATABASE_URL", stub), so aDATABASE_URLset 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.configalso callsload_dotenv(), which walks up from the package and loads any.envit finds, including one from a parent checkout. That.envsuppliedDATABASE_URL,SECRET_KEYand 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 callsisolate()before it importswebbsite.isolate()assigns the stub URL, setsPYTHON_DOTENV_DISABLED=1, drops the operator settingswebbsite.configreads, and pointsWEBBSITE_CACHE_DIRat one temporary directory for the session. DB-backed tests (*_pg.py,check_all_routes.py) are untouched.test_precompute_routes::test_every_sort_renders_from_one_computeerrored 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
tests.yml, run 3 times in a row with the same~/.cachestate:198 passedeach time. Before the fix, every run after the first failedtest_sentry_regressions::test_all.WEBBSITE_CACHE_DIRset to the stale real cache,DATABASE_URLset 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 passedboth times.uv run ruff check .: all checks passed.🤖 Generated with Claude Code