from typing import Any, Coroutine import pytest import pytest_asyncio from fastapi.testclient import TestClient from sqlalchemy.sql import text from syrupy.assertion import SnapshotAssertion from delivery_metadata.api.app import app from delivery_metadata.clients.art_relations import get_release_artists from delivery_metadata.exceptions import NoArtistForRelease @pytest_asyncio.fixture async def create_release_artist_tables(test_client: TestClient) -> None: async with app.state.art_relations_connector.db_session( transaction=True, turn_off_foreign_key_checks=True ) as session: await session.execute(text("TRUNCATE TABLE release_artist")) await session.execute( text(""" INSERT INTO release_artist ( release_artist_id, upc, role, release_id, artist_name ) VALUES ( :release_artist_id, :upc, :role, :release_id, :artist_name ) """), [ { "release_artist_id": 1, "upc": 123456789, "role": "performer", "release_id": 5678910, "artist_name": "John Doe", }, { "release_artist_id": 2, "upc": 123456788, "role": "performer", "release_id": 5678911, "artist_name": "Jane Doe", }, { "release_artist_id": 3, "upc": 75679660923, "role": "performer", "release_id": 4953147, "artist_name": "Very Big Star", }, { "release_artist_id": 6, "upc": 75679660923, "role": "featuring", "release_id": 4953147, "artist_name": "Smaller Star", }, { "release_artist_id": 8, "upc": 75679660923, "role": "feature_to_primary", "release_id": 4953147, "artist_name": "Smaller Star", }, { "release_artist_id": 9, "upc": 75679660923, "role": "feature_to_primary", "release_id": 4953147, "artist_name": "Missing featuring role", }, { "release_artist_id": 4, "upc": 75679660923, "role": "producer", "release_id": 4953147, "artist_name": "Big Time Producer", }, { "release_artist_id": 5, "upc": 75679660923, "role": "writer", "release_id": 4953147, "artist_name": "Genius Writer", }, { "release_artist_id": 7, "upc": 75679660923, "role": "writer", "release_id": 4953147, "artist_name": "", }, { "release_artist_id": 10, "upc": 75679660923, "role": "feature_to_primary", "release_id": 4953147, "artist_name": "double featured", }, { "release_artist_id": 11, "upc": 75679660923, "role": "feature_to_primary", "release_id": 4953147, "artist_name": "double featured", }, ], ) @pytest.mark.asyncio async def test_get_release_artists( create_release_artist_tables: Coroutine[Any, Any, None], snapshot: SnapshotAssertion, ) -> None: result = await get_release_artists(4953147) assert result == snapshot @pytest.mark.asyncio async def test_get_no_release_artists( create_release_artist_tables: Coroutine[Any, Any, None], ) -> None: with pytest.raises(NoArtistForRelease): await get_release_artists(4953148)