Files
FabledCurator/tests/test_gallery_artist_payload.py
T
2026-05-18 20:15:07 -04:00

95 lines
2.5 KiB
Python

from datetime import UTC, datetime, timedelta
import pytest
from backend.app import create_app
from backend.app.models import (
Artist,
ImageProvenance,
ImageRecord,
Post,
Source,
)
pytestmark = pytest.mark.integration
@pytest.fixture
async def app():
return create_app()
@pytest.fixture
async def client(app):
async with app.test_client() as c:
yield c
async def _img(db, n):
rec = ImageRecord(
path=f"/images/ap/{n}.jpg", sha256=f"ap{n:062d}",
size_bytes=1, mime="image/jpeg", width=1, height=1,
origin="imported_filesystem", integrity_status="unknown",
)
rec.created_at = datetime.now(UTC) - timedelta(minutes=n)
db.add(rec)
await db.flush()
return rec
async def _artist_source(db, name, slug):
a = Artist(name=name, slug=slug)
db.add(a)
await db.flush()
s = Source(artist_id=a.id, platform="patreon",
url=f"https://patreon.test/{slug}")
db.add(s)
await db.flush()
return a, s
@pytest.mark.asyncio
async def test_scroll_artist_via_primary_post(client, db):
rec = await _img(db, 1)
_, s = await _artist_source(db, "Alice", "alice")
post = Post(source_id=s.id, external_post_id="1")
db.add(post)
await db.flush()
rec.primary_post_id = post.id
await db.flush()
await db.commit()
resp = await client.get("/api/gallery/scroll?limit=10")
assert resp.status_code == 200
body = await resp.get_json()
item = next(i for i in body["images"] if i["id"] == rec.id)
assert item["artist"] == {"name": "Alice", "slug": "alice"}
@pytest.mark.asyncio
async def test_scroll_artist_via_provenance_fallback(client, db):
rec = await _img(db, 1)
_, s = await _artist_source(db, "Bob", "bob")
post = Post(source_id=s.id, external_post_id="2")
db.add(post)
await db.flush()
db.add(ImageProvenance(image_record_id=rec.id, post_id=post.id,
source_id=s.id))
await db.flush()
await db.commit() # rec.primary_post_id stays NULL
resp = await client.get("/api/gallery/scroll?limit=10")
body = await resp.get_json()
item = next(i for i in body["images"] if i["id"] == rec.id)
assert item["artist"] == {"name": "Bob", "slug": "bob"}
@pytest.mark.asyncio
async def test_scroll_artist_null_when_none(client, db):
rec = await _img(db, 1)
await db.commit()
resp = await client.get("/api/gallery/scroll?limit=10")
body = await resp.get_json()
item = next(i for i in body["images"] if i["id"] == rec.id)
assert item["artist"] is None