Files
romm/backend/tests/endpoints/roms/test_random.py
Sam Dornan fd9a33bc35 fix(backend): return identifiers without building an ORM row per id (#4586)
* fix(backend): project the identifier endpoints onto the id column

The `/identifiers` routes for saves, states, firmware, platforms, smart
collections and rom notes each asked their handler for entities with
`only_fields=[Model.id]`. `load_only` narrows the selected columns but does
not cancel an eager relationship, so a `lazy="joined"` join still fired and a
full entity was still built for every row. The platforms route was the worst
of the six: `with_firmware` eager-loads every firmware row and its platform,
and `load_only` does not cancel that either.

Each handler grows a small query builder shared by its existing accessor and a
new `*_ids` accessor that projects onto the id column with
`with_only_columns`, which drops the entity and with it the eager joins. The
firmware accessor already applied `noload`, so its SQL is unchanged; it moves
to the projection only to stop building rows.

`/api/users/identifiers` and the two `/collections` callers that use more than
an id are left alone, because nothing eager fires for them.

The `executed_statements` fixture moves to `tests/conftest.py` so the new
endpoint tests can use it.

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

* Update backend/tests/endpoints/test_identifier_endpoints.py

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

* style(backend): drop the leading blank line black rejects

Removing the module docstring in the GitHub edit left an empty first line,
which is the only thing black wanted to change.

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

* test(backend): drive the note-identifier test through the endpoint

Greptile flagged the handler-level test for bypassing the route's auth,
404, and visibility checks. It now issues the request the frontend would
and asserts the id-only projection holds among the route's own reads.

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

* refactor(backend): fold the duplicate SQL-capture fixtures into the shared one

Step 3 of the PR-ready gauntlet (simplify). The shared
`executed_statements` fixture gains the two roms test files that still
declared byte-identical local copies, and the id-only assertions share one
statement parser instead of two.

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

* docs(backend): tighten the get_platform_ids docstring to one line

Step 4 of the PR-ready gauntlet (review-polish).

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

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
2026-09-17 11:13:07 -05:00

297 lines
9.6 KiB
Python

"""Tests for `GET /api/roms/random` (issue #4066).
The endpoint replaces the two-call "count the library, then fetch the row at a
random offset" dance the Home widget used to run. Paging to a random offset
costs more the bigger the library gets, so these tests pin both the behaviour
(a rom from the requested scope, every rom reachable) and the mechanism (no
offset walk, no full count).
"""
from collections import Counter
from unittest.mock import patch
import pytest
from fastapi import status
from fastapi.testclient import TestClient
from handler.database import (
db_collection_handler,
db_platform_handler,
db_rom_handler,
roms_handler,
)
from handler.database.base_handler import sync_session
from models.collection import Collection
from models.permission import HiddenEntity, PermEntity
from models.platform import Platform
from models.rom import Rom
from models.user import User
def _add_rom(platform: Platform, name: str) -> Rom:
return db_rom_handler.add_rom(
Rom(
platform_id=platform.id,
name=name,
slug=name,
fs_name=f"{name}.zip",
fs_name_no_tags=name,
fs_name_no_ext=name,
fs_extension="zip",
fs_path=f"{platform.slug}/roms",
)
)
@pytest.fixture
def other_platform() -> Platform:
return db_platform_handler.add_platform(
Platform(
name="other_platform",
slug="other_platform_slug",
fs_slug="other_platform_slug",
)
)
def test_get_random_rom_returns_a_rom(
client: TestClient, access_token: str, rom: Rom
) -> None:
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
body = response.json()
assert body is not None
assert body["id"] == rom.id
# The widget renders cover, platform and release year straight off the pick.
assert body["platform_slug"] == rom.platform_slug
def test_get_random_rom_requires_auth(client: TestClient) -> None:
response = client.get("/api/roms/random")
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_get_random_rom_without_roms_returns_null(
client: TestClient, access_token: str, platform: Platform
) -> None:
"""An empty library is not an error: the widget shows its own empty copy."""
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
assert response.json() is None
def test_get_random_rom_reaches_every_rom(
client: TestClient, access_token: str, rom: Rom, platform: Platform
) -> None:
"""Sampling must cover the whole set, not park on one end of it."""
ids = {rom.id} | {_add_rom(platform, f"rom_{i}").id for i in range(4)}
seen: Counter[int] = Counter()
for _ in range(120):
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
seen[response.json()["id"]] += 1
assert set(seen) == ids
def test_get_random_rom_scoped_to_platform(
client: TestClient,
access_token: str,
rom: Rom,
platform: Platform,
other_platform: Platform,
) -> None:
other_ids = {_add_rom(other_platform, f"other_{i}").id for i in range(3)}
for _ in range(20):
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
params={"platform_ids": [other_platform.id]},
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["id"] in other_ids
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
params={"platform_ids": [platform.id]},
)
assert response.json()["id"] == rom.id
def test_get_random_rom_scoped_to_collection(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
platform: Platform,
) -> None:
in_collection = _add_rom(platform, "in_collection")
_add_rom(platform, "out_of_collection")
collection = db_collection_handler.add_collection(
Collection(name="Picks", description="", user_id=admin_user.id)
)
db_collection_handler.add_roms_to_collection(collection.id, [in_collection.id])
for _ in range(10):
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
params={"collection_id": collection.id},
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["id"] == in_collection.id
def test_get_random_rom_scoped_to_empty_collection_returns_null(
client: TestClient, access_token: str, admin_user: User, rom: Rom
) -> None:
"""No sampled key can land in an empty scope, so this is the fallback's null."""
collection = db_collection_handler.add_collection(
Collection(name="Empty", description="", user_id=admin_user.id)
)
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
params={"collection_id": collection.id},
)
assert response.status_code == status.HTTP_200_OK
assert response.json() is None
def test_get_random_rom_skips_hidden_roms(
client: TestClient,
viewer_user: User,
viewer_access_token: str,
rom: Rom,
platform: Platform,
) -> None:
"""Admin-hidden roms are out of scope for the pick, like any other list."""
visible = _add_rom(platform, "visible_rom")
with sync_session.begin() as session:
session.add(
HiddenEntity(
entity=PermEntity.ROMS, entity_id=rom.id, user_id=viewer_user.id
)
)
for _ in range(30):
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {viewer_access_token}"},
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["id"] == visible.id
def test_get_random_rom_rechecks_visibility_after_fetching(
client: TestClient,
viewer_user: User,
viewer_access_token: str,
rom: Rom,
platform: Platform,
) -> None:
"""The pick is re-checked against the row that came back, not the filter.
The id is chosen by a filtered query but fetched by raw id, so a rom that
moved to a hidden platform in between would arrive hidden. Standing in for
that race by handing the endpoint a rom the filter would have excluded.
"""
# A visible rom so the pick resolves an id, otherwise the empty scope
# returns null on its own and the fetch is never reached.
_add_rom(platform, "visible_rom")
with sync_session.begin() as session:
session.add(
HiddenEntity(
entity=PermEntity.ROMS, entity_id=rom.id, user_id=viewer_user.id
)
)
with patch.object(db_rom_handler, "get_rom_simple", return_value=rom):
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {viewer_access_token}"},
)
assert response.status_code == status.HTTP_200_OK
# Null, not 404: a hidden rom stays indistinguishable from an empty scope.
assert response.json() is None
def test_get_random_rom_does_not_page_or_count(
client: TestClient,
access_token: str,
rom: Rom,
platform: Platform,
executed_statements: list[str],
) -> None:
"""The point of the endpoint: cost that doesn't grow with the library.
Both mechanisms it replaces walk the whole filtered set: an OFFSET steps
over every preceding row, and the total was its own count of the library.
Sampling primary keys does neither, and consecutively-numbered roms always
land it a hit.
"""
for i in range(5):
_add_rom(platform, f"rom_{i}")
executed_statements.clear()
with (
patch.object(
db_rom_handler, "get_rom_count", wraps=db_rom_handler.get_rom_count
) as get_rom_count,
patch.object(
db_rom_handler, "get_rom_id_index", wraps=db_rom_handler.get_rom_id_index
) as get_rom_id_index,
):
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
get_rom_count.assert_not_called()
get_rom_id_index.assert_not_called()
rom_queries = [sql for sql in executed_statements if " roms" in sql.lower()]
assert rom_queries, "expected the pick to query the roms table"
assert not [sql for sql in rom_queries if "offset" in sql.lower()]
def test_get_random_rom_falls_back_when_sampling_misses(
client: TestClient, access_token: str, rom: Rom, platform: Platform
) -> None:
"""A scope the sampled keys never hit still resolves a pick.
Sampling zero keys stands in for the real cases: a narrow filter, or an id
space left sparse by deletions.
"""
ids = {rom.id} | {_add_rom(platform, f"rom_{i}").id for i in range(4)}
seen: set[int] = set()
with patch.object(roms_handler, "RANDOM_ID_SAMPLE_SIZE", 0):
for _ in range(120):
response = client.get(
"/api/roms/random",
headers={"Authorization": f"Bearer {access_token}"},
)
assert response.status_code == status.HTTP_200_OK
seen.add(response.json()["id"])
# The fallback is a position in the set, so it reaches all of it too.
assert seen == ids