mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 10:32:57 +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>
269 lines
10 KiB
Python
269 lines
10 KiB
Python
"""Checks for the `rom_identity_keys` mirror that backs the `sibling_roms` view.
|
|
|
|
Migration 0127 moved sibling matching off an OR-of-equalities self-join over
|
|
`roms` and onto one row per (ROM, provider it has an id for), maintained by
|
|
triggers on `roms` rather than by application code. These tests write through
|
|
the normal handlers and assert both the key rows and the view follow.
|
|
"""
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from sqlalchemy import String, select
|
|
from tests.factories import make_rom
|
|
|
|
from handler.database import db_rom_handler
|
|
from handler.database.base_handler import sync_session
|
|
from models.platform import Platform
|
|
from models.rom import (
|
|
IDENTITY_ID_FIELDS,
|
|
Rom,
|
|
RomIdentityKey,
|
|
SiblingRom,
|
|
)
|
|
from models.user import User
|
|
|
|
# `flashpoint_id` is a varchar column, the other ids are integers, so a
|
|
# parametrized test has to hand each one a value its column accepts.
|
|
_STRING_ID_FIELDS = frozenset(
|
|
field
|
|
for field in IDENTITY_ID_FIELDS
|
|
if isinstance(Rom.__table__.c[field].type, String)
|
|
)
|
|
|
|
|
|
def _sample_id(field: str, value: int) -> int | str:
|
|
return str(value) if field in _STRING_ID_FIELDS else value
|
|
|
|
|
|
def _keys(rom_id: int) -> set[tuple[int, int, str]]:
|
|
"""The (provider code, platform_id, provider_id) rows the triggers wrote.
|
|
|
|
`provider_id` is the varchar every provider's id is cast into.
|
|
"""
|
|
with sync_session.begin() as session:
|
|
return {
|
|
tuple(row)
|
|
for row in session.execute(
|
|
select(
|
|
RomIdentityKey.provider,
|
|
RomIdentityKey.platform_id,
|
|
RomIdentityKey.provider_id,
|
|
).where(RomIdentityKey.rom_id == rom_id)
|
|
).all()
|
|
}
|
|
|
|
|
|
def _view_rows(rom_id: int) -> list[int]:
|
|
"""Raw `sibling_roms` rows, undeduped: the view emits one per match."""
|
|
with sync_session.begin() as session:
|
|
return sorted(
|
|
session.scalars(
|
|
select(SiblingRom.sibling_rom_id).where(SiblingRom.rom_id == rom_id)
|
|
).all()
|
|
)
|
|
|
|
|
|
def _siblings(rom_id: int, user_id: int) -> list[int]:
|
|
with sync_session.begin() as session:
|
|
buckets = db_rom_handler.get_siblings_for_roms(
|
|
[rom_id], user_id=user_id, session=session
|
|
)
|
|
return sorted(sibling.id for sibling, _ in buckets[rom_id])
|
|
|
|
|
|
class TestRomIdentityKeys:
|
|
def test_insert_writes_one_key_per_matched_provider(self, platform: Platform):
|
|
rom = make_rom(platform, "scraped", igdb_id=11, ss_id=22)
|
|
|
|
assert _keys(rom.id) == {
|
|
(IDENTITY_ID_FIELDS.index("igdb_id"), platform.id, "11"),
|
|
(IDENTITY_ID_FIELDS.index("ss_id"), platform.id, "22"),
|
|
}
|
|
|
|
def test_unscraped_rom_has_no_keys_and_no_siblings(
|
|
self, admin_user: User, platform: Platform
|
|
):
|
|
first = make_rom(platform, "unscraped_a")
|
|
second = make_rom(platform, "unscraped_b")
|
|
|
|
assert _keys(first.id) == set()
|
|
assert _siblings(first.id, admin_user.id) == []
|
|
assert _siblings(second.id, admin_user.id) == []
|
|
|
|
@pytest.mark.parametrize("field", IDENTITY_ID_FIELDS)
|
|
def test_each_provider_matches_on_its_own(
|
|
self, admin_user: User, platform: Platform, field: str
|
|
):
|
|
"""Guards the trigger's provider list against the view's.
|
|
|
|
Appending to `IDENTITY_ID_FIELDS` without a migration that backfills
|
|
and matches on the new provider fails here.
|
|
"""
|
|
ids: dict[str, Any] = {field: _sample_id(field, 4242)}
|
|
first = make_rom(platform, f"{field}_a", **ids)
|
|
second = make_rom(platform, f"{field}_b", **ids)
|
|
|
|
assert _siblings(first.id, admin_user.id) == [second.id]
|
|
assert _siblings(second.id, admin_user.id) == [first.id]
|
|
|
|
def test_ids_from_different_providers_do_not_collide(
|
|
self, admin_user: User, platform: Platform
|
|
):
|
|
"""The provider is part of the key, so `igdb_id = 5` is not `ss_id = 5`."""
|
|
igdb = make_rom(platform, "igdb_five", igdb_id=5)
|
|
ss = make_rom(platform, "ss_five", ss_id=5)
|
|
|
|
assert _siblings(igdb.id, admin_user.id) == []
|
|
assert _siblings(ss.id, admin_user.id) == []
|
|
|
|
def test_same_id_on_another_platform_is_not_a_sibling(
|
|
self, admin_user: User, platform: Platform, other_platform: Platform
|
|
):
|
|
here = make_rom(platform, "here", igdb_id=77)
|
|
there = make_rom(other_platform, "there", igdb_id=77)
|
|
|
|
assert _siblings(here.id, admin_user.id) == []
|
|
assert _siblings(there.id, admin_user.id) == []
|
|
|
|
def test_pair_matching_two_providers_surfaces_once(
|
|
self, admin_user: User, platform: Platform
|
|
):
|
|
"""The view emits a row per matching provider; the API must not."""
|
|
first = make_rom(platform, "twice_a", igdb_id=9, ss_id=9)
|
|
second = make_rom(platform, "twice_b", igdb_id=9, ss_id=9)
|
|
|
|
assert _view_rows(first.id) == [second.id, second.id]
|
|
assert _siblings(first.id, admin_user.id) == [second.id]
|
|
|
|
loaded = db_rom_handler.get_rom(first.id)
|
|
assert loaded is not None
|
|
assert [sibling.id for sibling in loaded.sibling_roms] == [second.id]
|
|
|
|
def test_metadata_refresh_moves_the_keys(
|
|
self, admin_user: User, platform: Platform
|
|
):
|
|
"""`update_rom` is a Core-level UPDATE, so only the trigger covers it."""
|
|
rom = make_rom(platform, "refreshed", igdb_id=100)
|
|
old_match = make_rom(platform, "old_match", igdb_id=100)
|
|
new_match = make_rom(platform, "new_match", igdb_id=200)
|
|
|
|
db_rom_handler.update_rom(rom.id, {"igdb_id": 200})
|
|
|
|
assert _keys(rom.id) == {
|
|
(IDENTITY_ID_FIELDS.index("igdb_id"), platform.id, "200")
|
|
}
|
|
assert _siblings(rom.id, admin_user.id) == [new_match.id]
|
|
assert _siblings(old_match.id, admin_user.id) == []
|
|
|
|
def test_clearing_an_id_drops_the_key(self, admin_user: User, platform: Platform):
|
|
rom = make_rom(platform, "cleared", igdb_id=300)
|
|
other = make_rom(platform, "still_matched", igdb_id=300)
|
|
|
|
db_rom_handler.update_rom(rom.id, {"igdb_id": None})
|
|
|
|
assert _keys(rom.id) == set()
|
|
assert _siblings(rom.id, admin_user.id) == []
|
|
assert _siblings(other.id, admin_user.id) == []
|
|
|
|
def test_moving_a_rom_moves_its_keys(
|
|
self, admin_user: User, platform: Platform, other_platform: Platform
|
|
):
|
|
rom = make_rom(platform, "moved", igdb_id=400)
|
|
left_behind = make_rom(platform, "left_behind", igdb_id=400)
|
|
|
|
db_rom_handler.update_rom(rom.id, {"platform_id": other_platform.id})
|
|
|
|
assert _keys(rom.id) == {
|
|
(IDENTITY_ID_FIELDS.index("igdb_id"), other_platform.id, "400")
|
|
}
|
|
assert _siblings(rom.id, admin_user.id) == []
|
|
assert _siblings(left_behind.id, admin_user.id) == []
|
|
|
|
def test_delete_cascades(self, platform: Platform):
|
|
rom = make_rom(platform, "deleted", igdb_id=500)
|
|
rom_id = rom.id
|
|
|
|
db_rom_handler.delete_rom(rom_id)
|
|
|
|
assert _keys(rom_id) == set()
|
|
|
|
def test_update_touching_no_identity_id_skips_the_resync(self, platform: Platform):
|
|
"""The `<=>` guard: without it every rom write pays a full resync."""
|
|
rom = make_rom(platform, "guarded", igdb_id=600)
|
|
marker = (IDENTITY_ID_FIELDS.index("tgdb_id"), platform.id, "999999")
|
|
with sync_session.begin() as session:
|
|
session.add(
|
|
RomIdentityKey(
|
|
provider=marker[0],
|
|
platform_id=marker[1],
|
|
provider_id=marker[2],
|
|
rom_id=rom.id,
|
|
)
|
|
)
|
|
|
|
db_rom_handler.update_rom(rom.id, {"fs_size_bytes": 1234})
|
|
assert marker in _keys(rom.id)
|
|
|
|
db_rom_handler.update_rom(rom.id, {"igdb_id": 601})
|
|
assert marker not in _keys(rom.id)
|
|
|
|
def test_hidden_siblings_are_excluded(self, admin_user: User, platform: Platform):
|
|
rom = make_rom(platform, "visible", igdb_id=700)
|
|
hidden_rom = make_rom(platform, "hidden_rom", igdb_id=700)
|
|
|
|
with sync_session.begin() as session:
|
|
buckets = db_rom_handler.get_siblings_for_roms(
|
|
[rom.id],
|
|
user_id=admin_user.id,
|
|
session=session,
|
|
hidden_rom_ids=[hidden_rom.id],
|
|
)
|
|
assert buckets[rom.id] == []
|
|
|
|
with sync_session.begin() as session:
|
|
buckets = db_rom_handler.get_siblings_for_roms(
|
|
[rom.id],
|
|
user_id=admin_user.id,
|
|
session=session,
|
|
hidden_platform_ids=[platform.id],
|
|
)
|
|
assert buckets[rom.id] == []
|
|
|
|
def test_is_main_sibling_resolves_per_user(
|
|
self, admin_user: User, platform: Platform
|
|
):
|
|
rom = make_rom(platform, "main_a", igdb_id=800)
|
|
sibling = make_rom(platform, "main_b", igdb_id=800)
|
|
rom_user = db_rom_handler.add_rom_user(rom_id=sibling.id, user_id=admin_user.id)
|
|
db_rom_handler.update_rom_user(rom_user.id, {"is_main_sibling": True})
|
|
|
|
with sync_session.begin() as session:
|
|
buckets = db_rom_handler.get_siblings_for_roms(
|
|
[rom.id], user_id=admin_user.id, session=session
|
|
)
|
|
assert [(s.id, is_main) for s, is_main in buckets[rom.id]] == [
|
|
(sibling.id, True)
|
|
]
|
|
|
|
|
|
class TestIdentityKeyStatistics:
|
|
"""Migration 0127 samples this table right after its backfill, which a fresh
|
|
install runs while the table is still empty. The scan that fills it has to
|
|
resample, or the optimizer keeps the plan it chose for an empty table.
|
|
"""
|
|
|
|
def test_resampling_leaves_the_keys_readable(self, platform: Platform):
|
|
"""Runs the real statement, so each engine's CI leg proves its own syntax."""
|
|
rom = make_rom(platform, "sampled", igdb_id=99)
|
|
|
|
db_rom_handler.refresh_identity_key_statistics()
|
|
|
|
assert _keys(rom.id) == {
|
|
(IDENTITY_ID_FIELDS.index("igdb_id"), platform.id, "99")
|
|
}
|
|
|
|
def test_resampling_an_empty_table_is_not_an_error(self) -> None:
|
|
"""A fresh install resamples after a scan that found nothing."""
|
|
db_rom_handler.refresh_identity_key_statistics()
|