Files
romm/backend/tests/handler/database/test_roms_released_days.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

125 lines
4.7 KiB
Python

"""What the `released_days` filter selects (issue #3440).
The filter means exactly what it says: the days asked for, in every year up to
`released_before_year`. The calendar policy that the Home anniversary widget
layers on top (skipping 1 January, excluding the current year, rolling 29
February onto 28 February) lives in the client, so nothing here reads a clock.
"""
from collections.abc import Sequence
from datetime import date, datetime, timezone
from tests.factories import make_rom
from handler.database import db_rom_handler
from models.platform import Platform
from models.rom import Rom
def _dated_rom(platform: Platform, name: str, released: date | None) -> Rom:
"""Seed a rom whose `generated_first_release_date` lands on `released`.
The generated column is derived from the provider blobs, and IGDB reports
seconds, so that is what goes in (0098 multiplies it up to milliseconds).
"""
rom = make_rom(platform, name)
if released is None:
return rom
seconds = int(
datetime(
released.year, released.month, released.day, tzinfo=timezone.utc
).timestamp()
)
return db_rom_handler.update_rom(
rom.id, {"igdb_metadata": {"first_release_date": str(seconds)}}
)
def _names(
days: Sequence[tuple[int, int]], before_year: int | None = None
) -> list[str]:
roms = db_rom_handler.get_roms_scalar(
released_days=days,
released_before_year=before_year,
order_by="first_release_date",
)
return [rom.name or "" for rom in roms]
class TestMatching:
def test_matches_the_same_day_across_years(self, platform: Platform):
_dated_rom(platform, "same_day_1995", date(1995, 9, 8))
_dated_rom(platform, "same_day_2004", date(2004, 9, 8))
_dated_rom(platform, "other_day", date(1995, 9, 9))
assert _names([(9, 8)]) == ["same_day_1995", "same_day_2004"]
def test_orders_oldest_release_first(self, platform: Platform):
_dated_rom(platform, "newest", date(2010, 9, 8))
_dated_rom(platform, "oldest", date(1985, 9, 8))
_dated_rom(platform, "middle", date(1999, 9, 8))
assert _names([(9, 8)]) == ["oldest", "middle", "newest"]
def test_undated_roms_never_match(self, platform: Platform):
_dated_rom(platform, "undated", None)
assert _names([(9, 8)]) == []
def test_no_days_leaves_the_query_alone(self, platform: Platform):
"""An absent filter must not narrow anything, the way every other one behaves."""
_dated_rom(platform, "dated", date(1995, 9, 8))
_dated_rom(platform, "undated", None)
assert sorted(_names([])) == ["dated", "undated"]
def test_january_first_is_an_ordinary_day_here(self, platform: Platform):
"""The widget skips it, the filter does not: it is not the filter's business."""
_dated_rom(platform, "year_only", date(1983, 1, 1))
assert _names([(1, 1)]) == ["year_only"]
def test_a_day_no_year_has_matches_nothing(self, platform: Platform):
_dated_rom(platform, "end_of_february", date(1994, 2, 28))
assert _names([(2, 30)]) == []
class TestMultipleDays:
def test_matches_the_union_of_the_days(self, platform: Platform):
"""The widget asks for 28 and 29 February together in a non-leap year."""
_dated_rom(platform, "leap_baby", date(2004, 2, 29))
_dated_rom(platform, "end_of_february", date(1998, 2, 28))
_dated_rom(platform, "march", date(1998, 3, 1))
assert _names([(2, 28), (2, 29)]) == ["end_of_february", "leap_baby"]
def test_29_february_alone_only_matches_leap_years(self, platform: Platform):
_dated_rom(platform, "leap_baby", date(2004, 2, 29))
_dated_rom(platform, "end_of_february", date(1998, 2, 28))
assert _names([(2, 29)]) == ["leap_baby"]
class TestYearBound:
def test_the_bound_year_is_excluded(self, platform: Platform):
"""The widget passes its own year, so "today" is never its own anniversary."""
_dated_rom(platform, "released_this_year", date(2026, 9, 8))
_dated_rom(platform, "released_before", date(2025, 9, 8))
assert _names([(9, 8)], before_year=2026) == ["released_before"]
def test_without_a_bound_every_year_matches(self, platform: Platform):
_dated_rom(platform, "released_this_year", date(2026, 9, 8))
_dated_rom(platform, "released_before", date(2025, 9, 8))
assert _names([(9, 8)]) == ["released_before", "released_this_year"]
def test_the_year_boundary_does_not_bleed(self, platform: Platform):
_dated_rom(platform, "new_years_eve", date(1999, 12, 31))
_dated_rom(platform, "new_years_day", date(2000, 1, 1))
assert _names([(12, 31)]) == ["new_years_eve"]
assert _names([(1, 1)]) == ["new_years_day"]