mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* 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>
401 lines
14 KiB
Python
401 lines
14 KiB
Python
"""Smart collection membership as a composed query rather than a stored list.
|
|
|
|
A smart collection is a saved filter, so serving a page of it must apply its
|
|
criteria to the ROM query instead of assembling the whole membership list in
|
|
memory and writing it back on every read (see #4029).
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
from typing import Any
|
|
|
|
from tests.factories import make_rom, make_save
|
|
|
|
from handler.database import db_collection_handler, db_rom_handler
|
|
from models.assets import Save
|
|
from models.collection import Collection, SmartCollection
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
|
|
def _add_rom(
|
|
platform: Platform,
|
|
name: str,
|
|
*,
|
|
cover: str = "",
|
|
manual_metadata: dict[str, Any] | None = None,
|
|
regions: list[str] | None = None,
|
|
) -> Rom:
|
|
return make_rom(
|
|
platform,
|
|
name,
|
|
fs_stem=name.lower().replace(" ", "_"),
|
|
manual_metadata=manual_metadata or {},
|
|
regions=regions or [],
|
|
path_cover_s=cover,
|
|
path_cover_l=cover,
|
|
)
|
|
|
|
|
|
def _add_smart_collection(
|
|
user: User,
|
|
criteria: dict[str, Any],
|
|
*,
|
|
name: str = "Smart",
|
|
is_public: bool = False,
|
|
) -> SmartCollection:
|
|
return db_collection_handler.add_smart_collection(
|
|
SmartCollection(
|
|
name=name,
|
|
description="",
|
|
user_id=user.id,
|
|
is_public=is_public,
|
|
filter_criteria=criteria,
|
|
)
|
|
)
|
|
|
|
|
|
def _favorite(user: User, roms: Sequence[Rom]) -> Collection:
|
|
collection = db_collection_handler.add_collection(
|
|
Collection(
|
|
name=f"Favorites {user.id}",
|
|
description="",
|
|
user_id=user.id,
|
|
is_favorite=True,
|
|
)
|
|
)
|
|
db_collection_handler.add_roms_to_collection(
|
|
collection.id, [rom.id for rom in roms]
|
|
)
|
|
return collection
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_applies_criteria(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
matching = [
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]}),
|
|
_add_rom(platform, "Rally Two", manual_metadata={"genres": ["Racing"]}),
|
|
]
|
|
_add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {rom.id for rom in matching}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_narrows_with_the_gallery_filters(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
keeper = _add_rom(
|
|
platform, "Rally Two", manual_metadata={"genres": ["Racing"]}, regions=["USA"]
|
|
)
|
|
_add_rom(
|
|
platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]}, regions=["USA"]
|
|
)
|
|
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id,
|
|
regions=["USA"],
|
|
user_id=admin_user.id,
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {keeper.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_keeps_both_search_terms(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# The collection's own search term and the gallery's are separate fulltext
|
|
# clauses in one statement; they must not share a bind parameter name.
|
|
match_both = _add_rom(platform, "Rally Champion")
|
|
_add_rom(platform, "Rally Junior")
|
|
_add_rom(platform, "Champion Cup")
|
|
|
|
smart_collection = _add_smart_collection(admin_user, {"search_term": "rally"})
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id,
|
|
search_term="champion",
|
|
user_id=admin_user.id,
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {match_both.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_resolves_per_viewer(
|
|
platform: Platform, admin_user: User, editor_user: User
|
|
):
|
|
# A public smart collection built on a per-user filter must answer for the
|
|
# viewer, not for whoever opened it last.
|
|
admin_pick = _add_rom(platform, "Admin Pick")
|
|
editor_pick = _add_rom(platform, "Editor Pick")
|
|
_favorite(admin_user, [admin_pick])
|
|
_favorite(editor_user, [editor_pick])
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"favorite": True}, is_public=True
|
|
)
|
|
|
|
admin_roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
editor_roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=editor_user.id
|
|
)
|
|
|
|
assert {rom.id for rom in admin_roms} == {admin_pick.id}
|
|
assert {rom.id for rom in editor_roms} == {editor_pick.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_scoped_to_a_regular_collection(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
inside = _add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
_add_rom(platform, "Rally Two", manual_metadata={"genres": ["Racing"]})
|
|
|
|
collection = db_collection_handler.add_collection(
|
|
Collection(name="Shelf", description="", user_id=admin_user.id)
|
|
)
|
|
db_collection_handler.add_roms_to_collection(collection.id, [inside.id])
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"genres": ["Racing"], "collection_id": collection.id}
|
|
)
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
|
|
assert {rom.id for rom in roms} == {inside.id}
|
|
|
|
|
|
def test_filter_roms_by_smart_collection_with_joined_criteria(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# `playable` joins platforms and `has_soundtrack` is a correlated column
|
|
# property; both have to survive being applied to a bare-column subquery.
|
|
rom = _add_rom(platform, "Rally One")
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"playable": False, "has_soundtrack": False, "missing": False}
|
|
)
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=admin_user.id
|
|
)
|
|
|
|
assert {result.id for result in roms} == {rom.id}
|
|
|
|
|
|
def test_filter_roms_by_unknown_smart_collection_returns_nothing(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
_add_rom(platform, "Rally One")
|
|
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=123456, user_id=admin_user.id
|
|
)
|
|
|
|
assert list(roms) == []
|
|
|
|
|
|
def test_unusable_criteria_match_nothing_rather_than_everything(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# `filter_criteria` is stored unvalidated, and honouring a row only in part
|
|
# would drop the constraint that makes it a narrower list than the library.
|
|
_add_rom(platform, "Rally One")
|
|
_add_rom(platform, "Rally Two")
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"platform_ids": [platform.id, "not-an-id"]}
|
|
)
|
|
|
|
members = db_collection_handler.get_smart_collection_members(
|
|
smart_collection, user_id=admin_user.id
|
|
)
|
|
|
|
assert list(members) == []
|
|
|
|
|
|
def test_refresh_smart_collection_updates_the_cached_columns(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
# The cached columns back the collections list and the ROM detail page.
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
assert smart_collection.rom_count == 0
|
|
|
|
roms = [
|
|
_add_rom(
|
|
platform,
|
|
f"Rally {index}",
|
|
cover=f"cover_{index}.png",
|
|
manual_metadata={"genres": ["Racing"]},
|
|
)
|
|
for index in range(3)
|
|
]
|
|
|
|
refreshed = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
|
|
assert refreshed is not None
|
|
assert refreshed.rom_count == 3
|
|
assert set(refreshed.rom_ids) == {rom.id for rom in roms}
|
|
assert len(refreshed.path_covers_small) == 3
|
|
assert len(refreshed.path_covers_large) == 3
|
|
|
|
|
|
def test_refresh_smart_collection_is_a_noop_when_nothing_moved(
|
|
platform: Platform, admin_user: User, mocker
|
|
):
|
|
# Rewriting an unchanged collection would bump `updated_at`, the `?ts=`
|
|
# cover cache-buster that clients also sync on.
|
|
smart_collection = _add_smart_collection(admin_user, {"genres": ["Racing"]})
|
|
_add_rom(
|
|
platform,
|
|
"Rally One",
|
|
cover="cover_0.png",
|
|
manual_metadata={"genres": ["Racing"]},
|
|
)
|
|
first = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
assert first is not None
|
|
assert len(first.path_covers_small) == 1
|
|
|
|
update = mocker.spy(db_collection_handler, "update_smart_collection")
|
|
second = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
|
|
assert update.call_count == 0
|
|
assert second is not None
|
|
assert second.path_covers_small == first.path_covers_small
|
|
|
|
|
|
def test_refresh_smart_collections_covers_every_collection(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
racing = _add_smart_collection(admin_user, {"genres": ["Racing"]}, name="Racing")
|
|
puzzle = _add_smart_collection(admin_user, {"genres": ["Puzzle"]}, name="Puzzle")
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
_add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
_add_rom(platform, "Puzzler Two", manual_metadata={"genres": ["Puzzle"]})
|
|
|
|
db_collection_handler.refresh_smart_collections()
|
|
|
|
refreshed_racing = db_collection_handler.get_smart_collection(racing.id)
|
|
refreshed_puzzle = db_collection_handler.get_smart_collection(puzzle.id)
|
|
assert refreshed_racing is not None and refreshed_racing.rom_count == 1
|
|
assert refreshed_puzzle is not None and refreshed_puzzle.rom_count == 2
|
|
|
|
|
|
def test_refresh_for_roms_updates_the_collections_the_rom_moved_between(
|
|
platform: Platform, admin_user: User
|
|
):
|
|
racing = _add_smart_collection(admin_user, {"genres": ["Racing"]}, name="Racing")
|
|
puzzle = _add_smart_collection(admin_user, {"genres": ["Puzzle"]}, name="Puzzle")
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
mover = _add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
db_collection_handler.refresh_smart_collections()
|
|
|
|
db_rom_handler.update_rom(mover.id, {"manual_metadata": {"genres": ["Racing"]}})
|
|
db_collection_handler.refresh_smart_collections_for_roms([mover.id])
|
|
|
|
racing_after = db_collection_handler.get_smart_collection(racing.id)
|
|
puzzle_after = db_collection_handler.get_smart_collection(puzzle.id)
|
|
assert racing_after is not None and racing_after.rom_count == 2
|
|
assert puzzle_after is not None and puzzle_after.rom_count == 0
|
|
|
|
|
|
def test_refresh_for_roms_leaves_untouched_collections_alone(
|
|
platform: Platform, admin_user: User, mocker
|
|
):
|
|
# Recounting every collection for one edited ROM would scan the library
|
|
# once per collection, which is the cost this issue is about.
|
|
_add_smart_collection(admin_user, {"genres": ["Racing"]}, name="Racing")
|
|
_add_rom(platform, "Rally One", manual_metadata={"genres": ["Racing"]})
|
|
db_collection_handler.refresh_smart_collections()
|
|
bystander = _add_rom(platform, "Puzzler", manual_metadata={"genres": ["Puzzle"]})
|
|
|
|
refresh = mocker.spy(db_collection_handler, "refresh_smart_collection")
|
|
db_collection_handler.refresh_smart_collections_for_roms([bystander.id])
|
|
|
|
assert refresh.call_count == 0
|
|
|
|
|
|
def _add_save(rom: Rom, user: User, name: str = "save.sav") -> Save:
|
|
return make_save(
|
|
rom,
|
|
user,
|
|
name,
|
|
file_name_no_tags=name,
|
|
file_name_no_ext=name,
|
|
emulator="test_emulator",
|
|
file_path="test/saves",
|
|
file_size_bytes=1.0,
|
|
)
|
|
|
|
|
|
def test_refresh_for_roms_follows_per_user_state(platform: Platform, admin_user: User):
|
|
# A save is not a ROM edit, but it moves anything filtering on `has_saves`.
|
|
collection = _add_smart_collection(admin_user, {"has_saves": True})
|
|
rom = _add_rom(platform, "Rally One")
|
|
db_collection_handler.refresh_smart_collections()
|
|
seeded = db_collection_handler.get_smart_collection(collection.id)
|
|
assert seeded is not None and seeded.rom_count == 0
|
|
|
|
_add_save(rom, admin_user)
|
|
db_collection_handler.refresh_smart_collections_for_roms(
|
|
[rom.id], membership_only=True
|
|
)
|
|
|
|
after = db_collection_handler.get_smart_collection(collection.id)
|
|
assert after is not None
|
|
assert after.rom_ids == [rom.id]
|
|
|
|
|
|
def test_refresh_for_roms_membership_only_skips_a_member_that_stayed(
|
|
platform: Platform, admin_user: User, mocker
|
|
):
|
|
# Autosaves land constantly. Once the ROM is already a member, another save
|
|
# changes nothing about the collection, so it must not force a recount.
|
|
_add_smart_collection(admin_user, {"has_saves": True})
|
|
rom = _add_rom(platform, "Rally One")
|
|
_add_save(rom, admin_user, "first.sav")
|
|
db_collection_handler.refresh_smart_collections()
|
|
|
|
_add_save(rom, admin_user, "second.sav")
|
|
refresh = mocker.spy(db_collection_handler, "refresh_smart_collection")
|
|
db_collection_handler.refresh_smart_collections_for_roms(
|
|
[rom.id], membership_only=True
|
|
)
|
|
|
|
assert refresh.call_count == 0
|
|
|
|
|
|
def test_cached_membership_belongs_to_the_owner(
|
|
platform: Platform, admin_user: User, editor_user: User
|
|
):
|
|
# The cached columns live on the shared row, so they must describe the
|
|
# owner's view of a per-user filter rather than the last viewer's.
|
|
admin_pick = _add_rom(platform, "Admin Pick")
|
|
editor_pick = _add_rom(platform, "Editor Pick")
|
|
_favorite(admin_user, [admin_pick])
|
|
_favorite(editor_user, [editor_pick])
|
|
|
|
smart_collection = _add_smart_collection(
|
|
admin_user, {"favorite": True}, is_public=True
|
|
)
|
|
|
|
db_rom_handler.get_roms_scalar(
|
|
smart_collection_id=smart_collection.id, user_id=editor_user.id
|
|
)
|
|
refreshed = db_collection_handler.refresh_smart_collection(smart_collection.id)
|
|
|
|
assert refreshed is not None
|
|
assert set(refreshed.rom_ids) == {admin_pick.id}
|