mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
* chore(mypy): switch to strict mode with listed exceptions Replace the hand-picked flags with `strict = True`, turning off only the four flags that aren't clean yet (disallow_any_generics, warn_return_any, disallow_untyped_calls, disallow_untyped_decorators). Any other strict flag, including those future mypy releases add to the preset, is on by default. That turns on no_implicit_reexport, strict_equality and extra_checks: - Import each name from the module that defines it. About 240 test sites patched a dependency through the module that imported it (`scan_module.fs_rom_handler`); they now import the same object from its source, so every patch targets what it did before. Tests that replace a module attribute are unchanged. - handler.streaming.memory_cards re-exports store_memory_card_version explicitly, since endpoints call it there and tests patch it there. - mutagen gets implicit_reexport, as it re-exports without __all__. - Test comparisons build typed values (CoverResult, a typed expected dict, enum .value) and the CORS lookup casts for its identity check. - One ss_handler ** spread keeps a coded ignore: extra_checks wants its NotRequired dump_* keys spelled out, and the media dict never has them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G * chore(mypy): give every generic its type arguments Drop disallow_any_generics from the strict-mode exceptions and fix the 347 bare generics it reports, 230 of them in app code. - Bare `dict` becomes `dict[str, Any]`: update payloads, raw provider JSON and endpoint bodies map string keys to mixed values, so the Any is real and now written down. The OpenAPI document is unchanged. - Rom query filters take and return RomSelect; _music_facet_joins and _exclude_hidden are generic over the Select they're given, and _build_hash_selects returns id-only selects. - Redis clients are typed by what they return: the decoding caches are Redis[str], the RQ connection and binary cache Redis[bytes]. - The rest get explicit parameters: Row, Pattern, ColumnElement, QueryableAttribute, SpooledTemporaryFile, Task, Future and friends. Precise types reached callers that had been getting Any, which surfaced: - The LaunchBox cache writer is fed binary-cache pipelines. - The flashpoint and IGDB request helpers return raw JSON of varying shape, not always a dict. - The rom search endpoint returned dicts under a list[SearchRomSchema] annotation; it now validates them into that schema. - The audit filter's id subquery selects strings. - In tests the binary cache is the non-decoding fake shared with async_cache, so it is cast to Redis[bytes] rather than changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G * test(upload): assert cleanup against the patched upload directory The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G * chore(mypy): keep the row type through the ROM filter helpers filter_roms and its helpers also receive select(Rom.id) membership queries, so they now take and return the caller's Select type instead of narrowing to RomSelect. merged_ra_metadata mixes scalar and list values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
410 lines
14 KiB
Python
410 lines
14 KiB
Python
"""Smart collection membership as a composed query rather than a stored list.
|
|
|
|
A smart collection is a saved filter, so serving a page of it must apply its
|
|
criteria to the ROM query instead of assembling the whole membership list in
|
|
memory and writing it back on every read (see #4029).
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
from typing import Any
|
|
|
|
from handler.database import db_collection_handler, db_rom_handler, db_save_handler
|
|
from models.assets import Save
|
|
from models.collection import Collection, SmartCollection
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
|
|
def _add_rom(
|
|
platform: Platform,
|
|
name: str,
|
|
*,
|
|
cover: str = "",
|
|
manual_metadata: dict[str, Any] | None = None,
|
|
regions: list[str] | None = None,
|
|
) -> Rom:
|
|
slug = name.lower().replace(" ", "_")
|
|
return db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=platform.id,
|
|
name=name,
|
|
slug=slug,
|
|
fs_name=f"{slug}.zip",
|
|
fs_name_no_tags=slug,
|
|
fs_name_no_ext=slug,
|
|
fs_extension="zip",
|
|
fs_path=f"{platform.slug}/roms",
|
|
manual_metadata=manual_metadata or {},
|
|
regions=regions or [],
|
|
path_cover_s=cover,
|
|
path_cover_l=cover,
|
|
)
|
|
)
|
|
|
|
|
|
def _add_smart_collection(
|
|
user: User,
|
|
criteria: dict[str, Any],
|
|
*,
|
|
name: str = "Smart",
|
|
is_public: bool = False,
|
|
) -> SmartCollection:
|
|
return db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name=name,
|
|
description="",
|
|
user_id=user.id,
|
|
is_public=is_public,
|
|
filter_criteria=criteria,
|
|
)
|
|
)
|
|
|
|
|
|
def _favorite(user: User, roms: Sequence[Rom]) -> Collection:
|
|
collection = db_collection_handler.add_collection(
|
|
Collection(
|
|
name=f"Favorites {user.id}",
|
|
description="",
|
|
user_id=user.id,
|
|
is_favorite=True,
|
|
)
|
|
)
|
|
db_collection_handler.add_roms_to_collection(
|
|
collection.id, [rom.id for rom in roms]
|
|
)
|
|
return collection
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_applies_criteria(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
matching = [
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]}),
|
|
_add_rom(platform, "Rally Two", manual_metadata={"genres": ["Racing"]}),
|
|
]
|
|
_add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {rom.id for rom in matching}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_narrows_with_the_gallery_filters(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
keeper = _add_rom(
|
|
platform, "Rally Two", manual_metadata={"genres": ["Racing"]}, regions=["USA"]
|
|
)
|
|
_add_rom(
|
|
platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]}, regions=["USA"]
|
|
)
|
|
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id,
|
|
regions=["USA"],
|
|
user_id=admin_user.id,
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {keeper.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_keeps_both_search_terms(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# The collection's own search term and the gallery's are separate fulltext
|
|
# clauses in one statement; they must not share a bind parameter name.
|
|
match_both = _add_rom(platform, "Rally Champion")
|
|
_add_rom(platform, "Rally Junior")
|
|
_add_rom(platform, "Champion Cup")
|
|
|
|
smart_collection = _add_smart_collection(admin_user, {"search_term": "rally"})
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id,
|
|
search_term="champion",
|
|
user_id=admin_user.id,
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {match_both.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_resolves_per_viewer(
|
|
platform: Platform, admin_user: User, editor_user: User
|
|
):
|
|
# A public smart collection built on a per-user filter must answer for the
|
|
# viewer, not for whoever opened it last.
|
|
admin_pick = _add_rom(platform, "Admin Pick")
|
|
editor_pick = _add_rom(platform, "Editor Pick")
|
|
_favorite(admin_user, [admin_pick])
|
|
_favorite(editor_user, [editor_pick])
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"favorite": True}, is_public=True
|
|
)
|
|
|
|
admin_roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
editor_roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=editor_user.id
|
|
)
|
|
|
|
assert {rom.id for rom in admin_roms} == {admin_pick.id}
|
|
assert {rom.id for rom in editor_roms} == {editor_pick.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_scoped_to_a_regular_collection(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
inside = _add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
_add_rom(platform, "Rally Two", manual_metadata={"genres": ["Racing"]})
|
|
|
|
collection = db_collection_handler.add_collection(
|
|
Collection(name="Shelf", description="", user_id=admin_user.id)
|
|
)
|
|
db_collection_handler.add_roms_to_collection(collection.id, [inside.id])
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"genres": ["Racing"], "collection_id": collection.id}
|
|
)
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {inside.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_with_joined_criteria(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# `playable` joins platforms and `has_soundtrack` is a correlated column
|
|
# property; both have to survive being applied to a bare-column subquery.
|
|
rom = _add_rom(platform, "Rally One")
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"playable": False, "has_soundtrack": False, "missing": False}
|
|
)
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
|
|
assert {result.id for result in roms} == {rom.id}
|
|
|
|
|
|
def test_filter_roms_by_unknown_smart_collection_returns_nothing(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
_add_rom(platform, "Rally One")
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=123456, user_id=admin_user.id
|
|
)
|
|
|
|
assert list(roms) == []
|
|
|
|
|
|
def test_unusable_criteria_match_nothing_rather_than_everything(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# `filter_criteria` is stored unvalidated, and honouring a row only in part
|
|
# would drop the constraint that makes it a narrower list than the library.
|
|
_add_rom(platform, "Rally One")
|
|
_add_rom(platform, "Rally Two")
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"platform_ids": [platform.id, "not-an-id"]}
|
|
)
|
|
|
|
members = db_collection_handler.get_smart_collection_members(
|
|
smart_collection, user_id=admin_user.id
|
|
)
|
|
|
|
assert list(members) == []
|
|
|
|
|
|
def test_refresh_smart_collection_updates_the_cached_columns(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# The cached columns back the collections list and the ROM detail page.
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
assert smart_collection.rom_count == 0
|
|
|
|
roms = [
|
|
_add_rom(
|
|
platform,
|
|
f"Rally {index}",
|
|
cover=f"cover_{index}.png",
|
|
manual_metadata={"genres": ["Racing"]},
|
|
)
|
|
for index in range(3)
|
|
]
|
|
|
|
refreshed = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
|
|
assert refreshed is not None
|
|
assert refreshed.rom_count == 3
|
|
assert set(refreshed.rom_ids) == {rom.id for rom in roms}
|
|
assert len(refreshed.path_covers_small) == 3
|
|
assert len(refreshed.path_covers_large) == 3
|
|
|
|
|
|
def test_refresh_smart_collection_is_a_noop_when_nothing_moved(
|
|
platform: Platform, admin_user: User, mocker
|
|
):
|
|
# Rewriting an unchanged collection would bump `updated_at`, the `?ts=`
|
|
# cover cache-buster that clients also sync on.
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
_add_rom(
|
|
platform,
|
|
"Rally One",
|
|
cover="cover_0.png",
|
|
manual_metadata={"genres": ["Racing"]},
|
|
)
|
|
first = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
assert first is not None
|
|
assert len(first.path_covers_small) == 1
|
|
|
|
update = mocker.spy(db_collection_handler, "update_smart_collection")
|
|
second = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
|
|
assert update.call_count == 0
|
|
assert second is not None
|
|
assert second.path_covers_small == first.path_covers_small
|
|
|
|
|
|
def test_refresh_smart_collections_covers_every_collection(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
racing = _add_smart_collection(admin_user, {"genres": ["Racing"]}, name="Racing")
|
|
puzzle = _add_smart_collection(admin_user, {"genres": ["Puzzle"]}, name="Puzzle")
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
_add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
_add_rom(platform, "Puzzler Two", manual_metadata={"genres": ["Puzzle"]})
|
|
|
|
db_collection_handler.refresh_smart_collections()
|
|
|
|
refreshed_racing = db_collection_handler.get_smart_collection(racing.id)
|
|
refreshed_puzzle = db_collection_handler.get_smart_collection(puzzle.id)
|
|
assert refreshed_racing is not None and refreshed_racing.rom_count == 1
|
|
assert refreshed_puzzle is not None and refreshed_puzzle.rom_count == 2
|
|
|
|
|
|
def test_refresh_for_roms_updates_the_collections_the_rom_moved_between(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
racing = _add_smart_collection(admin_user, {"genres": ["Racing"]}, name="Racing")
|
|
puzzle = _add_smart_collection(admin_user, {"genres": ["Puzzle"]}, name="Puzzle")
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
mover = _add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
db_collection_handler.refresh_smart_collections()
|
|
|
|
db_rom_handler.update_rom(mover.id, {"manual_metadata": {"genres": ["Racing"]}})
|
|
db_collection_handler.refresh_smart_collections_for_roms([mover.id])
|
|
|
|
racing_after = db_collection_handler.get_smart_collection(racing.id)
|
|
puzzle_after = db_collection_handler.get_smart_collection(puzzle.id)
|
|
assert racing_after is not None and racing_after.rom_count == 2
|
|
assert puzzle_after is not None and puzzle_after.rom_count == 0
|
|
|
|
|
|
def test_refresh_for_roms_leaves_untouched_collections_alone(
|
|
platform: Platform, admin_user: User, mocker
|
|
):
|
|
# Recounting every collection for one edited ROM would scan the library
|
|
# once per collection, which is the cost this issue is about.
|
|
_add_smart_collection(admin_user, {"genres": ["Racing"]}, name="Racing")
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
db_collection_handler.refresh_smart_collections()
|
|
bystander = _add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
|
|
refresh = mocker.spy(db_collection_handler, "refresh_smart_collection")
|
|
db_collection_handler.refresh_smart_collections_for_roms([bystander.id])
|
|
|
|
assert refresh.call_count == 0
|
|
|
|
|
|
def _add_save(rom: Rom, user: User, name: str = "save.sav") -> Save:
|
|
return db_save_handler.add_save(
|
|
Save(
|
|
rom_id=rom.id,
|
|
user_id=user.id,
|
|
file_name=name,
|
|
file_name_no_tags=name,
|
|
file_name_no_ext=name,
|
|
file_extension="sav",
|
|
emulator="test_emulator",
|
|
file_path="test/saves",
|
|
file_size_bytes=1.0,
|
|
)
|
|
)
|
|
|
|
|
|
def test_refresh_for_roms_follows_per_user_state(platform: Platform, admin_user: User):
|
|
# A save is not a ROM edit, but it moves anything filtering on `has_saves`.
|
|
collection = _add_smart_collection(admin_user, {"has_saves": True})
|
|
rom = _add_rom(platform, "Rally One")
|
|
db_collection_handler.refresh_smart_collections()
|
|
seeded = db_collection_handler.get_smart_collection(collection.id)
|
|
assert seeded is not None and seeded.rom_count == 0
|
|
|
|
_add_save(rom, admin_user)
|
|
db_collection_handler.refresh_smart_collections_for_roms(
|
|
[rom.id], membership_only=True
|
|
)
|
|
|
|
after = db_collection_handler.get_smart_collection(collection.id)
|
|
assert after is not None
|
|
assert after.rom_ids == [rom.id]
|
|
|
|
|
|
def test_refresh_for_roms_membership_only_skips_a_member_that_stayed(
|
|
platform: Platform, admin_user: User, mocker
|
|
):
|
|
# Autosaves land constantly. Once the ROM is already a member, another save
|
|
# changes nothing about the collection, so it must not force a recount.
|
|
_add_smart_collection(admin_user, {"has_saves": True})
|
|
rom = _add_rom(platform, "Rally One")
|
|
_add_save(rom, admin_user, "first.sav")
|
|
db_collection_handler.refresh_smart_collections()
|
|
|
|
_add_save(rom, admin_user, "second.sav")
|
|
refresh = mocker.spy(db_collection_handler, "refresh_smart_collection")
|
|
db_collection_handler.refresh_smart_collections_for_roms(
|
|
[rom.id], membership_only=True
|
|
)
|
|
|
|
assert refresh.call_count == 0
|
|
|
|
|
|
def test_cached_membership_belongs_to_the_owner(
|
|
platform: Platform, admin_user: User, editor_user: User
|
|
):
|
|
# The cached columns live on the shared row, so they must describe the
|
|
# owner's view of a per-user filter rather than the last viewer's.
|
|
admin_pick = _add_rom(platform, "Admin Pick")
|
|
editor_pick = _add_rom(platform, "Editor Pick")
|
|
_favorite(admin_user, [admin_pick])
|
|
_favorite(editor_user, [editor_pick])
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"favorite": True}, is_public=True
|
|
)
|
|
|
|
db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=editor_user.id
|
|
)
|
|
refreshed = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
|
|
assert refreshed is not None
|
|
assert set(refreshed.rom_ids) == {admin_pick.id}
|