Files
romm/backend/tests/handler/database/test_roms_handler.py
Georges-Antoine Assi a38ebe29b5 Preserve custom name_sort_key; gate derivation on "still derived"
Drop the name_sort_key_custom flag/migration in favour of a flagless rule: a
key is "custom" when it no longer equals compute(name). Apply that consistently
across all three write paths so a manual sort key survives renames while a
derived key keeps following the name:

- @validates re-derives on name assignment only when the stored key still
  matches the derived value; direct name_sort_key assignment stores a
  normalized custom key (or reverts to derived when cleared). Handles both
  kwarg orders at construction.
- update_rom mirrors the same check for the bulk update() path it bypasses.
- The edit endpoint only writes the key when the user actually changed the
  field, delegating the untouched case to update_rom.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 11:30:40 -04:00

47 lines
1.9 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"
def test_explicit_name_sort_key_marks_custom(self, rom: Rom):
updated = db_rom_handler.update_rom(rom.id, {"name_sort_key": "zelda"})
assert updated.name_sort_key == "zelda"
def test_update_name_keeps_custom_sort_key(self, rom: Rom):
db_rom_handler.update_rom(rom.id, {"name_sort_key": "pinned"})
updated = db_rom_handler.update_rom(rom.id, {"name": "The New Name 2"})
# A pinned custom key is never clobbered by a name change.
assert updated.name == "The New Name 2"
assert updated.name_sort_key == "pinned"