"""Unit tests for Spotify album ID parsing logic.""" import pytest from product_staging.logic.spotify import ( parse_spotify_album_id, parse_spotify_artist_id, ) class TestParseSpotifyAlbumId: """Tests for parse_spotify_album_id.""" def test_raw_id(self) -> None: assert ( parse_spotify_album_id("5b7UrNnuckYDfCp6ZW0Sji") == "5b7UrNnuckYDfCp6ZW0Sji" ) def test_spotify_uri(self) -> None: assert ( parse_spotify_album_id("spotify:album:5b7UrNnuckYDfCp6ZW0Sji") == "5b7UrNnuckYDfCp6ZW0Sji" ) def test_spotify_url(self) -> None: assert ( parse_spotify_album_id( "https://open.spotify.com/album/5b7UrNnuckYDfCp6ZW0Sji" ) == "5b7UrNnuckYDfCp6ZW0Sji" ) def test_spotify_url_with_query_string(self) -> None: assert ( parse_spotify_album_id( "https://open.spotify.com/album/5b7UrNnuckYDfCp6ZW0Sji?si=YYOn1Oe6RNmeNjT4ga1s8g" ) == "5b7UrNnuckYDfCp6ZW0Sji" ) def test_spotify_url_http(self) -> None: assert ( parse_spotify_album_id( "http://open.spotify.com/album/5b7UrNnuckYDfCp6ZW0Sji" ) == "5b7UrNnuckYDfCp6ZW0Sji" ) def test_whitespace_trimmed(self) -> None: assert ( parse_spotify_album_id(" 5b7UrNnuckYDfCp6ZW0Sji ") == "5b7UrNnuckYDfCp6ZW0Sji" ) def test_invalid_id_raises(self) -> None: with pytest.raises(ValueError, match="Could not extract Spotify album ID"): parse_spotify_album_id("not-a-valid-id") def test_invalid_uri_raises(self) -> None: with pytest.raises(ValueError, match="Invalid Spotify album ID in URI"): parse_spotify_album_id("spotify:album:bad") def test_invalid_url_raises(self) -> None: with pytest.raises(ValueError, match="Invalid Spotify album URL"): parse_spotify_album_id( "https://open.spotify.com/artist/5b7UrNnuckYDfCp6ZW0Sji" ) def test_empty_string_raises(self) -> None: with pytest.raises(ValueError): parse_spotify_album_id("") class TestParseSpotifyArtistId: """Tests for parse_spotify_artist_id.""" def test_raw_id(self) -> None: assert ( parse_spotify_artist_id("4QkSD9TRUnMtI8Fq1jXJJe") == "4QkSD9TRUnMtI8Fq1jXJJe" ) def test_spotify_uri(self) -> None: assert ( parse_spotify_artist_id("spotify:artist:4QkSD9TRUnMtI8Fq1jXJJe") == "4QkSD9TRUnMtI8Fq1jXJJe" ) def test_spotify_url(self) -> None: assert ( parse_spotify_artist_id( "https://open.spotify.com/artist/4QkSD9TRUnMtI8Fq1jXJJe" ) == "4QkSD9TRUnMtI8Fq1jXJJe" ) def test_spotify_url_with_query_string(self) -> None: assert ( parse_spotify_artist_id( "https://open.spotify.com/artist/4QkSD9TRUnMtI8Fq1jXJJe?si=YYOn1Oe6RNmeNjT4ga1s8g" ) == "4QkSD9TRUnMtI8Fq1jXJJe" ) def test_spotify_url_http(self) -> None: assert ( parse_spotify_artist_id( "http://open.spotify.com/artist/4QkSD9TRUnMtI8Fq1jXJJe" ) == "4QkSD9TRUnMtI8Fq1jXJJe" ) def test_whitespace_trimmed(self) -> None: assert ( parse_spotify_artist_id(" 4QkSD9TRUnMtI8Fq1jXJJe ") == "4QkSD9TRUnMtI8Fq1jXJJe" ) def test_invalid_id_raises(self) -> None: with pytest.raises(ValueError, match="Could not extract Spotify artist ID"): parse_spotify_artist_id("not-a-valid-id") def test_invalid_uri_raises(self) -> None: with pytest.raises(ValueError, match="Invalid Spotify artist ID in URI"): parse_spotify_artist_id("spotify:artist:bad") def test_invalid_url_raises(self) -> None: with pytest.raises(ValueError, match="Invalid Spotify artist URL"): parse_spotify_artist_id( "https://open.spotify.com/album/4QkSD9TRUnMtI8Fq1jXJJe" ) def test_empty_string_raises(self) -> None: with pytest.raises(ValueError): parse_spotify_artist_id("")