Files
romm/backend/tests/handler/database/test_roms_handler.py
Georges-Antoine Assi 987e351113 refactor: derive file-name columns via @validates
Centralize the *_no_tags / *_no_ext / *_extension columns (derived from a
file name) behind @validates hooks instead of computing them by hand at
every write site:

- Add pure helpers (compute_file_name_parts and friends) to models.base;
  the filesystem base handler now delegates to them.
- Add @validates on Rom (fs_name), BaseAsset (file_name, inherited by all
  asset subclasses), and Firmware (file_name).
- update_rom keeps the fs_name-derived columns in sync on bulk update(),
  which also fixes the rename path never updating fs_extension.
- Drop the now-redundant computations at the scan/rename call sites.

Also fix the migration backfill loop and a pre-existing list[str | None]
type mismatch surfaced in scan_handler. Add tests for the helpers, the
validators, and the update_rom bulk-sync path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 21:14:16 -04:00

34 lines
1.3 KiB
Python

"""Unit tests for DBRomsHandler's derived-column bookkeeping.
Bulk `update()` bypasses the ORM `@validates` hooks, so `update_rom` keeps
the columns derived from `name` / `fs_name` in sync explicitly.
"""
from handler.database import db_rom_handler
from models.rom import Rom
class TestUpdateRomDerivedColumns:
def test_update_name_resyncs_name_sort_key(self, rom: Rom):
updated = db_rom_handler.update_rom(rom.id, {"name": "The New Name 2"})
assert updated.name == "The New Name 2"
assert updated.name_sort_key == "new name 000000000002"
def test_update_fs_name_resyncs_all_parts(self, rom: Rom):
updated = db_rom_handler.update_rom(rom.id, {"fs_name": "Sonic (Europe).md"})
assert updated.fs_name == "Sonic (Europe).md"
assert updated.fs_name_no_tags == "Sonic"
assert updated.fs_name_no_ext == "Sonic (Europe)"
# The extension is resynced too — the rename endpoint used to omit it.
assert updated.fs_extension == "md"
def test_update_unrelated_field_leaves_derived_columns(self, rom: Rom):
updated = db_rom_handler.update_rom(rom.id, {"summary": "just a summary"})
assert updated.summary == "just a summary"
assert updated.fs_name_no_tags == "test_rom"
assert updated.fs_extension == "zip"
assert updated.name_sort_key == "test_rom"