style: apply ruff format across backend + scripts (line-length 100)

Mechanical, whole-tree `ruff format` pass — no behaviour change. Recorded in
.git-blame-ignore-revs (next commit) so git blame skips it.
This commit is contained in:
2026-08-23 07:43:06 +02:00
parent 434a98d6ed
commit b1832afd18
217 changed files with 3149 additions and 1744 deletions
+176 -51
View File
@@ -6,6 +6,7 @@ aggregate, and the sync upsert idempotency — so the mechanical 1.x -> 2.0 conv
behaviour-equivalent: the exact same assertions must pass before and after the conversion. DB lane
(`db` fixture) — skipped without Postgres.
"""
from datetime import datetime, timezone
from fastapi.testclient import TestClient
@@ -30,35 +31,75 @@ def _seed(db):
show_lib = PlexLibrary(plex_key="2", title="Shows", kind="show", enabled=True)
db.add_all([movie_lib, show_lib])
db.flush()
m1 = PlexItem(rating_key="m1", library_id=movie_lib.id, kind="movie",
title="Alpha Movie", year=2001, added_at=_dt(1))
m2 = PlexItem(rating_key="m2", library_id=movie_lib.id, kind="movie",
title="Bravo Movie", year=2002, added_at=_dt(2))
show = PlexShow(rating_key="s1", library_id=show_lib.id, title="Charlie Show",
year=2003, added_at=_dt(3), child_count=1)
m1 = PlexItem(
rating_key="m1",
library_id=movie_lib.id,
kind="movie",
title="Alpha Movie",
year=2001,
added_at=_dt(1),
)
m2 = PlexItem(
rating_key="m2",
library_id=movie_lib.id,
kind="movie",
title="Bravo Movie",
year=2002,
added_at=_dt(2),
)
show = PlexShow(
rating_key="s1",
library_id=show_lib.id,
title="Charlie Show",
year=2003,
added_at=_dt(3),
child_count=1,
)
db.add_all([m1, m2, show])
db.flush()
season = PlexSeason(rating_key="se1", show_id=show.id, season_number=1)
db.add(season)
db.flush()
e1 = PlexItem(rating_key="e1", library_id=show_lib.id, kind="episode", title="Charlie Pilot",
show_id=show.id, season_id=season.id, season_number=1, episode_number=1, added_at=_dt(4))
e2 = PlexItem(rating_key="e2", library_id=show_lib.id, kind="episode", title="Charlie Finale",
show_id=show.id, season_id=season.id, season_number=1, episode_number=2, added_at=_dt(5))
e1 = PlexItem(
rating_key="e1",
library_id=show_lib.id,
kind="episode",
title="Charlie Pilot",
show_id=show.id,
season_id=season.id,
season_number=1,
episode_number=1,
added_at=_dt(4),
)
e2 = PlexItem(
rating_key="e2",
library_id=show_lib.id,
kind="episode",
title="Charlie Finale",
show_id=show.id,
season_id=season.id,
season_number=1,
episode_number=2,
added_at=_dt(5),
)
db.add_all([e1, e2])
user = User(email="plexbrowse@example.com")
db.add(user)
db.flush()
db.add_all([
PlexState(user_id=user.id, item_id=m1.id, status="watched"),
PlexState(user_id=user.id, item_id=e1.id, position_seconds=120),
])
db.add_all(
[
PlexState(user_id=user.id, item_id=m1.id, status="watched"),
PlexState(user_id=user.id, item_id=e1.id, position_seconds=120),
]
)
db.commit()
return {"user": user, "movie_lib": movie_lib, "show_lib": show_lib, "show": show}
def _request(db, user, monkeypatch, method, path, json=None):
monkeypatch.setattr(state, "_configured_cache", True) # the request gate 503s an unconfigured app
monkeypatch.setattr(
state, "_configured_cache", True
) # the request gate 503s an unconfigured app
app.dependency_overrides[get_db] = lambda: db
app.dependency_overrides[current_user] = lambda: user
try:
@@ -124,7 +165,10 @@ class TestUnifiedLibrary:
s = _seed(db)
body = _get(db, s["user"], monkeypatch, "/api/plex/library?limit=2&offset=0").json()
assert body["total"] == 3
assert [i["id"] for i in body["items"]] == ["s1", "m2"] # first page of the added-desc order
assert [i["id"] for i in body["items"]] == [
"s1",
"m2",
] # first page of the added-desc order
class TestRollup:
@@ -153,6 +197,7 @@ def _fake_paginate(by_type):
if progress is not None:
progress["complete"] = True # the fake returns the whole list — a complete listing
return list(by_type.get(typ, []))
return _p
@@ -172,9 +217,13 @@ class TestSyncUpsertIdempotency:
metas[0]["title"] = "First Renamed"
sync._sync_movies(db, object(), lib, {"movies": 0})
db.commit()
rows = db.execute(
select(PlexItem).where(PlexItem.library_id == lib.id, PlexItem.kind == "movie")
).scalars().all()
rows = (
db.execute(
select(PlexItem).where(PlexItem.library_id == lib.id, PlexItem.kind == "movie")
)
.scalars()
.all()
)
assert {r.rating_key for r in rows} == {"100", "101"} # no duplicate rows
assert next(r for r in rows if r.rating_key == "100").title == "First Renamed"
@@ -190,8 +239,15 @@ class TestSyncUpsertIdempotency:
{"ratingKey": "se1", "parentRatingKey": "s1", "title": "Season 1", "index": 1}
],
sync._TYPE_EPISODE: [
{"ratingKey": "e1", "title": "Ep One", "grandparentRatingKey": "s1",
"parentRatingKey": "se1", "parentIndex": 1, "index": 1, "addedAt": 1700000001}
{
"ratingKey": "e1",
"title": "Ep One",
"grandparentRatingKey": "s1",
"parentRatingKey": "se1",
"parentIndex": 1,
"index": 1,
"addedAt": 1700000001,
}
],
}
monkeypatch.setattr(sync, "_paginate", _fake_paginate(by_type))
@@ -216,15 +272,41 @@ class TestFacetsWithData:
show_lib = PlexLibrary(plex_key="2", title="Shows", kind="show", enabled=True)
db.add_all([movie_lib, show_lib])
db.flush()
db.add_all([
PlexItem(rating_key="m1", library_id=movie_lib.id, kind="movie", title="M1",
genres=["Comedy", "Drama"], content_rating="PG", year=2000, rating=7.5,
duration_s=6000),
PlexItem(rating_key="m2", library_id=movie_lib.id, kind="movie", title="M2",
genres=["Comedy"], content_rating="R", year=2010, rating=8.0, duration_s=7200),
PlexShow(rating_key="s1", library_id=show_lib.id, title="S1",
genres=["Drama"], content_rating="TV-14", year=2005, rating=9.0),
])
db.add_all(
[
PlexItem(
rating_key="m1",
library_id=movie_lib.id,
kind="movie",
title="M1",
genres=["Comedy", "Drama"],
content_rating="PG",
year=2000,
rating=7.5,
duration_s=6000,
),
PlexItem(
rating_key="m2",
library_id=movie_lib.id,
kind="movie",
title="M2",
genres=["Comedy"],
content_rating="R",
year=2010,
rating=8.0,
duration_s=7200,
),
PlexShow(
rating_key="s1",
library_id=show_lib.id,
title="S1",
genres=["Drama"],
content_rating="TV-14",
year=2005,
rating=9.0,
),
]
)
user = User(email="facetdata@example.com")
db.add(user)
db.commit()
@@ -232,7 +314,9 @@ class TestFacetsWithData:
body = _get(db, user, monkeypatch, "/api/plex/facets?scope=both").json()
assert {g["value"]: g["count"] for g in body["genres"]} == {"Comedy": 2, "Drama": 2}
assert {c["value"]: c["count"] for c in body["content_ratings"]} == {
"PG": 1, "R": 1, "TV-14": 1
"PG": 1,
"R": 1,
"TV-14": 1,
}
assert (body["year_min"], body["year_max"]) == (2000, 2010)
assert body["rating_max"] == 9.0
@@ -252,14 +336,22 @@ class TestPlaylistAndCollectionEndpoints:
lib = PlexLibrary(plex_key="1", title="Movies", kind="movie", enabled=True)
db.add(lib)
db.flush()
db.add_all([self._movie(db, lib.id, "m1", "First"), self._movie(db, lib.id, "m2", "Second")])
db.add_all(
[self._movie(db, lib.id, "m1", "First"), self._movie(db, lib.id, "m2", "Second")]
)
user = User(email="pl@example.com")
db.add(user)
db.commit()
# create (seeded with m1) -> item_count 1
created = _request(db, user, monkeypatch, "POST", "/api/plex/playlists",
{"title": "Faves", "item_rating_key": "m1"})
created = _request(
db,
user,
monkeypatch,
"POST",
"/api/plex/playlists",
{"title": "Faves", "item_rating_key": "m1"},
)
assert created.status_code == 200, created.text
pid = created.json()["id"]
assert created.json()["item_count"] == 1
@@ -269,11 +361,23 @@ class TestPlaylistAndCollectionEndpoints:
assert [(p["id"], p["item_count"]) for p in listed] == [(pid, 1)]
# add m2, then reorder to [m2, m1]
add = _request(db, user, monkeypatch, "POST", f"/api/plex/playlists/{pid}/items",
{"item_rating_key": "m2"})
add = _request(
db,
user,
monkeypatch,
"POST",
f"/api/plex/playlists/{pid}/items",
{"item_rating_key": "m2"},
)
assert add.status_code == 200, add.text
_request(db, user, monkeypatch, "PUT", f"/api/plex/playlists/{pid}/order",
{"item_rating_keys": ["m2", "m1"]})
_request(
db,
user,
monkeypatch,
"PUT",
f"/api/plex/playlists/{pid}/order",
{"item_rating_keys": ["m2", "m1"]},
)
detail = _get(db, user, monkeypatch, f"/api/plex/playlists/{pid}").json()
assert [it["id"] for it in detail["items"]] == ["m2", "m1"] # reorder applied, in order
@@ -301,29 +405,46 @@ class TestPlaylistAndCollectionEndpoints:
created = _request(db, user, monkeypatch, "POST", "/api/plex/playlists", {"title": "Bulk"})
pid = created.json()["id"]
added = _request(db, user, monkeypatch, "POST", f"/api/plex/playlists/{pid}/items/bulk",
{"item_rating_keys": ["m1", "m2", "m3"]})
added = _request(
db,
user,
monkeypatch,
"POST",
f"/api/plex/playlists/{pid}/items/bulk",
{"item_rating_keys": ["m1", "m2", "m3"]},
)
assert added.status_code == 200, added.text
assert added.json() == {"added": 3, "item_count": 3}
removed = _request(db, user, monkeypatch, "POST",
f"/api/plex/playlists/{pid}/items/remove-bulk",
{"item_rating_keys": ["m2"]})
removed = _request(
db,
user,
monkeypatch,
"POST",
f"/api/plex/playlists/{pid}/items/remove-bulk",
{"item_rating_keys": ["m2"]},
)
assert removed.status_code == 200, removed.text
assert removed.json() == {"removed": 1, "item_count": 2}
detail = _get(db, user, monkeypatch, f"/api/plex/playlists/{pid}").json()
assert [it["id"] for it in detail["items"]] == ["m1", "m3"] # input order preserved, m2 gone
assert [it["id"] for it in detail["items"]] == [
"m1",
"m3",
] # input order preserved, m2 gone
def test_collections_list(self, db, monkeypatch):
from app.models import PlexCollection
lib = PlexLibrary(plex_key="1", title="Movies", kind="movie", enabled=True)
db.add(lib)
db.flush()
db.add_all([
PlexCollection(rating_key="c1", library_id=lib.id, title="Marvel", child_count=12),
PlexCollection(rating_key="c2", library_id=lib.id, title="Pixar", child_count=5),
])
db.add_all(
[
PlexCollection(rating_key="c1", library_id=lib.id, title="Marvel", child_count=12),
PlexCollection(rating_key="c2", library_id=lib.id, title="Pixar", child_count=5),
]
)
user = User(email="col@example.com")
db.add(user)
db.commit()
@@ -346,8 +467,9 @@ class TestPlaylistAndCollectionEndpoints:
db.add(user)
db.commit()
watched = _request(db, user, monkeypatch, "POST", "/api/plex/item/m1/state",
{"status": "watched"})
watched = _request(
db, user, monkeypatch, "POST", "/api/plex/item/m1/state", {"status": "watched"}
)
assert watched.status_code == 200, watched.text
assert watched.json()["status"] == "watched"
row = db.execute(select(PlexState).where(PlexState.user_id == user.id)).scalars().one()
@@ -356,4 +478,7 @@ class TestPlaylistAndCollectionEndpoints:
# -> new deletes the state row
back = _request(db, user, monkeypatch, "POST", "/api/plex/item/m1/state", {"status": "new"})
assert back.status_code == 200, back.text
assert db.execute(select(PlexState).where(PlexState.user_id == user.id)).scalars().first() is None
assert (
db.execute(select(PlexState).where(PlexState.user_id == user.id)).scalars().first()
is None
)