Files
FabledCurator/tests/test_gallery_service.py
T
bvandeusen 22bc24b6b6 fix(fc2a): align CI with FabledRulebook — lint + short unit tests only
The CI failure resolving 'postgres' hostname was the symptom; the cause is
that the workflow violated FabledRulebook/forgejo.md's "CI philosophy —
lint + short unit tests only" rule. Integration tests against a real
Postgres are supposed to run locally via docker-compose, not in CI.

Changes:
- Marked 8 DB-dependent test files with @pytest.mark.integration:
  test_tag_service, test_importer, test_gallery_service, test_api_gallery,
  test_api_tags, test_api_settings, test_api_import_admin, test_maintenance.
- CI workflow drops the postgres/redis service containers and the alembic
  upgrade smoke step entirely.
- Pytest invocation in CI changes to `pytest -v -m "not integration"`.
- Added pytest marker registration to pyproject.toml.
- DB_PASSWORD and SECRET_KEY env vars retained because config.py reads
  them at import time even though unit tests don't actually use them
  (set to placeholder values).

What CI now runs:
- ruff check
- pytest on the 6 unit test files: test_slug, test_paths,
  test_migration_0002, test_thumbnailer, test_celery_smoke,
  test_tasks_register.
- npm install + npm run build

What CI no longer runs:
- alembic upgrade (no live DB)
- the 8 integration test files (these run locally via docker-compose)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 15:23:07 -04:00

125 lines
3.4 KiB
Python

from datetime import UTC, datetime, timedelta
import pytest
from backend.app.models import ImageRecord, Tag, TagKind
from backend.app.models.tag import image_tag
from backend.app.services.gallery_service import (
GalleryService,
decode_cursor,
encode_cursor,
)
pytestmark = pytest.mark.integration
def _now():
return datetime.now(UTC)
async def _seed_images(db, count: int, sha_prefix: str = "0") -> list[ImageRecord]:
base = _now()
records = []
for i in range(count):
r = ImageRecord(
path=f"/images/test/{i}.jpg",
sha256=f"{sha_prefix}{i:063d}",
size_bytes=1000,
mime="image/jpeg",
width=100,
height=100,
origin="imported_filesystem",
integrity_status="unknown",
)
r.created_at = base - timedelta(minutes=i)
db.add(r)
records.append(r)
await db.flush()
return records
def test_cursor_roundtrip():
ts = datetime(2026, 5, 14, 12, 30, 0, tzinfo=UTC)
encoded = encode_cursor(ts, 42)
back_ts, back_id = decode_cursor(encoded)
assert back_ts == ts
assert back_id == 42
def test_decode_invalid_cursor_raises():
with pytest.raises(ValueError):
decode_cursor("not-base64!!!")
@pytest.mark.asyncio
async def test_scroll_returns_newest_first(db):
await _seed_images(db, 5)
svc = GalleryService(db)
page = await svc.scroll(cursor=None, limit=10)
assert len(page.images) == 5
assert page.images[0].created_at > page.images[-1].created_at
@pytest.mark.asyncio
async def test_scroll_pagination(db):
await _seed_images(db, 5)
svc = GalleryService(db)
first = await svc.scroll(cursor=None, limit=2)
assert len(first.images) == 2
assert first.next_cursor is not None
second = await svc.scroll(cursor=first.next_cursor, limit=2)
assert len(second.images) == 2
assert {i.id for i in first.images}.isdisjoint({i.id for i in second.images})
@pytest.mark.asyncio
async def test_scroll_with_tag_filter(db):
images = await _seed_images(db, 5)
tag = Tag(name="filterme", kind=TagKind.general)
db.add(tag)
await db.flush()
await db.execute(
image_tag.insert().values(
image_record_id=images[0].id, tag_id=tag.id, source="manual"
)
)
svc = GalleryService(db)
page = await svc.scroll(cursor=None, limit=10, tag_id=tag.id)
assert len(page.images) == 1
assert page.images[0].id == images[0].id
@pytest.mark.asyncio
async def test_timeline_groups_by_month(db):
await _seed_images(db, 3)
svc = GalleryService(db)
buckets = await svc.timeline()
assert sum(b.count for b in buckets) == 3
@pytest.mark.asyncio
async def test_date_groups_in_page(db):
await _seed_images(db, 3)
svc = GalleryService(db)
page = await svc.scroll(cursor=None, limit=10)
assert len(page.date_groups) == 1
y, m, ids = page.date_groups[0]
assert len(ids) == 3
@pytest.mark.asyncio
async def test_neighbors(db):
images = await _seed_images(db, 3)
svc = GalleryService(db)
middle = images[1]
payload = await svc.get_image_with_tags(middle.id)
assert payload["neighbors"]["prev_id"] == images[0].id
assert payload["neighbors"]["next_id"] == images[2].id
@pytest.mark.asyncio
async def test_get_image_with_tags_returns_none_for_missing(db):
svc = GalleryService(db)
assert await svc.get_image_with_tags(99999) is None