Files
FabledCurator/tests/test_api_gallery.py
T
bvandeusen e05e0b9f37
CI / lint (push) Successful in 3s
CI / frontend-build (push) Successful in 20s
CI / backend-lint-and-test (push) Successful in 24s
CI / intimp (push) Successful in 3m51s
CI / intapi (push) Successful in 7m47s
CI / intcore (push) Successful in 8m19s
perf(gallery): materialize indexed effective_date sort key
The gallery cursored on COALESCE(post.post_date, image_record.created_at)
across the Post outer join — an expression spanning two tables that no
index can serve, so every /scroll sorted a large slice of the library
(and the old frontend fired ten serially). Materialize it:

- image_record.effective_date column + ix_image_record_effective_date
  (effective_date DESC, id DESC); alembic 0035 backfills
  COALESCE(primary post's post_date, created_at) for existing rows.
- gallery_service._effective_date_col() now returns the column, so scroll
  / timeline / jump / neighbors all order off the index instead of
  re-deriving the COALESCE. _neighbors reads record.effective_date
  directly (drops an extra Post lookup).
- importer._apply_sidecar maintains it: when a primary post with a date is
  linked, effective_date = post.post_date; plain inserts keep the
  created_at-equivalent server default.

Tests: sidecar import asserts effective_date == post.post_date; gallery
ordering/timeline/jump test seeds set effective_date alongside created_at.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-03 22:58:46 -04:00

62 lines
1.7 KiB
Python

from datetime import UTC, datetime, timedelta
import pytest
from backend.app.models import ImageRecord
pytestmark = pytest.mark.integration
async def _seed(db, count: int = 3):
base = datetime.now(UTC)
for i in range(count):
r = ImageRecord(
path=f"/images/x/{i}.jpg",
sha256=f"x{i:063d}",
size_bytes=1, mime="image/jpeg", width=1, height=1,
origin="imported_filesystem", integrity_status="unknown",
)
r.created_at = base - timedelta(minutes=i)
r.effective_date = r.created_at # no post → tracks created_at (0035)
db.add(r)
await db.flush()
await db.commit()
@pytest.mark.asyncio
async def test_scroll_returns_images(client, db):
await _seed(db)
resp = await client.get("/api/gallery/scroll?limit=10")
assert resp.status_code == 200
body = await resp.get_json()
assert len(body["images"]) == 3
assert "next_cursor" in body
assert "date_groups" in body
@pytest.mark.asyncio
async def test_scroll_rejects_bad_limit(client):
resp = await client.get("/api/gallery/scroll?limit=notanumber")
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_timeline_endpoint(client, db):
await _seed(db)
resp = await client.get("/api/gallery/timeline")
assert resp.status_code == 200
body = await resp.get_json()
assert sum(b["count"] for b in body) == 3
@pytest.mark.asyncio
async def test_jump_requires_year_month(client):
resp = await client.get("/api/gallery/jump")
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_image_detail_404_when_missing(client):
resp = await client.get("/api/gallery/image/99999")
assert resp.status_code == 404