Files
romm/backend/tests/handler/database/test_rom_facets.py
Spinnich f69e4756a7 perf(stats): aggregate server-stats breakdowns from roms_facets
The Server Stats page ran two full scans of the multi-gigabyte `roms`
table on every visit: the metadata-coverage breakdown counted eleven
provider-id columns per platform, and the region breakdown pulled every
row's regions into Python. Both were returned in the same request as the
cheap summary counts, so the whole page stayed blank until they finished.

Mirror the provider match ids into the narrow `roms_facets` table
(migration 0102, kept in sync by the same triggers as the existing facet
columns) and read both breakdowns from it, so neither query touches
`roms`. Split the v2 Server Stats view into a summary-first request and a
separate breakdown request so the six top cards render immediately.

Fixes #3901

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-23 17:27:09 +00:00

73 lines
2.4 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 select
from handler.database import db_rom_handler
from handler.database.base_handler import sync_session
from models.rom import 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_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"]