test: cover the "preferences unavailable" degradation path - #344
Merged
Conversation
`_install_prefs_loader` promises it "never 500s a request over prefs: a DB
hiccup (or the table not yet migrated) falls back to None, and the read
helpers use their existing cookie/default path." Nothing tested that.
The last three uncovered spots in the repo turned out to be exactly that
one path, split across three files:
- main.py 147-148 except SQLAlchemyError -> request.state.prefs = None
- routes/card.py 105 _hist_currency cookie fallback when prefs is None
- routes/search.py 72-73 _display_prefs cookie fallback when prefs is None
They are unreachable in normal tests because the middleware always runs and
always succeeds, which is also why "the table not yet migrated" — an
instance upgraded before `alembic upgrade head` catches up — was an
untested claim rather than a verified one.
A fixture makes every Preferences lookup raise OperationalError, and the
tests assert /search, /card/{id} and / still render *and still honor the
visitor's cookies* rather than 500ing or silently reverting to defaults.
Verified the fixture actually bites: under it, coverage of those files
inverts (the prefs branches go uncovered, the fallbacks get hit).
Repo-wide coverage is back to 100% (0 missed lines).
Tests only — no behavior change.
Co-Authored-By: Claude Opus 5 (1M context) <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.
What I found
You asked why
request.state.prefswas None in tests. It isn't — I had the line numbers inverted when I reported that. The middleware runs and succeeds normally;card.py:104andsearch.py:71(the prefs-populated branches) are covered. What was uncovered were the fallbacks on the next lines.Re-reading them, the last three uncovered spots in the repo turned out to be a single coherent path split across three files:
main.pyexcept SQLAlchemyError→request.state.prefs = Noneroutes/card.py_hist_currencycookie fallback when prefs is Noneroutes/search.py_display_prefscookie fallback when prefs is NoneThey're unreachable in normal tests precisely because the middleware always runs and always succeeds.
_install_prefs_loaderpromises:Nothing tested that promise. And "the table not yet migrated" is a real scenario — an instance upgraded before
alembic upgrade headcatches up, which is exactly when you most want pages to render.What this adds
A fixture makes every
Preferenceslookup raiseOperationalError, then asserts/search,/card/{id}and/still render and still honor the visitor's cookies — not just "doesn't 500", but "doesn't silently revert to defaults either":/searchstill applies thescryme_search_filtercookie (Counterspell in, Grizzly Bears out)./card/{id}still resolvesscryme_hist_currency=gbpand renders£5.00./renders with no prefs cookies at all.I verified the fixture actually bites rather than passing vacuously: under it, coverage of those three files inverts —
main.pymisses 143-146 instead of 147-148,card.pymisses 104 instead of 105,search.pymisses 71 instead of 72-73. So the tests are genuinely driving the degradation path.Result
Repo-wide coverage is back to 100% (0 missed lines), 1179 tests. Tests only, no behavior change.
🤖 Generated with Claude Code