mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
* perf(backend): check rom-note visibility without the related load
The five `/api/roms/{id}/notes*` routes each opened with `get_rom(id)` to
reach two scalars: `id` for the 404 and `id` plus `platform_id` for
`assert_rom_visible`. `get_rom` runs the whole `with_details` eager load, so
every note read or write paid for the platform, files, metadata, saves,
states, screenshots, rom_users, siblings, collections and notes of the rom,
about 12 statements.
`DBRomsHandler.get_rom_visibility` selects those two columns and nothing
else, and `assert_rom_visible` now takes the `RomVisibility` row it returns
as well as a full `Rom`, so no other caller of the check changes.
Measured on a 3,000-rom library with 300 notes on the target rom, one admin
request:
GET /api/roms/{id}/notes/identifiers 17 statements / 30,059 bytes -> 5 / 8,066
POST /api/roms/{id}/notes 17 statements / 23,209 bytes -> 5 / 1,216
The server-side `Questions` delta per request falls from 33 to 21.
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>
* test(roms): match the rom lookup across dialects
The notes-load guard pinned the exact SQL text of the visibility lookup,
including MariaDB's `?` bind placeholder. psycopg renders the same query
as `%(id_1)s::INTEGER`, so the assertion could never match on the
PostgreSQL leg of the CI matrix and all three tests would have failed
there. Compare a dialect-independent prefix instead.
Step 2 of the pr-ready gauntlet: correctness fix plus the comment and
docstring trims it flagged.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* test: drop the second copy of the statement-capture fixture
`test_released_days.py` carried its own `captured_sql`, the same
before_cursor_execute listener the notes-load guard now shares as
`executed_statements` from tests/conftest.py. Point that file at the
shared fixture and drop the now-unused imports.
Step 3 of the pr-ready gauntlet: reuse and simplification fixes.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* test: name the notes auth helper as the other endpoint tests do
Seven endpoint test modules already define `_auth` for the bearer header;
this file introduced a third name for the same one-liner.
Step 4 of the pr-ready gauntlet: review-polish consistency pass, after
trunk fmt and the affected-suite run.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
110 lines
3.1 KiB
Python
110 lines
3.1 KiB
Python
"""The notes routes read a ROM's id and platform id, not the full `get_rom` load."""
|
|
|
|
from fastapi import status
|
|
from fastapi.testclient import TestClient
|
|
|
|
from handler.database import db_rom_handler
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
# The tables `get_rom` eager-loads, minus `rom_notes` and `platforms`, both of
|
|
# which the notes queries themselves read.
|
|
EAGER_TABLES = (
|
|
"rom_files",
|
|
"roms_metadata",
|
|
"saves",
|
|
"states",
|
|
"screenshots",
|
|
"rom_user",
|
|
"collections_roms",
|
|
"sibling_roms",
|
|
"track_meta",
|
|
"rom_file_doc_meta",
|
|
)
|
|
|
|
# Truncated at the bind placeholder: MariaDB renders `?` and psycopg
|
|
# `%(id_1)s::INTEGER`, and CI runs both.
|
|
ROM_LOOKUP = "SELECT roms.id, roms.platform_id FROM roms WHERE roms.id ="
|
|
|
|
|
|
def _auth(access_token: str) -> dict[str, str]:
|
|
return {"Authorization": f"Bearer {access_token}"}
|
|
|
|
|
|
def _flat(statements: list[str]) -> list[str]:
|
|
return [" ".join(statement.split()) for statement in statements]
|
|
|
|
|
|
def _reads(statements: list[str], table: str) -> list[str]:
|
|
return [s for s in _flat(statements) if f"FROM {table} " in s]
|
|
|
|
|
|
def _assert_narrow_rom_lookup(statements: list[str]) -> None:
|
|
flat = _flat(statements)
|
|
|
|
narrow = [s for s in flat if s.startswith(ROM_LOOKUP)]
|
|
assert len(narrow) == 1, flat
|
|
|
|
for table in EAGER_TABLES:
|
|
assert _reads(statements, table) == [], table
|
|
|
|
|
|
def test_note_identifiers_resolves_the_rom_without_the_related_load(
|
|
client: TestClient,
|
|
access_token: str,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
executed_statements: list[str],
|
|
) -> None:
|
|
db_rom_handler.create_rom_note(rom_id=rom.id, user_id=admin_user.id, title="one")
|
|
|
|
executed_statements.clear()
|
|
response = client.get(
|
|
f"/api/roms/{rom.id}/notes/identifiers", headers=_auth(access_token)
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
_assert_narrow_rom_lookup(executed_statements)
|
|
# The route's own note query is the only read; a full `Rom` adds a second.
|
|
assert len(_reads(executed_statements, "rom_notes")) == 1
|
|
|
|
|
|
def test_create_note_resolves_the_rom_without_the_related_load(
|
|
client: TestClient,
|
|
access_token: str,
|
|
rom: Rom,
|
|
executed_statements: list[str],
|
|
) -> None:
|
|
executed_statements.clear()
|
|
response = client.post(
|
|
f"/api/roms/{rom.id}/notes",
|
|
headers=_auth(access_token),
|
|
json={"title": "from the test"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
_assert_narrow_rom_lookup(executed_statements)
|
|
assert _reads(executed_statements, "rom_notes") == []
|
|
|
|
|
|
def test_update_note_resolves_the_rom_without_the_related_load(
|
|
client: TestClient,
|
|
access_token: str,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
executed_statements: list[str],
|
|
) -> None:
|
|
note = db_rom_handler.create_rom_note(
|
|
rom_id=rom.id, user_id=admin_user.id, title="before"
|
|
)
|
|
|
|
executed_statements.clear()
|
|
response = client.put(
|
|
f"/api/roms/{rom.id}/notes/{note['id']}",
|
|
headers=_auth(access_token),
|
|
json={"content": "after"},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
_assert_narrow_rom_lookup(executed_statements)
|