mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +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>
167 lines
5.5 KiB
Python
167 lines
5.5 KiB
Python
"""Sorting and range-filtering the gallery by HowLongToBeat main-story time.
|
|
|
|
`generated_hltb_main_story` (migration 0128) materializes the seconds buried in
|
|
the `hltb_metadata` blob so the length sort reads a `roms` column and the range
|
|
filter composes into SQL. A rom with no HowLongToBeat time derives NULL, which
|
|
is what keeps it out of a filtered result. The sort's query shape is pinned
|
|
with the other metadata sorts in `test_roms_metadata_sort.py`.
|
|
"""
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from handler.database import db_collection_handler, db_rom_handler
|
|
from models.collection import SmartCollection
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
SHORT_SECONDS = 2 * 3600
|
|
MEDIUM_SECONDS = 10 * 3600
|
|
LONG_SECONDS = 40 * 3600
|
|
|
|
|
|
def _make_rom(platform: Platform, fs_name: str, **metadata) -> Rom:
|
|
rom = db_rom_handler.add_rom(
|
|
Rom(
|
|
platform_id=platform.id,
|
|
name=fs_name,
|
|
slug=fs_name,
|
|
fs_name=f"{fs_name}.zip",
|
|
fs_name_no_tags=fs_name,
|
|
fs_name_no_ext=fs_name,
|
|
fs_extension="zip",
|
|
fs_path=f"{platform.slug}/roms",
|
|
)
|
|
)
|
|
if metadata:
|
|
rom = db_rom_handler.update_rom(rom.id, metadata)
|
|
return rom
|
|
|
|
|
|
def _filtered_names(**kwargs) -> set[str]:
|
|
return {rom.name or "" for rom in db_rom_handler.get_roms_scalar(**kwargs)}
|
|
|
|
|
|
@pytest.fixture
|
|
def length_roms(platform: Platform) -> None:
|
|
_make_rom(platform, "short", hltb_metadata={"main_story": SHORT_SECONDS})
|
|
_make_rom(platform, "medium", hltb_metadata={"main_story": MEDIUM_SECONDS})
|
|
_make_rom(platform, "long", hltb_metadata={"main_story": LONG_SECONDS})
|
|
_make_rom(platform, "unknown")
|
|
|
|
|
|
class TestGeneratedColumn:
|
|
@pytest.mark.parametrize(
|
|
("metadata", "expected"),
|
|
[
|
|
({"main_story": MEDIUM_SECONDS}, MEDIUM_SECONDS),
|
|
({"main_story": 0}, None),
|
|
({"main_story": None}, None),
|
|
({"main_story": "not a number"}, None),
|
|
({"main_plus_extra": MEDIUM_SECONDS}, None),
|
|
({}, None),
|
|
],
|
|
)
|
|
def test_derives_the_main_story_seconds(
|
|
self, platform: Platform, metadata: dict[str, Any], expected: int | None
|
|
):
|
|
rom = _make_rom(platform, "derived", hltb_metadata=metadata)
|
|
|
|
reloaded = db_rom_handler.get_rom(rom.id)
|
|
assert reloaded is not None
|
|
assert reloaded.generated_hltb_main_story == expected
|
|
|
|
|
|
class TestLengthSort:
|
|
@pytest.mark.parametrize("order_dir", ["asc", "desc"])
|
|
def test_breaks_ties_on_the_primary_key(self, order_dir: str):
|
|
"""Length ties are common (every rom without a HowLongToBeat time is
|
|
NULL), and the gallery pages by offset, so the order has to be total."""
|
|
query, _ = db_rom_handler.get_roms_query(
|
|
order_by="hltb_main_story", order_dir=order_dir
|
|
)
|
|
|
|
assert str(query).endswith(f"roms.id {order_dir.upper()}")
|
|
|
|
def test_paging_a_tie_block_neither_repeats_nor_drops_a_rom(
|
|
self, platform: Platform
|
|
):
|
|
for index in range(10):
|
|
_make_rom(
|
|
platform, f"tied-{index}", hltb_metadata={"main_story": MEDIUM_SECONDS}
|
|
)
|
|
|
|
paged: list[str] = []
|
|
for offset in range(0, 10, 3):
|
|
page = db_rom_handler.get_roms_scalar(order_by="hltb_main_story")[
|
|
offset : offset + 3
|
|
]
|
|
paged.extend(rom.name or "" for rom in page)
|
|
|
|
assert sorted(paged) == sorted(f"tied-{i}" for i in range(10))
|
|
|
|
def test_ascending(self, length_roms: None):
|
|
ordered = [
|
|
rom.name
|
|
for rom in db_rom_handler.get_roms_scalar(order_by="hltb_main_story")
|
|
]
|
|
|
|
assert ordered == ["short", "medium", "long", "unknown"]
|
|
|
|
def test_descending(self, length_roms: None):
|
|
ordered = [
|
|
rom.name
|
|
for rom in db_rom_handler.get_roms_scalar(
|
|
order_by="hltb_main_story", order_dir="desc"
|
|
)
|
|
]
|
|
|
|
assert ordered == ["long", "medium", "short", "unknown"]
|
|
|
|
|
|
class TestLengthFilter:
|
|
def test_minimum_only(self, length_roms: None):
|
|
assert _filtered_names(hltb_main_story_min=MEDIUM_SECONDS) == {
|
|
"medium",
|
|
"long",
|
|
}
|
|
|
|
def test_maximum_only(self, length_roms: None):
|
|
assert _filtered_names(hltb_main_story_max=MEDIUM_SECONDS) == {
|
|
"short",
|
|
"medium",
|
|
}
|
|
|
|
def test_both_bounds(self, length_roms: None):
|
|
assert _filtered_names(
|
|
hltb_main_story_min=SHORT_SECONDS + 1,
|
|
hltb_main_story_max=LONG_SECONDS - 1,
|
|
) == {"medium"}
|
|
|
|
def test_unset_bounds_filter_nothing(self, length_roms: None):
|
|
assert _filtered_names() == {"short", "medium", "long", "unknown"}
|
|
|
|
def test_a_zero_minimum_still_hides_roms_without_a_length(self, length_roms: None):
|
|
assert _filtered_names(hltb_main_story_min=0) == {"short", "medium", "long"}
|
|
|
|
|
|
class TestSmartCollectionCriteria:
|
|
def test_saved_length_range_scopes_membership(
|
|
self, length_roms: None, admin_user: User
|
|
):
|
|
smart_collection = db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name="Short games",
|
|
description="",
|
|
user_id=admin_user.id,
|
|
is_public=False,
|
|
filter_criteria={"hltb_main_story_max": MEDIUM_SECONDS},
|
|
)
|
|
)
|
|
|
|
assert _filtered_names(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
) == {"short", "medium"}
|