"""Global participants router tests.""" import pytest from fastapi.testclient import TestClient from pytest_mock import MockerFixture @pytest.fixture def global_participant_uuid(): return "b1a2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d" @pytest.fixture def unknown_uuid(): return "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee" @pytest.fixture def global_participant(global_participant_uuid): return { "id": global_participant_uuid, "name": "Test Artist Global", "spotify_id": "4iV5W9uYEdYUVa79Axb7Rh", "apple_music_id": "1234567890", "chartmetric_id": "987654", "image_url": "https://example.com/artists/test-artist.jpg", "monthly_listeners": 7654321, } class TestGetGlobalParticipant: def test_returns_global_participant( self, mocker: MockerFixture, test_client: TestClient, global_participant_uuid, global_participant, ) -> None: mocker.patch( "contributor.logic.global_participants.get_global_participant", return_value=global_participant, ) response = test_client.get(f"/global-participants/{global_participant_uuid}") assert response.status_code == 200 assert response.json() == global_participant def test_returns_404_when_not_found( self, mocker: MockerFixture, test_client: TestClient, unknown_uuid, ) -> None: mocker.patch( "contributor.logic.global_participants.get_global_participant", return_value=None, ) response = test_client.get(f"/global-participants/{unknown_uuid}") assert response.status_code == 404 class TestGetContributorsForGlobalParticipant: def test_returns_contributors( self, mocker: MockerFixture, test_client: TestClient, global_participant_uuid, ) -> None: contributors = [ { "id": 42, "uuid": "550e8400-e29b-41d4-a716-446655440000", "name": "Test Artist", "spotify_id": "3TVXtAsR1Inumwj472S9r4", "apple_music_id": None, "spotify_artist_key": None, "isni": None, "company_brand_uuid": "22222222-3333-4aaa-8bbb-cccccccccccc", "parent_company_uuid": "33333333-4444-4ddd-8eee-ffffffffffff", "artist_info_ids": [], "global_participant": None, "label": { "uuid": "cccc0000-dddd-4eee-8fff-aaaaaaaaaaaa", "vendor_id": 100, "subaccount_id": 0, }, } ] mocker.patch( "contributor.logic.global_participants.get_contributors_for_global_participant", return_value=contributors, ) response = test_client.get( f"/global-participants/{global_participant_uuid}/contributors" ) assert response.status_code == 200 assert response.json() == { "global_participant": {"id": global_participant_uuid}, "contributors": contributors, } def test_returns_404_when_global_participant_not_found( self, mocker: MockerFixture, test_client: TestClient, unknown_uuid, ) -> None: mocker.patch( "contributor.logic.global_participants.get_contributors_for_global_participant", return_value=None, ) response = test_client.get(f"/global-participants/{unknown_uuid}/contributors") assert response.status_code == 404 class TestLoadGlobalParticipants: def test_returns_global_participants( self, mocker: MockerFixture, test_client: TestClient, global_participant_uuid, global_participant, ) -> None: mocker.patch( "contributor.logic.global_participants.get_global_participants", return_value=[global_participant], ) response = test_client.post( "/global-participants/dataloader", json={"global_participants": [{"id": global_participant_uuid}]}, ) assert response.status_code == 200 assert response.json() == [global_participant] def test_returns_empty_list_when_none_found( self, mocker: MockerFixture, test_client: TestClient, global_participant_uuid, ) -> None: mocker.patch( "contributor.logic.global_participants.get_global_participants", return_value=[], ) response = test_client.post( "/global-participants/dataloader", json={"global_participants": [{"id": global_participant_uuid}]}, ) assert response.status_code == 200 assert response.json() == [] def test_returns_multiple_global_participants( self, mocker: MockerFixture, test_client: TestClient, global_participant, ) -> None: second_gp = { "id": "c2b3d4e5-f6a7-5b8c-9d0e-1f2a3b4c5d6e", "name": "Second Artist", "spotify_id": "5jW5X9uYEdYUVa79Axb7Rh", "apple_music_id": "0987654321", "chartmetric_id": "654321", "image_url": "https://example.com/artists/second-artist.jpg", "monthly_listeners": 5432100, } mocker.patch( "contributor.logic.global_participants.get_global_participants", return_value=[global_participant, second_gp], ) response = test_client.post( "/global-participants/dataloader", json={ "global_participants": [ {"id": global_participant["id"]}, {"id": second_gp["id"]}, ] }, ) assert response.status_code == 200 assert response.json() == [global_participant, second_gp]