diff --git a/backend/app/services/provenance_service.py b/backend/app/services/provenance_service.py index 52f7cd7..ac9d7da 100644 --- a/backend/app/services/provenance_service.py +++ b/backend/app/services/provenance_service.py @@ -65,3 +65,20 @@ class ProvenanceService: for ip, post, src, art in rows ], } + + async def for_post(self, post_id: int) -> dict | None: + stmt = ( + select(Post, Source, Artist) + .join(Source, Source.id == Post.source_id) + .join(Artist, Artist.id == Source.artist_id) + .where(Post.id == post_id) + ) + row = (await self.session.execute(stmt)).first() + if row is None: + return None + post, src, art = row + return { + "post": _post_dict(post), + "source": _source_dict(src), + "artist": _artist_dict(art), + } diff --git a/tests/test_provenance_service.py b/tests/test_provenance_service.py index 4f7987e..5670eb4 100644 --- a/tests/test_provenance_service.py +++ b/tests/test_provenance_service.py @@ -129,3 +129,27 @@ async def test_for_image_null_post_fields_serialize_null(db): assert e["post"]["title"] is None assert e["post"]["description_html"] is None assert e["post"]["attachment_count"] is None + + +@pytest.mark.asyncio +async def test_for_post_missing_returns_none(db): + svc = ProvenanceService(db) + assert await svc.for_post(999999) is None + + +@pytest.mark.asyncio +async def test_for_post_returns_post_source_artist(db): + artist, source, post = await _seed_post( + db, artist_name="Carol", slug="carol", platform="patreon", + ext_id="77", title="T", desc="

d

", count=3, + ) + svc = ProvenanceService(db) + payload = await svc.for_post(post.id) + assert payload["post"]["id"] == post.id + assert payload["post"]["title"] == "T" + assert payload["post"]["description_html"] == "

d

" + assert payload["post"]["attachment_count"] == 3 + assert payload["source"] == {"id": source.id, "platform": "patreon", + "url": source.url} + assert payload["artist"] == {"id": artist.id, "name": "Carol", + "slug": "carol"}