Files
romm/backend/tests/handler/database/test_rom_facets.py
Georges-Antoine Assi 6224c9e48c perf(backend): mirror ROM filter values into a narrow trigger-maintained table
Building the filter dropdowns aggregated the facet columns straight off
`roms`. Those columns are cheap to compute since #3826, but they sit inline
with the raw provider-metadata blobs, so the aggregation read every row of a
multi-gigabyte table: seconds of pure disk reading on any database whose
buffer pool is smaller than the table (the stock 128-256 MB defaults always
are on a large library).

`roms_facets` holds only the filter values and is kept in sync by AFTER
INSERT/UPDATE triggers on `roms`, so every write path stays consistent
without an application hook. Deletes ride the ON DELETE CASCADE foreign key.

Fixes #3882

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-22 10:05:07 -04:00

57 lines
1.9 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_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"]