Files
romm/backend/tests/handler/database/test_states_handler.py
Sam Dornan 79fbce6beb test(backend): build test rows through shared factories and enforce them with ruff (#4972)
* 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>
2026-09-30 14:01:56 -05:00

197 lines
6.9 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 tests.factories import make_state
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."""
make_state(
rom,
admin_user,
"state1.state",
emulator="emulator1",
file_path=f"{platform.slug}/states/emulator1",
file_size_bytes=100,
)
make_state(
rom,
admin_user,
"state2.state",
emulator="emulator2",
file_path=f"{platform.slug}/states/emulator2",
file_size_bytes=200,
)
# 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."""
make_state(
rom,
admin_user,
"state_emu1.state",
emulator="emulator1",
file_path=f"{platform.slug}/states/emulator1",
file_size_bytes=100,
)
make_state(
rom,
admin_user,
"state_emu2.state",
emulator="emulator2",
file_path=f"{platform.slug}/states/emulator2",
file_size_bytes=200,
)
# 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]