mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
`GET /api/roms` already does what `/roms/anniversaries` hand-rolled: it selects ordered ids over the filtered set, takes the total from that index, hydrates only the page asked for, and re-projects the rows into id order. So the widget now asks it for a page rather than owning an endpoint that pulls the whole day. `released_days` on the list endpoint takes days as "M-D", matching any year, and `released_before_year` bounds them. Both go through `filter_roms`, so the same day-of-year match is available to the gallery filters and to smart-collection criteria, and the range union stays where it was: still sargable, still a covering index range scan on `idx_roms_generated_first_release_date`, no SQL date function and no dialect branching. Verified on a 73k-rom database that the plan through the list endpoint's join is unchanged: `type=range`, `Using where; Using index`, no filesort. The filter means exactly what it says, so the calendar policy moved to the client, where the clock it depends on already lived. `anniversaryQuery` picks the days and the year bound off the viewer's own date: it skips 1 January, where several providers park year-only metadata, rolls 29 February onto 28 February in a non-leap year, and bounds the years at the viewer's own. The server no longer has to second-guess a timezone offset, so the 31 December asked for on 1 January UTC correction is gone rather than fixed. Consequences for the widget: the counter is the day's real total instead of one clipped at a 500-row ceiling that no longer exists, and a card costs one page of 24 rather than up to 4 MB up front. Paging backwards stays in memory; only moving past the loaded pages fetches, with the spinner on the arrow so focus is never yanked mid-page. A failed page keeps the card on screen. `released_days` is added to the list endpoint's unscoped-cache gate: the sidecar keys encode no filters, so a narrowed page must not be served under the shared "all" key. Covered by a test that primes the unscoped key first. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
"""Tests for the day-of-year predicate behind the `released_days` filter (issue #3440).
|
|
|
|
`generated_first_release_date` is epoch milliseconds, so "8 September in any year" is a
|
|
union of one-day ranges rather than a `MONTH()/DAY()` call. Ranges are sargable, which is
|
|
the whole point: they let the column's index serve a range scan instead of a scan of every
|
|
row. These tests pin the range arithmetic; the endpoint tests pin what it selects.
|
|
"""
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from utils.database import (
|
|
EARLIEST_RELEASE_YEAR,
|
|
LATEST_RELEASE_YEAR,
|
|
MS_PER_DAY,
|
|
day_of_year_ranges,
|
|
release_day_ranges,
|
|
)
|
|
|
|
|
|
def _ms(year: int, month: int, day: int) -> int:
|
|
return int(datetime(year, month, day, tzinfo=timezone.utc).timestamp() * 1000)
|
|
|
|
|
|
def test_covers_every_year_below_the_bound():
|
|
ranges = day_of_year_ranges(9, 8, before_year=2026)
|
|
|
|
assert len(ranges) == 2026 - EARLIEST_RELEASE_YEAR
|
|
assert (_ms(1958, 9, 8), _ms(1958, 9, 9)) in ranges
|
|
assert (_ms(2025, 9, 8), _ms(2025, 9, 9)) in ranges
|
|
|
|
|
|
def test_each_range_spans_exactly_one_day():
|
|
assert all(
|
|
end - start == MS_PER_DAY
|
|
for start, end in day_of_year_ranges(9, 8, before_year=2026)
|
|
)
|
|
|
|
|
|
def test_ranges_are_sorted_ascending():
|
|
"""Emitted in key order so the index range scan needs no sort on top of it."""
|
|
ranges = day_of_year_ranges(2, 28, before_year=2026)
|
|
|
|
assert ranges == sorted(ranges)
|
|
|
|
|
|
def test_the_bound_year_is_excluded():
|
|
"""An anniversary is at least a year old, so the current year is never in the union."""
|
|
ranges = day_of_year_ranges(9, 8, before_year=2026)
|
|
|
|
assert (_ms(2026, 9, 8), _ms(2026, 9, 9)) not in ranges
|
|
assert max(end for _, end in ranges) == _ms(2025, 9, 9)
|
|
|
|
|
|
def test_releases_before_the_epoch_get_negative_ranges():
|
|
"""Video games predate 1970, and providers report those releases as negative epochs."""
|
|
start, end = day_of_year_ranges(9, 8, before_year=2026)[0]
|
|
|
|
assert start < 0
|
|
assert end < 0
|
|
|
|
|
|
def test_the_year_boundary_does_not_bleed():
|
|
"""31 December's ranges must stop before 1 January, and vice versa."""
|
|
dec = dict(day_of_year_ranges(12, 31, before_year=2026))
|
|
jan = dict(day_of_year_ranges(1, 1, before_year=2026))
|
|
|
|
assert dec[_ms(1999, 12, 31)] == _ms(2000, 1, 1)
|
|
assert _ms(2000, 1, 1) in jan
|
|
assert not set(dec) & set(jan)
|
|
|
|
|
|
def test_29_february_only_matches_leap_years():
|
|
starts = {start for start, _ in day_of_year_ranges(2, 29, before_year=2026)}
|
|
|
|
assert _ms(2024, 2, 29) in starts
|
|
assert _ms(2023, 2, 28) not in starts
|
|
assert len(starts) == len(range(1960, 2025, 4))
|
|
|
|
|
|
@pytest.mark.parametrize(("month", "day"), [(2, 30), (4, 31), (6, 31)])
|
|
def test_a_day_no_year_has_yields_no_ranges(month: int, day: int):
|
|
assert day_of_year_ranges(month, day, before_year=2026) == []
|
|
|
|
|
|
class TestReleaseDayRanges:
|
|
"""The union the filter hands to the index, over one or more days."""
|
|
|
|
def test_merges_days_in_key_order(self):
|
|
"""Two days concatenated would interleave; the index wants one ascending run."""
|
|
ranges = release_day_ranges([(2, 29), (2, 28)], before_year=2026)
|
|
|
|
assert ranges == sorted(ranges)
|
|
assert set(ranges) == set(day_of_year_ranges(2, 28, before_year=2026)) | set(
|
|
day_of_year_ranges(2, 29, before_year=2026)
|
|
)
|
|
|
|
def test_a_single_day_matches_the_underlying_ranges(self):
|
|
assert release_day_ranges([(9, 8)], before_year=2026) == day_of_year_ranges(
|
|
9, 8, before_year=2026
|
|
)
|
|
|
|
def test_no_days_yields_no_ranges(self):
|
|
assert release_day_ranges([], before_year=2026) == []
|
|
|
|
def test_an_unbounded_caller_still_gets_a_finite_union(self):
|
|
"""`epoch_ms_in_ranges` cannot express "any year", so the bound is capped."""
|
|
ranges = release_day_ranges([(9, 8)])
|
|
|
|
assert len(ranges) == LATEST_RELEASE_YEAR - EARLIEST_RELEASE_YEAR
|
|
assert max(end for _, end in ranges) == _ms(LATEST_RELEASE_YEAR - 1, 9, 9)
|