Ejemplo n.º 1
0
 def test_generate_and_save_traemplist_from_standard_playlist(self):
     playlist_track = self._create_test_track("playlist_track")
     self.spotify_client_mock.get_playlist.return_value = Playlist(
         playlist_id="playlist_id",
         name="playlist_name").add_track(playlist_track)
     generated_tracks_collection = TracksCollection().add_track(
         playlist_track)
     self.traemplist_generator_mock.generate.return_value = generated_tracks_collection
     TraemplistGeneratorService(
         config=TraemplistConfig(account=AccountConfig(
             credentials=self.account_credentials,
             playlists=[PlaylistConfig(id="playlist_id")]),
                                 traemplist_songs_count=2,
                                 traemplist_id="traemplist_id"),
         client=self.spotify_client_mock,
         generator=self.traemplist_generator_mock,
         logger=mock.Mock()).generate_and_save_traemplist()
     self.traemplist_generator_mock.generate.assert_called_once_with(
         input_tracks_collection=TracksCollection().add_track(
             playlist_track),
         size=2)
     self.spotify_client_mock.get_playlist.assert_called_once_with(
         "playlist_id")
     self.spotify_client_mock.replace_playlist_tracks.assert_called_once_with(
         playlist_id="traemplist_id", new_track_ids=["playlist_track"])
Ejemplo n.º 2
0
 def _get_related_artists_tracks(self, artist: Artist) -> TracksCollection:
     related_artists_tracks = TracksCollection()
     for related_artist in self.client.get_related_artists(
             artist_id=artist.id):
         related_artists_tracks.add_tracks(
             self.client.get_artist_top_tracks(artist_id=related_artist.id))
     return related_artists_tracks
Ejemplo n.º 3
0
 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)
         ])
Ejemplo n.º 4
0
 def test_get_random_track(self):
     with self.subTest("From non-empty collection"):
         track = self._create_test_track()
         collection = TracksCollection().add_track(track)
         self.assertEqual(collection.get_random_track(), track)
     with self.subTest("From empty collection"):
         with self.assertRaises(EmptyTracksCollectionError):
             TracksCollection().get_random_track()
Ejemplo n.º 5
0
 def test_save_recently_played_tracks_success(self):
     recently_played_tracks = TracksCollection()
     recently_played_tracks.add_track(self._create_test_track("test_track"))
     self.spotify_client_mock.get_recently_played_tracks.return_value = recently_played_tracks
     self.tracks_history_service.save_recently_played_tracks()
     self.tracks_repository_mock.save_tracks.assert_called_once_with([
         TrackRecord(id=track.id)
         for track in recently_played_tracks.get_tracks()
     ])
Ejemplo n.º 6
0
 def test_tracks_adding(self):
     collection = TracksCollection()
     self.assertEqual(collection.get_tracks(), set())
     track_a = self._create_test_track()
     track_b = self._create_test_track()
     collection.add_track(track_a)
     collection.add_track(track_b)
     self.assertEqual(collection.get_tracks(), {track_a, track_b})
     track_c = self._create_test_track()
     track_d = self._create_test_track()
     collection.add_tracks(
         TracksCollection().add_track(track_c).add_track(track_d))
     self.assertEqual(collection.get_tracks(),
                      {track_a, track_b, track_c, track_d})
Ejemplo n.º 7
0
 def generate(self, input_tracks_collection: TracksCollection,
              size: int) -> TracksCollection:
     """
     :raises TraemplistGeneratorException
     """
     if size < 0:
         raise InvalidTraemplistSizeError
     traemplist = TracksCollection()
     while True:
         if not input_tracks_collection:
             self.logger.log_info(
                 "Input tracks collection is empty - generating done")
             return traemplist
         start_track = input_tracks_collection.get_random_track()
         self.logger.log_info(
             f"Randomly picked track: '{start_track.artist.name} - {start_track.name}'"
         )
         self.logger.log_info("Loading top related artists' tracks")
         related_artists_tracks = list(
             self._get_related_artists_tracks(
                 start_track.artist).get_tracks())
         random.shuffle(related_artists_tracks)
         for track in related_artists_tracks:
             if self._is_traemplist_candidate(track, traemplist):
                 self.logger.log_info(
                     f"'{track.artist.name} - {track.name}' seems like a good choice, adding"
                 )
                 traemplist.add_track(track)
                 if len(traemplist) >= size:
                     return traemplist
                 break
         input_tracks_collection.remove_artist_tracks(start_track.artist)
Ejemplo n.º 8
0
 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")
Ejemplo n.º 9
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))
Ejemplo n.º 10
0
 def _is_traemplist_candidate(self, track: Track,
                              traemplist: TracksCollection) -> bool:
     if self.history.contains_track(track.id):
         self.logger.log_info(
             f"You've already heard '{track.artist.name} - {track.name}', skipping"
         )
         return False
     if traemplist.contains_artist_track(track.artist):
         self.logger.log_info(
             f"Artist '{track.artist.name}' is already in traemplist, skipping"
         )
         return False
     return True
Ejemplo n.º 11
0
 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(
         )
Ejemplo n.º 12
0
 def _get_input_tracks(self) -> TracksCollection:
     input_tracks = TracksCollection()
     for playlist in self.config.account.playlists:
         if playlist.id == Config.LIKED_SONGS_PLAYLIST_ID:
             input_tracks.add_tracks(
                 self.client.get_user_liked_tracks()
             )
         else:
             input_tracks.add_tracks(
                 self.client.get_playlist(playlist.id)
             )
     return input_tracks
Ejemplo n.º 13
0
 def test_length(self):
     collection = TracksCollection()
     self.assertEqual(len(collection), 0)
     collection.add_track(self._create_test_track())
     self.assertEqual(len(collection), 1)
Ejemplo n.º 14
0
 def test_contains(self):
     track_a = self._create_test_track()
     track_b = self._create_test_track()
     collection = TracksCollection().add_track(track_a)
     self.assertTrue(track_a in collection)
     self.assertFalse(track_b in collection)
Ejemplo n.º 15
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")))
Ejemplo n.º 16
0
 def _tracks_to_track_records(tracks: TracksCollection) -> [TrackRecord]:
     return [TrackRecord(id=track.id) for track in tracks.get_tracks()]
Ejemplo n.º 17
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")
     ])