mirror of
https://github.com/rommapp/romm.git
synced 2026-12-06 15:54:45 +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>
235 lines
7.7 KiB
Python
235 lines
7.7 KiB
Python
"""What a scan does to a ROM's external IDs.
|
|
|
|
A complete rescan clears every ID it could rematch; a provider that failed
|
|
answered nothing, so the ID it could not check has to survive.
|
|
"""
|
|
|
|
from typing import Any
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
from tests.handler.scan_stubs import add_n64_platform, add_rom, run_scan
|
|
|
|
from handler.database import db_rom_handler
|
|
from handler.metadata.hasheous_handler import HasheousRom
|
|
from handler.metadata.igdb_handler import IGDBRom
|
|
from handler.metadata.pouet_handler import PouetRom
|
|
from handler.metadata.sgdb_handler import SGDBRom
|
|
from handler.metadata.ss_handler import ScreenScraperExhaustedError, SSRom
|
|
from handler.scan_handler import MetadataSource, ScanType
|
|
from models.rom import Rom
|
|
|
|
FS_NAME = "Some Homebrew Thing (Aftermarket).z64"
|
|
STALE_IGDB_ID = 1289
|
|
STALE_MOBY_ID = 42
|
|
STALE_SGDB_ID = 777
|
|
STALE_POUET_ID = 106640
|
|
STALE_SS_ID = 314
|
|
STALE_HASHEOUS_ID = 2718
|
|
|
|
MISS = IGDBRom(igdb_id=None)
|
|
SGDB_MISS = SGDBRom(sgdb_id=None)
|
|
POUET_MISS = PouetRom(pouet_id=None)
|
|
MATCH = IGDBRom(igdb_id=9999, name="A Real Game")
|
|
UNREACHABLE = HTTPException(status_code=503, detail="provider is down")
|
|
SS_MISS = SSRom(ss_id=None)
|
|
SS_SHORT_CIRCUITED = ScreenScraperExhaustedError(SS_MISS)
|
|
HASHEOUS_MISS = HasheousRom(hasheous_id=None, igdb_id=None, tgdb_id=None)
|
|
|
|
|
|
def _lookup(result: Any) -> AsyncMock:
|
|
"""A provider lookup that answers with `result`, or fails when it is one."""
|
|
if isinstance(result, Exception):
|
|
return AsyncMock(side_effect=result)
|
|
return AsyncMock(return_value=result)
|
|
|
|
|
|
async def _rescan(
|
|
sources: list[str],
|
|
*,
|
|
scan_type: ScanType = ScanType.COMPLETE,
|
|
igdb_result: IGDBRom | Exception = MISS,
|
|
sgdb_result: SGDBRom | Exception = SGDB_MISS,
|
|
pouet_result: PouetRom | Exception = POUET_MISS,
|
|
platform_igdb_id: int | None = 4,
|
|
ss_lookup_result: tuple[SSRom, bool] | Exception = (SS_MISS, False),
|
|
ss_get_rom_result: SSRom | Exception = SS_MISS,
|
|
platform_hasheous_id: int | None = None,
|
|
hasheous_lookup_result: tuple[HasheousRom, bool] = (HASHEOUS_MISS, True),
|
|
) -> Rom:
|
|
"""Rescan a ROM carrying hand-set IDs, and persist the result."""
|
|
platform = add_n64_platform(
|
|
igdb_id=platform_igdb_id, ss_id=14, hasheous_id=platform_hasheous_id
|
|
)
|
|
rom = add_rom(
|
|
platform,
|
|
FS_NAME,
|
|
"Some Homebrew Thing",
|
|
name="Wrong Match",
|
|
igdb_id=STALE_IGDB_ID,
|
|
igdb_metadata={"total_rating": "9"},
|
|
moby_id=STALE_MOBY_ID,
|
|
sgdb_id=STALE_SGDB_ID,
|
|
pouet_id=STALE_POUET_ID,
|
|
ss_id=STALE_SS_ID,
|
|
hasheous_id=STALE_HASHEOUS_ID,
|
|
)
|
|
|
|
igdb_mock = _lookup(igdb_result)
|
|
pouet_mock = _lookup(pouet_result)
|
|
with (
|
|
patch("handler.scan_handler.meta_igdb_handler.get_rom", new=igdb_mock),
|
|
patch("handler.scan_handler.meta_igdb_handler.get_rom_by_id", new=igdb_mock),
|
|
patch(
|
|
"handler.scan_handler.meta_sgdb_handler.get_details_by_names",
|
|
new=_lookup(sgdb_result),
|
|
),
|
|
patch("handler.scan_handler.meta_pouet_handler.get_rom", new=pouet_mock),
|
|
patch("handler.scan_handler.meta_pouet_handler.get_rom_by_id", new=pouet_mock),
|
|
patch(
|
|
"handler.scan_handler.meta_ss_handler.lookup_rom",
|
|
new=_lookup(ss_lookup_result),
|
|
),
|
|
patch(
|
|
"handler.scan_handler.meta_ss_handler.get_rom",
|
|
new=_lookup(ss_get_rom_result),
|
|
),
|
|
patch(
|
|
"handler.scan_handler.meta_hasheous_handler.is_enabled", return_value=True
|
|
),
|
|
patch(
|
|
"handler.scan_handler.meta_hasheous_handler.lookup_rom",
|
|
new=_lookup(hasheous_lookup_result),
|
|
),
|
|
patch(
|
|
"handler.scan_handler.meta_hasheous_handler.get_igdb_game",
|
|
new=_lookup(HASHEOUS_MISS),
|
|
),
|
|
):
|
|
scanned = await run_scan(
|
|
platform, rom, scan_type=scan_type, metadata_sources=sources
|
|
)
|
|
|
|
return db_rom_handler.add_rom(scanned) # noqa: TID251
|
|
|
|
|
|
async def test_a_searched_source_that_misses_drops_its_stale_id():
|
|
saved = await _rescan([MetadataSource.IGDB])
|
|
|
|
assert saved.igdb_id is None
|
|
assert saved.igdb_metadata == {}
|
|
|
|
|
|
async def test_a_searched_source_that_matches_stores_the_new_id():
|
|
saved = await _rescan([MetadataSource.IGDB], igdb_result=MATCH)
|
|
|
|
assert saved.igdb_id == 9999
|
|
|
|
|
|
async def test_a_source_the_platform_cannot_use_keeps_its_id():
|
|
"""No `platform.igdb_id` means IGDB never ran, so it rules nothing out."""
|
|
saved = await _rescan([MetadataSource.IGDB], platform_igdb_id=None)
|
|
|
|
assert saved.igdb_id == STALE_IGDB_ID
|
|
|
|
|
|
async def test_a_source_that_fails_keeps_its_id():
|
|
saved = await _rescan(
|
|
[MetadataSource.IGDB], igdb_result=RuntimeError("IGDB is down")
|
|
)
|
|
|
|
assert saved.igdb_id == STALE_IGDB_ID
|
|
|
|
|
|
async def test_a_source_that_could_not_be_reached_keeps_its_id():
|
|
"""Pouët reports an outage as an empty match, so the handler has to raise."""
|
|
saved = await _rescan(
|
|
[MetadataSource.IGDB, MetadataSource.POUET], pouet_result=UNREACHABLE
|
|
)
|
|
|
|
assert saved.pouet_id == STALE_POUET_ID
|
|
|
|
|
|
async def test_a_screenscraper_breaker_keeps_its_id():
|
|
"""A breaker answers every remaining ROM, so it rules nothing out."""
|
|
saved = await _rescan(
|
|
[MetadataSource.IGDB, MetadataSource.SS],
|
|
ss_lookup_result=SS_SHORT_CIRCUITED,
|
|
ss_get_rom_result=SS_SHORT_CIRCUITED,
|
|
)
|
|
|
|
assert saved.ss_id == STALE_SS_ID
|
|
|
|
|
|
async def test_a_screenscraper_breaker_that_recovers_mid_rom_keeps_its_id():
|
|
"""The breaker can clear between this rom's two lookups, but the hash lookup
|
|
it short-circuited still ruled nothing out."""
|
|
saved = await _rescan(
|
|
[MetadataSource.IGDB, MetadataSource.SS],
|
|
ss_lookup_result=SS_SHORT_CIRCUITED,
|
|
ss_get_rom_result=SS_MISS,
|
|
)
|
|
|
|
assert saved.ss_id == STALE_SS_ID
|
|
|
|
|
|
async def test_a_hasheous_lookup_that_never_answered_keeps_its_id():
|
|
"""Hasheous swallows its own failures, so the conclusive flag is all a rescan
|
|
has to tell an outage from a miss it confirmed."""
|
|
saved = await _rescan(
|
|
[MetadataSource.IGDB, MetadataSource.HASHEOUS],
|
|
platform_hasheous_id=64,
|
|
hasheous_lookup_result=(HASHEOUS_MISS, False),
|
|
)
|
|
|
|
assert saved.hasheous_id == STALE_HASHEOUS_ID
|
|
|
|
|
|
async def test_a_hasheous_miss_it_confirmed_still_drops_its_id():
|
|
saved = await _rescan(
|
|
[MetadataSource.IGDB, MetadataSource.HASHEOUS],
|
|
platform_hasheous_id=64,
|
|
)
|
|
|
|
assert saved.hasheous_id is None
|
|
|
|
|
|
async def test_a_selected_screenscraper_that_misses_still_drops_its_id():
|
|
saved = await _rescan([MetadataSource.IGDB, MetadataSource.SS])
|
|
|
|
assert saved.ss_id is None
|
|
|
|
|
|
async def test_a_deselected_source_still_drops_its_id():
|
|
saved = await _rescan([MetadataSource.IGDB])
|
|
|
|
assert saved.moby_id is None
|
|
|
|
|
|
async def test_steamgriddb_drops_its_stale_id_after_a_miss():
|
|
"""SteamGridDB resolves after the reset, so it clears its own ID."""
|
|
saved = await _rescan([MetadataSource.IGDB, MetadataSource.SGDB], igdb_result=MATCH)
|
|
|
|
assert saved.sgdb_id is None
|
|
|
|
|
|
async def test_steamgriddb_keeps_its_stale_id_when_it_could_not_be_reached():
|
|
saved = await _rescan(
|
|
[MetadataSource.IGDB, MetadataSource.SGDB],
|
|
igdb_result=MATCH,
|
|
sgdb_result=UNREACHABLE,
|
|
)
|
|
|
|
assert saved.sgdb_id == STALE_SGDB_ID
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"scan_type", [ScanType.QUICK, ScanType.UPDATE, ScanType.UNMATCHED]
|
|
)
|
|
async def test_only_a_complete_rescan_drops_ids(scan_type: ScanType):
|
|
"""Every other scan type carries the existing IDs forward untouched."""
|
|
saved = await _rescan([MetadataSource.IGDB], scan_type=scan_type)
|
|
|
|
assert saved.igdb_id == STALE_IGDB_ID
|