Files
romm/backend/tests/handler/database/test_stats_handler.py
Sam Dornan 79fbce6beb test(backend): build test rows through shared factories and enforce them with ruff (#4972)
* 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>
2026-09-30 14:01:56 -05:00

136 lines
5.0 KiB
Python

"""Checks for the Server Stats per-platform breakdowns.
Both breakdowns aggregate from the narrow `roms_facets` mirror (migration 0100,
extended with the provider match ids) rather than scanning the multi-gigabyte
`roms` table. The mirror is trigger-maintained, so these tests write ROMs
through the normal handler and assert the breakdowns read the mirrored values.
"""
from tests.factories import make_rom
from handler.database import db_platform_handler, db_stats_handler
from models.platform import Platform
def _add_platform(slug: str) -> Platform:
return db_platform_handler.add_platform(
Platform(name=slug, slug=slug, fs_slug=slug)
)
class TestRegionBreakdown:
def test_counts_regions_per_platform(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
make_rom(platform_a, "a1", regions=["USA", "Japan"])
make_rom(platform_a, "a2", regions=["USA"])
make_rom(platform_b, "b1", regions=["Europe"])
breakdown = db_stats_handler.get_region_breakdown_by_platform()
assert breakdown[platform_a.id] == [
{"region": "USA", "count": 2},
{"region": "Japan", "count": 1},
]
assert breakdown[platform_b.id] == [{"region": "Europe", "count": 1}]
def test_empty_regions_are_omitted(self):
platform = _add_platform("platform_empty")
make_rom(platform, "no_region", regions=[])
breakdown = db_stats_handler.get_region_breakdown_by_platform()
assert platform.id not in breakdown
def test_hidden_platforms_are_excluded(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
make_rom(platform_a, "a1", regions=["USA"])
make_rom(platform_b, "b1", regions=["Europe"])
breakdown = db_stats_handler.get_region_breakdown_by_platform(
hidden_platform_ids=[platform_b.id]
)
assert platform_a.id in breakdown
assert platform_b.id not in breakdown
def test_hidden_roms_are_excluded(self):
platform = _add_platform("platform_a")
make_rom(platform, "a1", regions=["USA"])
hidden = make_rom(platform, "a2", regions=["USA"])
breakdown = db_stats_handler.get_region_breakdown_by_platform(
hidden_rom_ids=[hidden.id]
)
assert breakdown[platform.id] == [{"region": "USA", "count": 1}]
class TestMetadataCoverage:
def test_counts_matched_sources_per_platform(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
make_rom(platform_a, "a1", igdb_id=1, ss_id=2)
make_rom(platform_a, "a2", igdb_id=3)
make_rom(platform_b, "b1", moby_id=5)
coverage = db_stats_handler.get_metadata_coverage_by_platform()
assert {"source": "igdb", "matched": 2} in coverage[platform_a.id]
assert {"source": "ss", "matched": 1} in coverage[platform_a.id]
assert coverage[platform_b.id] == [{"source": "moby", "matched": 1}]
def test_string_id_sources_are_counted(self):
"""flashpoint/gamelist/libretro ids are strings, not integers."""
platform = _add_platform("platform_a")
make_rom(platform, "a1", flashpoint_id="abc", libretro_id="snes")
coverage = db_stats_handler.get_metadata_coverage_by_platform()
assert {"source": "flashpoint", "matched": 1} in coverage[platform.id]
assert {"source": "libretro", "matched": 1} in coverage[platform.id]
def test_blank_ids_are_not_counted(self):
"""0 and "" read as no match, as they do for `Rom.is_identified`."""
platform = _add_platform("platform_a")
make_rom(platform, "a1", igdb_id=0, libretro_id="")
make_rom(platform, "a2", ss_id=4)
coverage = db_stats_handler.get_metadata_coverage_by_platform()
assert coverage[platform.id] == [{"source": "ss", "matched": 1}]
def test_unmatched_sources_are_omitted(self):
platform = _add_platform("platform_a")
make_rom(platform, "a1", igdb_id=1)
coverage = db_stats_handler.get_metadata_coverage_by_platform()
sources = {item["source"] for item in coverage[platform.id]}
assert sources == {"igdb"}
def test_hidden_platforms_are_excluded(self):
platform_a = _add_platform("platform_a")
platform_b = _add_platform("platform_b")
make_rom(platform_a, "a1", igdb_id=1)
make_rom(platform_b, "b1", igdb_id=2)
coverage = db_stats_handler.get_metadata_coverage_by_platform(
hidden_platform_ids=[platform_b.id]
)
assert platform_a.id in coverage
assert platform_b.id not in coverage
def test_hidden_roms_are_excluded(self):
platform = _add_platform("platform_a")
make_rom(platform, "a1", igdb_id=1)
hidden = make_rom(platform, "a2", igdb_id=2)
coverage = db_stats_handler.get_metadata_coverage_by_platform(
hidden_rom_ids=[hidden.id]
)
assert coverage[platform.id] == [{"source": "igdb", "matched": 1}]