3f30327fa5
Phase 1 backend for the gallery filter bar. Extends scroll/timeline/jump
from a single mutually-exclusive filter to a composable one:
- tag_ids: image must carry ALL of them (one correlated EXISTS per tag —
AND, no row multiplication), replacing the single-tag JOIN.
- artist_id composes with tags; media_type ('image'|'video') narrows by
mime; post_id stays the exclusive post-detail path.
- sort ('newest'|'oldest') flips the effective_date/id cursor comparison
and ordering; the cursor value is unchanged (direction comes from the
request). jump_cursor honors sort too.
- Shared _apply_scope helper applied across scroll/timeline/jump so the
timeline sidebar reflects the filtered set. API _parse_filters parses
tag_id (comma list), artist_id, media, sort.
Tests: multi-tag AND, media filter, sort reversal (service + API);
post_id-excludes-others; single tag_id back-compat.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
80 lines
2.4 KiB
Python
80 lines
2.4 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
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_scroll_sort_param_reverses(client, db):
|
|
await _seed(db, 3)
|
|
newest = await (await client.get("/api/gallery/scroll?limit=10&sort=newest")).get_json()
|
|
oldest = await (await client.get("/api/gallery/scroll?limit=10&sort=oldest")).get_json()
|
|
ids_new = [i["id"] for i in newest["images"]]
|
|
ids_old = [i["id"] for i in oldest["images"]]
|
|
assert ids_old == list(reversed(ids_new))
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_scroll_media_param(client, db):
|
|
await _seed(db, 2) # only image/jpeg seeded
|
|
resp = await client.get("/api/gallery/scroll?limit=10&media=video")
|
|
assert resp.status_code == 200
|
|
assert (await resp.get_json())["images"] == []
|