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_store_customer from delivery_metadata.constants import StoreIds from delivery_metadata.exceptions import StoreNotFound from delivery_metadata.models.schemas import DeliveryMetadata @pytest_asyncio.fixture async def create_store_customer_table( delivery_metadata_mock: DeliveryMetadata, 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 customer_master_master")) await session.execute( text(""" INSERT INTO customer_master_master ( customer_master_master_id, customer_name, delivery_option, ddex_party_id, supports_localization, instant_grat, is_user_defined_contributors_supported ) VALUES ( :customer_master_master_id, :customer_name, :delivery_option, :ddex_party_id, :supports_localization, :instant_grat, :is_user_defined_contributors_supported ) """), [ { "customer_master_master_id": 1, "customer_name": "Just Can't Get Enough (Of This Music)", "delivery_option": "full", "ddex_party_id": "PAPIDA2011031503C", "supports_localization": "Y", "instant_grat": "Y", "is_user_defined_contributors_supported": "Y", }, { "customer_master_master_id": 2, "customer_name": "Never Let Me Frown Again Music", "delivery_option": "full", "ddex_party_id": "PAPIDA2016033003A", "supports_localization": "Y", "instant_grat": None, "is_user_defined_contributors_supported": "Y", }, { "customer_master_master_id": 3, "customer_name": "Down With The Synthness", "delivery_option": "cherry_pick", "ddex_party_id": "PAPIDA2008021899Y", "supports_localization": "Y", "instant_grat": None, "is_user_defined_contributors_supported": "Y", }, { "customer_master_master_id": 4, "customer_name": "Chop Bluesy!", "delivery_option": "full", "ddex_party_id": "PAPIDA2002091101C", "supports_localization": "Y", "instant_grat": None, "is_user_defined_contributors_supported": "Y", }, { "customer_master_master_id": 5, "customer_name": "Just Like Heaven (In Your Ears)", "delivery_option": "filtered_catalog", "ddex_party_id": "PAPIDA20091010431D", "supports_localization": "Y", "instant_grat": None, "is_user_defined_contributors_supported": "Y", }, { "customer_master_master_id": 6, "customer_name": "Private Eyes: Music Hall and Song Notes", "delivery_option": "full", "ddex_party_id": "PAPIDA19820802911C", "supports_localization": "Y", "instant_grat": None, "is_user_defined_contributors_supported": "N", }, { "customer_master_master_id": StoreIds.SPOTIFY, "customer_name": delivery_metadata_mock.delivery.store.recipient_ddex_party_name, "delivery_option": "full", "ddex_party_id": delivery_metadata_mock.delivery.store.recipient_ddex_party_id, "supports_localization": ( "Y" if delivery_metadata_mock.delivery.store.is_localization_supported else "N" ), "instant_grat": ( "Y" if delivery_metadata_mock.delivery.store.is_instant_gratification_supported else None ), "is_user_defined_contributors_supported": "Y", }, ], ) @pytest.mark.asyncio async def test_get_store_customer( create_store_customer_table: Coroutine[Any, Any, None], snapshot: SnapshotAssertion, ) -> None: result = await get_store_customer(StoreIds.SPOTIFY) assert result == snapshot @pytest.mark.asyncio async def test_get_store_customer_not_found( create_store_customer_table: Coroutine[Any, Any, None], ) -> None: with pytest.raises(StoreNotFound) as e: await get_store_customer(12321232123212321) assert str(e.value) == "No store found for store_id: 12321232123212321"