"""Services router tests.""" import pytest from fastapi.testclient import TestClient from pytest_mock import MockerFixture from contributor.connectors.apple_music import AppleArtist from contributor.connectors.spotify import SpotifyArtist @pytest.fixture def apple_artist(): return AppleArtist( identifier="123456", name="Test Apple Artist", genres=["Pop", "Rock"], url="https://music.apple.com/artist/123456", ) @pytest.fixture def spotify_artist(): return SpotifyArtist( identifier="4Z8W4fKeB5YxbusRsdQVPb", name="Test Spotify Artist", followers=1000, genres=["Pop", "Rock"], url="https://open.spotify.com/artist/4Z8W4fKeB5YxbusRsdQVPb", image="https://i.scdn.co/image/abc123", ) class TestGetAppleMusicArtistById: def test_returns_artist( self, mocker: MockerFixture, test_client: TestClient, apple_artist: AppleArtist, ) -> None: mocker.patch( "contributor.logic.service.apple_music.get_artist_by_id", return_value=apple_artist, ) response = test_client.get("/services/apple-music/artists/123456") assert response.status_code == 200 assert response.json() == apple_artist.model_dump() def test_passes_id_to_logic( self, mocker: MockerFixture, test_client: TestClient, apple_artist: AppleArtist, ) -> None: mock = mocker.patch( "contributor.logic.service.apple_music.get_artist_by_id", return_value=apple_artist, ) test_client.get("/services/apple-music/artists/AM_999") mock.assert_called_once_with(artist_id="AM_999") def test_returns_null_when_not_found( self, mocker: MockerFixture, test_client: TestClient, ) -> None: mocker.patch( "contributor.logic.service.apple_music.get_artist_by_id", return_value=None, ) response = test_client.get("/services/apple-music/artists/unknown") assert response.status_code == 200 assert response.json() is None class TestSearchAppleMusicArtists: def test_returns_artists( self, mocker: MockerFixture, test_client: TestClient, apple_artist: AppleArtist, ) -> None: mocker.patch( "contributor.logic.service.apple_music.artist_search", return_value=[apple_artist], ) response = test_client.get( "/services/apple-music/artists/search", params={"query": "test", "limit": 10, "offset": 0, "localization": "en"}, ) assert response.status_code == 200 assert response.json() == [apple_artist.model_dump()] def test_passes_params_to_logic( self, mocker: MockerFixture, test_client: TestClient, ) -> None: mock = mocker.patch( "contributor.logic.service.apple_music.artist_search", return_value=[], ) test_client.get( "/services/apple-music/artists/search", params={"query": "drake", "limit": 5, "offset": 10, "localization": "fr"}, ) mock.assert_called_once_with( query="drake", limit=5, offset=10, localization="fr", ) def test_returns_empty_list( self, mocker: MockerFixture, test_client: TestClient, ) -> None: mocker.patch( "contributor.logic.service.apple_music.artist_search", return_value=[], ) response = test_client.get( "/services/apple-music/artists/search", params={ "query": "nonexistent", "limit": 10, "offset": 0, "localization": None, }, ) assert response.status_code == 200 assert response.json() == [] class TestGetSpotifyArtistById: def test_returns_artist( self, mocker: MockerFixture, test_client: TestClient, spotify_artist: SpotifyArtist, ) -> None: mocker.patch( "contributor.logic.service.spotify.get_artist_by_id", return_value=spotify_artist, ) response = test_client.get("/services/spotify/artists/4Z8W4fKeB5YxbusRsdQVPb") assert response.status_code == 200 assert response.json() == spotify_artist.model_dump() def test_passes_id_to_logic( self, mocker: MockerFixture, test_client: TestClient, spotify_artist: SpotifyArtist, ) -> None: mock = mocker.patch( "contributor.logic.service.spotify.get_artist_by_id", return_value=spotify_artist, ) test_client.get("/services/spotify/artists/SP_999") mock.assert_called_once_with(artist_id="SP_999") def test_returns_null_when_not_found( self, mocker: MockerFixture, test_client: TestClient, ) -> None: mocker.patch( "contributor.logic.service.spotify.get_artist_by_id", return_value=None, ) response = test_client.get("/services/spotify/artists/unknown") assert response.status_code == 200 assert response.json() is None class TestSearchSpotifyArtists: def test_returns_artists( self, mocker: MockerFixture, test_client: TestClient, spotify_artist: SpotifyArtist, ) -> None: mocker.patch( "contributor.logic.service.spotify.artist_search", return_value=[spotify_artist], ) response = test_client.get( "/services/spotify/artists/search", params={"query": "test", "limit": 10, "offset": 0}, ) assert response.status_code == 200 assert response.json() == [spotify_artist.model_dump()] def test_passes_params_to_logic( self, mocker: MockerFixture, test_client: TestClient, ) -> None: mock = mocker.patch( "contributor.logic.service.spotify.artist_search", return_value=[], ) test_client.get( "/services/spotify/artists/search", params={"query": "drake", "limit": 5, "offset": 10}, ) mock.assert_called_once_with( query="drake", limit=5, offset=10, ) def test_returns_empty_list( self, mocker: MockerFixture, test_client: TestClient, ) -> None: mocker.patch( "contributor.logic.service.spotify.artist_search", return_value=[], ) response = test_client.get( "/services/spotify/artists/search", params={"query": "nonexistent", "limit": 10, "offset": 0}, ) assert response.status_code == 200 assert response.json() == []