mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
* test(backend): build test ROMs through one make_rom factory Tests created Rom rows by hand in dozens of places, each spelling out the same slug and filename fields and deriving the slug inline. Production never computes a slug from a name, so this was test-only filler duplicated across files. tests/factories.py adds make_rom(platform, name, *, fs_stem, fs_extension, **overrides), which fills the slug and filename columns and persists through db_rom_handler.add_rom. The shared conftest fixtures, scan_stubs.add_rom and the local helpers and inline constructors across 35 test files now use it. Each test keeps its filename shape (underscored stems in the FULLTEXT search tests, spaced "Name (USA)" stems in the sibling tests), and fixtures shared across files keep their exact slugs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend): keep extensionless filenames intact in scan_stubs.add_rom rpartition(".") on a name with no dot returned an empty stem and the whole name as the extension, so make_rom stored ".README" for "README". os.path.splitext leaves such names whole. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend): build test saves, states and screenshots through factories Tests built Save, State and Screenshot rows by hand across about 20 files, each spelling out file_name_no_tags, file_name_no_ext and file_extension although BaseAsset already derives them from file_name. tests/factories.py adds make_save, make_state and make_screenshot. Each sets the rom and user ids, the file name, and a default file_path under the rom's platform, applies overrides last, and persists through the matching db handler. The conftest asset fixtures and the persisted constructors and local helpers now use them. Rows a test observes keep their file_path, emulator, slot, size and hash, and a filename part stays an override wherever it differs from what the model derives. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend): ban direct add_* persistence in tests via ruff TID251 The factories only stay the one home for test rows if nothing routes around them. Ruff's banned-api rule now flags db_rom_handler.add_rom, db_save_handler.add_save, db_state_handler.add_state and db_screenshot_handler.add_screenshot anywhere under tests/ except tests/factories.py. Production code is unaffected. The remaining add_rom calls move onto make_rom: the unique-path, soundtrack and id-index tests in test_roms_handler.py, the scan tests in test_fastapi.py, physical and missing rows in the feed, download and model tests, and the name=None exporter rows. Scan tests keep a filename-derived placeholder as the name, which scan_rom treats the same as no name. Three calls keep a noqa: the tests of how add_rom merges a partial scanned row, and the rescan test that persists scan_rom's output as a scan does. The backend skill and the architecture doc now point at tests/factories.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend): move the firmware and client-token factories into tests/factories.py The add_firmware and add_device_token conftest fixtures returned a builder, unlike the importable make_* functions. They become make_firmware and make_device_token alongside them, with the same defaults, and their callers import them rather than requesting a fixture. make_device_token now draws the raw credential from auth_handler.generate_client_token, so it carries the real rmm_ prefix instead of restating it, and the hand-built client tokens in the auth tests use it too. The ruff TID251 ban now also covers db_firmware_handler.add_firmware and db_client_token_handler.add_token under tests/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: point at every test factory, not just the ROM and asset ones Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
5.3 KiB
Python
157 lines
5.3 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 tests.factories import make_rom
|
|
|
|
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 = make_rom(platform, fs_name)
|
|
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"}
|