Files
romm/backend/tests/handler/database/test_roms_page_rows_query.py
Stu Anderson 0941b3033e feat(roms): load roms by an array of ids (#5663)
* feat(roms): load roms by an array of ids

GET /api/roms accepts up to 256 rom_ids and returns those roms in the
order given. Ids the caller can't see, or that don't exist, are left out.

Closes #5659

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* fix(roms): code-review pass on rom_ids window

Reuse RomIdScope (dedupe, shared 500 cap), skip sidecar and grouped page
cache reads for a window by id, report offset 0 and the id count as limit,
and rename the shadowed rom_ids local in _transform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(roms): simplify pass on rom_ids window

Return early for rom_ids before building the filtered query, the sidecars
or any cache key, through a shared _hydrate helper, so the paged path is
back to its prior shape. Tests use the headers fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(roms): review-polish pass, cover a hidden platform in the rom_ids window

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(collections): refuse malformed virtual collection ids, require page-row visibility

A malformed virtual_collection_id raised an uncaught decode error (500) on
the gallery, random and bulk download routes. It is now a 422 at the query
boundary, and GET /collections/virtual/{id} answers 404.

get_page_rows_query takes visibility as a required argument, since it is
the access check on caller-supplied rom ids.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(gallery): load scroll windows by rom id

Once the bootstrap holds the whole ordered id index, a window fetch sends
that slice as rom_ids, so the backend skips the filtered, offset-paged
query. Items are placed by id, since ids that went missing or hidden are
dropped from the response.

On a 100k-rom MariaDB library, a filtered grouped window went from
0.8-2.0 s to ~50 ms, and a deep ungrouped window from 106 to 50 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-20 11:12:57 -05:00

65 lines
2.1 KiB
Python

"""Loading a page's rows by id, without the filters or the sibling grouping."""
from typing import Any
from sqlalchemy import Select
from tests.conftest import engine # noqa: F401 (loads the test database setup)
from tests.sql_dialects import POSTGRESQL_DIALECT, compile_sql
from handler.auth.rom_visibility import UNRESTRICTED, RomVisibilityFilter
from handler.database import db_rom_handler
from handler.database.rom_filters import RomFilterParams
PAGE_IDS = [11, 22, 33]
def _sql(statement: Select[Any]) -> str:
return compile_sql(statement, POSTGRESQL_DIALECT, literal_binds=True).lower()
def test_the_grouped_query_does_rank_the_library():
"""Grouping puts a window over every rom, which the page-row tests below must not."""
query, sort_key = db_rom_handler.get_roms_query(order_by="name", user_id=1)
grouped = db_rom_handler.filter_roms(
query=query,
filters=RomFilterParams(group_by_meta_id=True),
sort_key=sort_key,
order_by="name",
user_id=1,
)
assert "row_number()" in _sql(grouped)
def test_loading_a_page_does_not_rank_the_library():
assert "row_number()" not in _sql(
db_rom_handler.get_page_rows_query(PAGE_IDS, visibility=UNRESTRICTED)
)
def test_loading_a_page_is_limited_to_its_ids():
assert "roms.id in (11, 22, 33)" in _sql(
db_rom_handler.get_page_rows_query(PAGE_IDS, visibility=UNRESTRICTED)
)
def test_loading_a_page_does_not_refilter_on_rom_user():
assert "rom_user" not in _sql(
db_rom_handler.get_page_rows_query(PAGE_IDS, visibility=UNRESTRICTED)
)
def test_loading_a_page_keeps_the_visibility_filter():
visibility = RomVisibilityFilter(hidden_platform_ids=frozenset({7}))
sql = _sql(db_rom_handler.get_page_rows_query(PAGE_IDS, visibility=visibility))
assert "roms.platform_id not in (7)" in sql
def test_loading_a_page_keeps_the_file_stats_the_serializer_reads():
assert "rom_files" in _sql(
db_rom_handler.get_page_rows_query(
PAGE_IDS, visibility=UNRESTRICTED, include_file_stats=True
)
)