Exemplo n.º 1
0
 def test_generate_success(self):
     client_mock = mock.Mock()
     history = InMemoryTracksRepository()
     history.save_tracks([
         TrackRecord(id="history_track_a"),
         TrackRecord(id="history_track_b"),
         TrackRecord(id="history_track_c")
     ])
     input_tracks_collection = TracksCollection()
     input_tracks_collection.add_track(
         self._create_track(track_id="input_track_a"))
     input_tracks_collection.add_track(
         self._create_track(track_id="input_track_b"))
     input_tracks_collection.add_track(
         self._create_track(track_id="input_track_c"))
     client_mock.get_related_artists.side_effect = [
         [Artist(id="related_artist_a", name="related_artist_a")],
         [Artist(id="related_artist_b", name="related_artist_b")],
         [Artist(id="related_artist_b", name="related_artist_c")]
     ]
     client_mock.get_artist_top_tracks.side_effect = [
         TracksCollection().add_track(
             self._create_track(track_id="top_track_a")).add_track(
                 self._create_track(track_id="history_track_b")),
         TracksCollection().add_track(
             self._create_track(track_id="top_track_b")).add_track(
                 self._create_track(track_id="top_track_a")),
         TracksCollection().add_track(
             self._create_track(track_id="top_track_c"))
     ]
     traemplist = TraemplistGenerator(
         client=client_mock, history=history, logger=mock.Mock()).generate(
             input_tracks_collection=input_tracks_collection, size=100)
     self.assertEqual(
         traemplist,
         TracksCollection().add_track(
             self._create_track(track_id="top_track_a")).add_track(
                 self._create_track(track_id="top_track_b")).add_track(
                     self._create_track(track_id="top_track_c")))
     client_mock.get_related_artists.assert_has_calls([
         mock.call(artist_id="track_input_track_a_artist_id"),
         mock.call(artist_id="track_input_track_b_artist_id"),
         mock.call(artist_id="track_input_track_c_artist_id")
     ],
                                                      any_order=True)
     client_mock.get_artist_top_tracks.assert_has_calls([
         mock.call(artist_id="related_artist_a"),
         mock.call(artist_id="related_artist_b"),
         mock.call(artist_id="related_artist_b")
     ])
Exemplo n.º 2
0
 def test_remove_artist_tracks(self):
     track_a = self._create_test_track()
     track_b = self._create_test_track(artist=Artist(id="new", name="new"))
     collection = TracksCollection().add_track(track_a).add_track(track_b)
     collection.remove_artist_tracks(track_a.artist)
     self.assertFalse(collection.contains_artist_track(track_a.artist))
     self.assertTrue(collection.contains_artist_track(track_b.artist))
Exemplo n.º 3
0
 def _create_track(track_id: str) -> Track:
     return Track(id=track_id,
                  name=f"track_{track_id}_name",
                  artist=Artist(
                      id=f"track_{track_id}_artist_id",
                      name=f"track_{track_id}_artist_name",
                  ))
Exemplo n.º 4
0
 def _create_test_track(artist: Optional[Artist] = None) -> Track:
     track_id = str(uuid4())
     if not artist:
         artist = Artist(id="artist", name="Artist")
     return Track(id=track_id, name=track_id, artist=artist)
Exemplo n.º 5
0
 def test_contains_artist_track(self):
     track = self._create_test_track()
     collection = TracksCollection().add_track(track)
     self.assertTrue(collection.contains_artist_track(track.artist))
     self.assertFalse(
         collection.contains_artist_track(Artist(id="new", name="new")))
