test(be): every dependency override goes through override_dep

22 test files set `app.dependency_overrides[...]` by hand and tore down with a blanket `.clear()`
in a `finally`, which wipes every override rather than the test's own and stops fixtures composing.
They now take the `override_dep` fixture (helpers get it passed in), whose teardown restores each
key's prior value; no clear() carried meaning between two phases of a test. A convention test fails
any test file that writes `dependency_overrides` directly.
This commit is contained in:
2026-09-19 17:26:07 +02:00
parent dc902d2c0e
commit 8759b5860d
23 changed files with 537 additions and 529 deletions
+28 -19
View File
@@ -231,61 +231,70 @@ def _seed(db):
return user
def _get(db, user, monkeypatch, path):
def _get(db, user, monkeypatch, override_dep, path):
monkeypatch.setattr(state, "_configured_cache", True)
app.dependency_overrides[get_db] = lambda: db
app.dependency_overrides[current_user] = lambda: user
try:
return TestClient(app).get(path)
finally:
app.dependency_overrides.clear()
override_dep(get_db, lambda: db)
override_dep(current_user, lambda: user)
return TestClient(app).get(path)
def _order(r) -> list[str]:
return [i["title"] for i in r.json()["items"]]
def test_collection_detail_defaults_to_release_year_ascending(db, monkeypatch):
def test_collection_detail_defaults_to_release_year_ascending(db, monkeypatch, override_dep):
"""The order the page had before it grew a control — a franchise reads in release order."""
user = _seed(db)
r = _get(db, user, monkeypatch, "/api/plex/collections/col1")
r = _get(db, user, monkeypatch, override_dep, "/api/plex/collections/col1")
assert r.status_code == 200
assert _order(r) == ["Zulu", "Mike", "Series", "Alpha"]
def test_collection_detail_honours_sort_and_direction(db, monkeypatch):
def test_collection_detail_honours_sort_and_direction(db, monkeypatch, override_dep):
user = _seed(db)
r = _get(db, user, monkeypatch, "/api/plex/collections/col1?sort=year&sort_dir=desc")
r = _get(
db, user, monkeypatch, override_dep, "/api/plex/collections/col1?sort=year&sort_dir=desc"
)
assert _order(r) == ["Alpha", "Series", "Mike", "Zulu"]
r = _get(db, user, monkeypatch, "/api/plex/collections/col1?sort=title&sort_dir=asc")
r = _get(
db, user, monkeypatch, override_dep, "/api/plex/collections/col1?sort=title&sort_dir=asc"
)
assert _order(r) == ["Alpha", "Mike", "Series", "Zulu"]
r = _get(db, user, monkeypatch, "/api/plex/collections/col1?sort=rating&sort_dir=desc")
r = _get(
db, user, monkeypatch, override_dep, "/api/plex/collections/col1?sort=rating&sort_dir=desc"
)
# The show has no rating, so it sinks — it is not "rated 0".
assert _order(r) == ["Zulu", "Mike", "Alpha", "Series"]
def test_lastwatched_ranks_a_shows_episodes_alongside_the_movies(db, monkeypatch):
def test_lastwatched_ranks_a_shows_episodes_alongside_the_movies(db, monkeypatch, override_dep):
"""The show leg needs the rollup's `resumed_at`; with only a status map this key would order
every show as never-watched."""
user = _seed(db)
r = _get(db, user, monkeypatch, "/api/plex/collections/col1?sort=lastwatched&sort_dir=desc")
r = _get(
db,
user,
monkeypatch,
override_dep,
"/api/plex/collections/col1?sort=lastwatched&sort_dir=desc",
)
# Series (episode resumed NOW) > Zulu (watched 2 days ago) > the two never-touched, title-asc.
assert _order(r) == ["Series", "Zulu", "Alpha", "Mike"]
def test_collections_list_resolves_titles_by_key(db, monkeypatch):
def test_collections_list_resolves_titles_by_key(db, monkeypatch, override_dep):
"""What a shared Plex link needs: the URL carries bare rating_keys, so the sidebar token and the
filter chip have nothing to show but `17045` until these are resolved."""
user = _seed(db)
r = _get(db, user, monkeypatch, "/api/plex/collections?keys=col1")
r = _get(db, user, monkeypatch, override_dep, "/api/plex/collections?keys=col1")
assert r.status_code == 200
assert [c["title"] for c in r.json()["collections"]] == ["Franchise"]
def test_an_empty_keys_param_asks_for_nothing(db, monkeypatch):
def test_an_empty_keys_param_asks_for_nothing(db, monkeypatch, override_dep):
"""`keys=` with nothing in it must not fall through to "every collection" — that would be a
200 KB answer to a question about zero ids."""
user = _seed(db)
r = _get(db, user, monkeypatch, "/api/plex/collections?keys=")
r = _get(db, user, monkeypatch, override_dep, "/api/plex/collections?keys=")
assert r.status_code == 200
assert r.json()["collections"] == []