Files
romm/backend/tests/handler/database/test_rom_facets.py
Georges-Antoine Assi 2c98488951 refactor(steam): move the CDN probe into the adapter, trim the heartbeat
The portrait-capsule HEAD request was the one Steam call living outside
SteamService, with its own session, user-agent and timeout. It moves in as
get_library_capsule_url, following LibretroThumbnailsService.head, so the
handler just picks the capsule or falls back to the header image.

get_app_details grows a filters passthrough, and the heartbeat asks for
"basic" instead of pulling a whole store page to read one success flag.

Guard the facet mirror against a provider that reaches the model but not a
migration's trigger column list: test_every_provider_id_is_mirrored writes
every METADATA_SOURCE_COLUMNS id and asserts each roms_facets twin follows.
Verified against triggers rebuilt without steam_id, where it fails naming
the missing provider.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-01 19:20:14 -04:00

121 lines
4.0 KiB
Python

"""Checks for the `roms_facets` mirror that backs the filter dropdowns.
The table holds a copy of each ROM's filter values (migration 0100) and is
maintained by database triggers on `roms`, not by application code, so these
tests write through the normal handlers and assert the mirror follows.
"""
from sqlalchemy import String, select
from handler.database import db_rom_handler
from handler.database.base_handler import sync_session
from models.rom import (
METADATA_SOURCE_COLUMNS,
METADATA_SOURCE_FACET_COLUMNS,
Rom,
RomFacets,
)
def _facets(rom_id: int) -> RomFacets | None:
with sync_session.begin() as session:
return session.scalar(select(RomFacets).where(RomFacets.rom_id == rom_id))
class TestRomFacets:
def test_insert_mirrors_the_rom(self, rom: Rom):
facets = _facets(rom.id)
assert facets is not None
assert facets.platform_id == rom.platform_id
def test_update_mirrors_derived_and_raw_values(self, rom: Rom):
db_rom_handler.update_rom(
rom.id,
{
"igdb_metadata": {"genres": ["Action", "RPG"], "franchises": ["Zelda"]},
"regions": ["USA"],
"tags": ["Proto"],
},
)
facets = _facets(rom.id)
assert facets is not None
assert facets.genres == ["Action", "RPG"]
assert facets.franchises == ["Zelda"]
assert facets.regions == ["USA"]
assert facets.tags == ["Proto"]
def test_provider_ids_mirror_the_rom(self, rom: Rom):
# Backs the Server Stats metadata-coverage breakdown, which counts these
# off the mirror instead of scanning `roms`.
db_rom_handler.update_rom(
rom.id,
{"igdb_id": 1234, "moby_id": 56, "flashpoint_id": "fp-1"},
)
facets = _facets(rom.id)
assert facets is not None
assert facets.igdb_id == 1234
assert facets.moby_id == 56
assert facets.flashpoint_id == "fp-1"
# Sources the ROM didn't match stay null.
assert facets.ss_id is None
def test_every_provider_id_is_mirrored(self, rom: Rom):
"""Guards the trigger column list: a provider missed there mirrors null."""
# A string id for the providers whose column is a slug, else an int.
values = {
slug: f"{slug}-1" if isinstance(column.type, String) else index + 1
for index, (slug, column) in enumerate(METADATA_SOURCE_COLUMNS.items())
}
db_rom_handler.update_rom(
rom.id,
{
column.key: values[slug]
for slug, column in METADATA_SOURCE_COLUMNS.items()
},
)
facets = _facets(rom.id)
assert facets is not None
assert {
slug: getattr(facets, column.key)
for slug, column in METADATA_SOURCE_FACET_COLUMNS.items()
} == values
def test_publishers_developers_mirror(self, rom: Rom):
db_rom_handler.update_rom(
rom.id,
{
"ss_metadata": {
"companies": ["Atari", "Artech Studios"],
"publishers": ["Atari"],
"developers": ["Artech Studios"],
}
},
)
facets = _facets(rom.id)
assert facets is not None
assert facets.publishers == ["Atari"]
assert facets.developers == ["Artech Studios"]
filters = db_rom_handler.get_rom_filters()
assert "Atari" in filters["publishers"]
assert "Artech Studios" in filters["developers"]
def test_delete_cascades(self, rom: Rom):
rom_id = rom.id
db_rom_handler.delete_rom(rom_id)
assert _facets(rom_id) is None
def test_filter_values_follow_a_metadata_edit(self, rom: Rom):
db_rom_handler.update_rom(
rom.id, {"igdb_metadata": {"genres": ["Puzzle"]}, "languages": ["En"]}
)
filters = db_rom_handler.get_rom_filters()
assert "Puzzle" in filters["genres"]
assert "En" in filters["languages"]