mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
The saves handler carried two ROM filters that ANDed together, and the states handler had only the singular one, so the same "which ROMs" question had three answers across two sibling handlers. Collapse both onto rom_ids: a single-ROM lookup is just a one-element scope, which is what the three internal callers in the saves endpoint now pass. That leaves the routes to fold their own public rom_id param into the scope. Rather than write the fold twice, narrow_rom_id_scope joins utils/validation beside RomIdScope, keeping the documented "combined with rom_id" semantics: both filters still apply together, so the effective scope is their intersection. GET /api/states gains the rom_ids param GET /api/saves got in #4304, declared with the same shared RomIdScope, so a client syncing a known set of ROMs can discover states the same way it discovers saves, under the same cap and the same 422 on a bad scope. One behaviour shift falls out of the handler's old falsy check: rom_id=0 used to be ignored and return the whole library, and now scopes to nothing, which is what RomIdScope's positive-ID rule says a zero means. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
213 lines
7.6 KiB
Python
213 lines
7.6 KiB
Python
"""
|
|
Unit tests for DBStatesHandler platform filtering functionality.
|
|
|
|
This module tests the platform filtering fixes for DBStatesHandler to ensure
|
|
it properly filters by platform_id through the Rom relationship.
|
|
"""
|
|
|
|
from handler.database import db_state_handler
|
|
from models.assets import State
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
from models.user import User
|
|
|
|
|
|
class TestDBStatesHandlerPlatformFiltering:
|
|
"""Test suite for platform filtering in DBStatesHandler."""
|
|
|
|
def test_get_states_without_platform_filter(self, admin_user: User, state: State):
|
|
"""Test that get_states returns all states when no platform filter is applied."""
|
|
states = db_state_handler.get_states(user_id=admin_user.id)
|
|
|
|
assert len(states) >= 1
|
|
state_ids = [state.id for state in states]
|
|
assert state.id in state_ids
|
|
|
|
def test_get_states_with_platform_filter(
|
|
self, admin_user: User, platform: Platform, state: State
|
|
):
|
|
"""Test that get_states filters correctly by platform_id."""
|
|
states = db_state_handler.get_states(
|
|
user_id=admin_user.id, platform_id=platform.id
|
|
)
|
|
|
|
assert len(states) == 1
|
|
assert states[0].id == state.id
|
|
assert states[0].file_name == "test_state.state"
|
|
|
|
def test_get_states_with_rom_ids_and_platform_filter(
|
|
self, admin_user: User, platform: Platform, rom: Rom, state: State
|
|
):
|
|
"""Test that get_states works with both rom_ids and platform_id filters."""
|
|
states = db_state_handler.get_states(
|
|
user_id=admin_user.id, rom_ids=[rom.id], platform_id=platform.id
|
|
)
|
|
|
|
assert len(states) == 1
|
|
assert states[0].id == state.id
|
|
|
|
def test_get_states_with_nonexistent_platform(self, admin_user: User, state: State):
|
|
"""Test that get_states returns empty list for nonexistent platform."""
|
|
states = db_state_handler.get_states(user_id=admin_user.id, platform_id=999)
|
|
|
|
assert len(states) == 0
|
|
|
|
def test_platform_filtering_relationship_integrity(
|
|
self, admin_user: User, platform: Platform, rom: Rom, state: State
|
|
):
|
|
"""Test that platform filtering correctly uses the Rom relationship."""
|
|
assert rom.platform_id == platform.id
|
|
|
|
states_platform = db_state_handler.get_states(
|
|
user_id=admin_user.id, platform_id=platform.id
|
|
)
|
|
|
|
assert len(states_platform) == 1
|
|
|
|
def test_multiple_states_same_platform(
|
|
self, admin_user: User, platform: Platform, rom: Rom
|
|
):
|
|
"""Test filtering with multiple states on the same platform."""
|
|
state1 = State(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="state1.state",
|
|
file_name_no_tags="state1",
|
|
file_name_no_ext="state1",
|
|
file_extension="state",
|
|
emulator="emulator1",
|
|
file_path=f"{platform.slug}/states/emulator1",
|
|
file_size_bytes=100,
|
|
)
|
|
state2 = State(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="state2.state",
|
|
file_name_no_tags="state2",
|
|
file_name_no_ext="state2",
|
|
file_extension="state",
|
|
emulator="emulator2",
|
|
file_path=f"{platform.slug}/states/emulator2",
|
|
file_size_bytes=200,
|
|
)
|
|
|
|
db_state_handler.add_state(state1)
|
|
db_state_handler.add_state(state2)
|
|
|
|
# Filter by platform should return both states
|
|
states = db_state_handler.get_states(
|
|
user_id=admin_user.id, platform_id=platform.id
|
|
)
|
|
|
|
assert len(states) == 2
|
|
state_names = [state.file_name for state in states]
|
|
assert "state1.state" in state_names
|
|
assert "state2.state" in state_names
|
|
|
|
def test_get_state_by_filename_with_platform_filter(
|
|
self, admin_user: User, rom: Rom, state: State
|
|
):
|
|
"""Test that get_state_by_filename works correctly with platform filtering."""
|
|
retrieved_state = db_state_handler.get_state_by_filename(
|
|
user_id=admin_user.id, rom_id=rom.id, file_name=state.file_name
|
|
)
|
|
|
|
assert retrieved_state is not None
|
|
assert retrieved_state.id == state.id
|
|
assert retrieved_state.file_name == state.file_name
|
|
|
|
def test_platform_filtering_with_different_emulators(
|
|
self, admin_user: User, platform: Platform, rom: Rom
|
|
):
|
|
"""Test platform filtering with states from different emulators."""
|
|
state_emulator1 = State(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="state_emu1.state",
|
|
file_name_no_tags="state_emu1",
|
|
file_name_no_ext="state_emu1",
|
|
file_extension="state",
|
|
emulator="emulator1",
|
|
file_path=f"{platform.slug}/states/emulator1",
|
|
file_size_bytes=100,
|
|
)
|
|
state_emulator2 = State(
|
|
rom_id=rom.id,
|
|
user_id=admin_user.id,
|
|
file_name="state_emu2.state",
|
|
file_name_no_tags="state_emu2",
|
|
file_name_no_ext="state_emu2",
|
|
file_extension="state",
|
|
emulator="emulator2",
|
|
file_path=f"{platform.slug}/states/emulator2",
|
|
file_size_bytes=200,
|
|
)
|
|
|
|
db_state_handler.add_state(state_emulator1)
|
|
db_state_handler.add_state(state_emulator2)
|
|
|
|
# Filter by platform should return both states regardless of emulator
|
|
states = db_state_handler.get_states(
|
|
user_id=admin_user.id, platform_id=platform.id
|
|
)
|
|
|
|
assert len(states) == 2
|
|
emulators = [state.emulator for state in states]
|
|
assert "emulator1" in emulators
|
|
assert "emulator2" in emulators
|
|
|
|
def test_get_state_by_id_with_platform_context(
|
|
self, admin_user: User, platform: Platform, state: State
|
|
):
|
|
"""Test that get_state works correctly and maintains platform context."""
|
|
retrieved_state = db_state_handler.get_state(user_id=admin_user.id, id=state.id)
|
|
|
|
assert retrieved_state is not None
|
|
assert retrieved_state.id == state.id
|
|
assert retrieved_state.file_name == "test_state.state"
|
|
|
|
# Verify the state is associated with the correct platform through ROM
|
|
assert retrieved_state.rom.platform_id == platform.id
|
|
|
|
|
|
class TestGetStatesRomIdsScope:
|
|
"""Test suite for the `rom_ids` scope on DBStatesHandler.get_states."""
|
|
|
|
def test_scopes_to_listed_roms(
|
|
self, admin_user: User, rom: Rom, state: State, second_state: State
|
|
):
|
|
states = db_state_handler.get_states(user_id=admin_user.id, rom_ids=[rom.id])
|
|
|
|
assert [s.id for s in states] == [state.id]
|
|
|
|
def test_scopes_to_multiple_roms(
|
|
self,
|
|
admin_user: User,
|
|
rom: Rom,
|
|
second_rom: Rom,
|
|
state: State,
|
|
second_state: State,
|
|
):
|
|
states = db_state_handler.get_states(
|
|
user_id=admin_user.id, rom_ids=[rom.id, second_rom.id]
|
|
)
|
|
|
|
assert {s.id for s in states} == {state.id, second_state.id}
|
|
|
|
def test_empty_scope_returns_nothing(self, admin_user: User, state: State):
|
|
assert db_state_handler.get_states(user_id=admin_user.id, rom_ids=[]) == []
|
|
|
|
def test_omitted_scope_returns_everything(self, admin_user: User, state: State):
|
|
states = db_state_handler.get_states(user_id=admin_user.id, rom_ids=None)
|
|
|
|
assert state.id in [s.id for s in states]
|
|
|
|
def test_combines_with_platform_filter(
|
|
self, admin_user: User, platform: Platform, rom: Rom, state: State
|
|
):
|
|
states = db_state_handler.get_states(
|
|
user_id=admin_user.id, rom_ids=[rom.id], platform_id=platform.id
|
|
)
|
|
|
|
assert [s.id for s in states] == [state.id]
|