Exemplo n.º 6
0
class SpotifyClientTest(TestCase):

    ACCESS_TOKEN = "test_access_token"
    ARTIST_RESPONSE_DATA = {"id": "artist_id", "name": "artist_name"}
    ARTIST_RESPONSE_OBJECT = Artist(id="artist_id", name="artist_name")
    TRACK_RESPONSE_DATA = {
        "id": "track_id",
        "name": "track_name",
        "artists": [ARTIST_RESPONSE_DATA]
    }
    TRACK_RESPONSE_OBJECT = Track(id="track_id",
                                  name="track_name",
                                  artist=ARTIST_RESPONSE_OBJECT)

    def setUp(self) -> None:
        self.token_provider = mock.Mock()
        self.token_provider.get_access_token.return_value = self.ACCESS_TOKEN
        self.client = SpotifyClient(access_token_provider=self.token_provider)

    def test_get_user_playlist_ids_success(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_playlists.side_effect = [{
                "items": [{
                    "id": str(i)
                } for i in range(50)]
            }, {
                "items": [{
                    "id": str(i)
                } for i in range(50, 60)]
            }]
            self.assertEqual(list(self.client.get_user_playlist_ids()),
                             [str(i) for i in range(60)])
            client_instance_mock.current_user_playlists.assert_has_calls([
                mock.call(limit=SpotifyClient.GET_USER_PLAYLIST_LIMIT,
                          offset=0),
                mock.call(limit=SpotifyClient.GET_USER_PLAYLIST_LIMIT,
                          offset=SpotifyClient.GET_USER_PLAYLIST_LIMIT)
            ])

    def test_get_user_playlist_ids_request_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_playlists.side_effect = SpotifyException(
                "error", "error", "error")
            with self.assertRaises(SpotifyClientRequestError):
                list(self.client.get_user_playlist_ids())

    def test_get_user_playlist_ids_response_data_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_playlists.return_value = {
                "invalid_data"
            }
            with self.assertRaises(SpotifyClientResponseDataError):
                list(self.client.get_user_playlist_ids())

    def test_get_playlist_success(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.playlist.return_value = {
                "id": "playlist_id",
                "name": "playlist_name",
                "tracks": {
                    "items": [{
                        "track": self.TRACK_RESPONSE_DATA
                    }]
                }
            }
            self.assertEqual(
                self.client.get_playlist(playlist_id="playlist_id"),
                Playlist(playlist_id="playlist_id",
                         name="playlist_name").add_track(
                             self.TRACK_RESPONSE_OBJECT))
            client_instance_mock.playlist.assert_called_once_with(
                playlist_id="playlist_id",
                fields=["id, name, tracks.items(track(name, id, artists))"])

    def test_get_playlist_request_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.playlist.side_effect = SpotifyException(
                "error", "error", "error")
            with self.assertRaises(SpotifyClientRequestError):
                self.client.get_playlist(playlist_id="playlist_id")

    def test_get_playlist_response_data_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.playlist.return_value = {"invalid_data"}
            with self.assertRaises(SpotifyClientResponseDataError):
                self.client.get_playlist(playlist_id="playlist_id")

    def test_get_recently_played_tracks_success(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_recently_played.return_value = {
                "items": [{
                    "track": self.TRACK_RESPONSE_DATA
                }]
            }
            self.assertEqual(
                self.client.get_recently_played_tracks(),
                TracksCollection().add_track(self.TRACK_RESPONSE_OBJECT))
            client_instance_mock.current_user_recently_played.assert_called_once(
            )

    def test_get_recently_played_tracks_request_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_recently_played.side_effect = SpotifyException(
                "error", "error", "error")
            with self.assertRaises(SpotifyClientRequestError):
                self.client.get_recently_played_tracks()

    def test_get_recently_played_tracks_response_data_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_recently_played.return_value = {
                "invalid_data"
            }
            with self.assertRaises(SpotifyClientResponseDataError):
                self.client.get_recently_played_tracks()

    def test_get_related_artists_success(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.artist_related_artists.return_value = {
                "artists": [self.ARTIST_RESPONSE_DATA]
            }
            self.assertEqual(
                self.client.get_related_artists(artist_id="artist_id"),
                [self.ARTIST_RESPONSE_OBJECT])
            client_instance_mock.artist_related_artists.assert_called_once_with(
                artist_id="artist_id")

    def test_get_related_artists_request_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.artist_related_artists.side_effect = SpotifyException(
                "error", "error", "error")
            with self.assertRaises(SpotifyClientRequestError):
                self.client.get_related_artists(artist_id="artist_id")

    def test_get_related_artists_response_data_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.artist_related_artists.return_value = {
                "invalid_data"
            }
            with self.assertRaises(SpotifyClientResponseDataError):
                self.client.get_related_artists(artist_id="artist_id")

    def test_get_artist_top_tracks_success(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.artist_top_tracks.return_value = {
                "tracks": [self.TRACK_RESPONSE_DATA]
            }
            self.assertEqual(
                self.client.get_artist_top_tracks(artist_id="artist_id"),
                TracksCollection().add_track(self.TRACK_RESPONSE_OBJECT))
            client_instance_mock.artist_top_tracks.assert_called_once_with(
                artist_id="artist_id")

    def test_get_artist_top_tracks_request_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.artist_top_tracks.side_effect = SpotifyException(
                "error", "error", "error")
            with self.assertRaises(SpotifyClientRequestError):
                self.client.get_artist_top_tracks(artist_id="artist_id")

    def test_get_artist_top_tracks_response_data_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.artist_top_tracks.return_value = {
                "invalid_data"
            }
            with self.assertRaises(SpotifyClientResponseDataError):
                self.client.get_artist_top_tracks(artist_id="artist_id")

    def test_replace_playlist_tracks_success(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            self.client.replace_playlist_tracks(
                playlist_id="playlist_id",
                new_track_ids=["track_a", "track_b"])
            client_instance_mock.playlist_replace_items.assert_called_once_with(
                playlist_id="playlist_id", items=["track_a", "track_b"])

    def test_replace_playlist_tracks_request_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.playlist_replace_items.side_effect = SpotifyException(
                "error", "error", "error")
            with self.assertRaises(SpotifyClientRequestError):
                self.client.replace_playlist_tracks(
                    playlist_id="playlist_id",
                    new_track_ids=["track_a", "track_b"])

    def test_get_user_liked_tracks_success(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            tracks_data = []
            tracks_objects = []
            for i in range(self.client.GET_USER_LIKED_SONGS_LIMIT):
                track_data = dict(self.TRACK_RESPONSE_DATA)
                track_id = f"track_{i}_id"
                track_name = f"track_{i}_name"
                track_data["id"] = track_id
                track_data["name"] = track_name
                tracks_data.append({"track": track_data})
                tracks_objects.append(
                    Track(id=track_id,
                          name=track_name,
                          artist=self.ARTIST_RESPONSE_OBJECT))
            client_instance_mock.current_user_saved_tracks.side_effect = [{
                "items":
                tracks_data
            }, {
                "items": []
            }]
            expected_tracks_collection = TracksCollection()
            for tracks_object in tracks_objects:
                expected_tracks_collection.add_track(tracks_object)
            self.assertEqual(self.client.get_user_liked_tracks(),
                             expected_tracks_collection)
            client_instance_mock.current_user_saved_tracks.assert_has_calls([
                mock.call(limit=SpotifyClient.GET_USER_LIKED_SONGS_LIMIT,
                          offset=0),
                mock.call(limit=SpotifyClient.GET_USER_LIKED_SONGS_LIMIT,
                          offset=SpotifyClient.GET_USER_LIKED_SONGS_LIMIT)
            ])

    def test_get_user_liked_tracks_request_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_saved_tracks.side_effect = SpotifyException(
                "error", "error", "error")
            with self.assertRaises(SpotifyClientRequestError):
                self.client.get_user_liked_tracks()

    def test_get_user_liked_tracks_response_data_error(self):
        with mock.patch("traemplist.client.Spotify") as client_mock:
            client_instance_mock = mock.Mock()
            client_mock.side_effect = lambda *args, **kwargs: client_instance_mock
            client_instance_mock.current_user_saved_tracks.return_value = {
                "invalid_data"
            }
            with self.assertRaises(SpotifyClientResponseDataError):
                self.client.get_user_liked_tracks()
Exemplo n.º 7
0
 def _create_test_track(track_id: str) -> Track:
     return Track(id=track_id,
                  name=f"{track_id} name",
                  artist=Artist(id=f"{track_id} artist_id",
                                name=f"{track_id } artist_name"))