def test__add_artist_genres__multiple_albums_multiple_genres(self): mock_artist_1 = mock_artist(spotify_id="mock-artist-id-1") mock_artist_2 = mock_artist(spotify_id="mock-artist-id-2") mock_album_1 = mock_album(genres=[], artists=[mock_artist_1], spotify_id="mock-album-id-1") mock_album_2 = mock_album(genres=[], artists=[mock_artist_2], spotify_id="mock-album-id-2") mock_albums = [mock_album_1, mock_album_2] def mock_get_artist_genres(artist): if artist == mock_artist_1: return ["jazz"] elif artist == mock_artist_2: return ["rock", "prog rock"] raise ValueError("unexpected artist ID") self.mock_spotify.get_artist_genres = MagicMock( side_effect=mock_get_artist_genres) albums_with_genres = self.music_util._add_artist_genres(mock_albums) self.assertEqual(albums_with_genres[mock_album_1].genres, ["jazz"]) self.assertEqual(sorted(albums_with_genres[mock_album_2].genres), ["prog rock", "rock"])
def test_get_num_diff_artists__diff_ids__counts_both(self): albums = [ mock_album(artists=[mock_artist(spotify_id="123")]), mock_album(artists=[mock_artist(spotify_id="456")]), ] num = self.music_util.get_num_diff_artists(albums) self.assertEqual(2, num)
def test_get_num_diff_artists__duplicate__counts_once(self): albums = [ mock_album(artists=[mock_artist(spotify_id="123")]), mock_album(artists=[mock_artist(spotify_id="123")]), ] num = self.music_util.get_num_diff_artists(albums) self.assertEqual(1, num)
def test_get_albums_as_readable_list__multiple_artists(self): artist1, artist2 = mock_artist(name="mock artist 1"), mock_artist( name="mock artist 2") album = mock_album(artists=[artist1, artist2], name="mock album") albums = [album] albums_as_readable_str = self.music_util.get_albums_as_readable_list( albums) self.assertEqual("- mock album by mock artist 1, mock artist 2", albums_as_readable_str)
def test_get_most_popular_artist__multiple_artists(self): artists = [ mock_artist(name="jimmy reed", popularity=1), mock_artist(name="howlin' wolf", popularity=3), mock_artist(name="junior wells", popularity=2), ] most_popular_artist = self.music_util.get_most_popular_artist(artists) self.assertEqual("howlin' wolf", most_popular_artist.name) self.assertEqual(3, most_popular_artist.popularity)
def test_get_albums_as_readable_list__one_album(self): artist = mock_artist(name="mock artist") album = mock_album(artists=[artist], name="mock album") albums = [album] albums_as_readable_str = self.music_util.get_albums_as_readable_list( albums) self.assertEqual("- mock album by mock artist", albums_as_readable_str)
def test_get_artist_genres(self): artist = mock_artist(spotify_id="3nFkdlSjzX9mRTtwJOzDYB") # Jay-Z artist_genres = self.spotify.get_artist_genres(artist) self.assertEqual( sorted(['east coast hip hop', 'hip hop', 'rap']), sorted(artist_genres), )
def test_get_artist_ids(self): artist = mock_artist(spotify_id="mock-id-123") mock_artists = [artist] mock_tracks = [mock_track(artists=mock_artists)] self.mock_spotify.get_playlist = MagicMock(return_value=mock_playlist( tracks=mock_tracks)) artists = self.music_util.get_artists("mock_playlist_id") self.assertEqual([artist], artists)
def test__add_artist_genres__single_album_single_genre(self): self.mock_spotify.get_artist_genres = MagicMock(return_value=["jazz"]) mock_album1 = mock_album( genres=[], artists=[mock_artist(spotify_id="mock-artist-id")], spotify_id="mock-album-id") mock_albums = [mock_album1] albums_with_genres = self.music_util._add_artist_genres(mock_albums) self.assertEqual(albums_with_genres[mock_album1].genres, ["jazz"])
def test_get_albums_as_readable_list__multiple_albums(self): artist = mock_artist(name="mock artist") album1 = mock_album(artists=[artist], name="mock album 1") album2 = mock_album(artists=[artist], name="mock album 2") albums = [album1, album2] albums_as_readable_str = self.music_util.get_albums_as_readable_list( albums) self.assertEqual( "- mock album 1 by mock artist\n- mock album 2 by mock artist", albums_as_readable_str)
def test_get_genres_by_album(self): bob_dylan = mock_artist(spotify_id="bob-dylan-id") bob_dylan_album = mock_album(spotify_id="bob-dylan-album-id", artists=[bob_dylan]) johnny_cash = mock_artist(spotify_id="johnny-cash-id") johnny_cash_album = mock_album(spotify_id="johnny-cash-album-id", artists=[johnny_cash]) mock_albums = [bob_dylan_album, johnny_cash_album] self.mock_spotify.get_albums = MagicMock(return_value=mock_albums) def mock_get_artist_genres(artist): if artist == bob_dylan: return ["folk rock"] elif artist == johnny_cash: return ["country folk"] raise ValueError("unexpected artist ID") self.mock_spotify.get_artist_genres = MagicMock( side_effect=mock_get_artist_genres) genres_by_album = self.music_util.get_genres_by_album(mock_albums) self.assertEqual(["folk rock"], genres_by_album[bob_dylan_album]) self.assertEqual(["country folk"], genres_by_album[johnny_cash_album])
def test_get_num_diff_artists__single__counts(self): albums = [mock_album(artists=[mock_artist()])] num = self.music_util.get_num_diff_artists(albums) self.assertEqual(1, num)
def test_get_most_popular_artist__single_artist(self): artists = [mock_artist(name="jimmy reed")] most_popular_artist = self.music_util.get_most_popular_artist(artists) self.assertEqual("jimmy reed", most_popular_artist.name)
def test_get_artist_albums(self): artist = mock_artist(spotify_id="74ASZWbe4lXaubB36ztrGX") # Bob Dylan albums = self.spotify.get_artist_albums(artist) self.assertIn("Rough and Rowdy Ways", [album.name for album in albums])