Ejemplo n.º 1
0
    def test_get_recommendations_based_on_tracks__groups_recommendations_with_same_percentage(
            self):
        mock_track1 = mock_track(spotify_id="mock-track-id-1")
        mock_track10 = mock_track(spotify_id="mock-track-id-10")
        mock_track2 = mock_track(spotify_id="mock-track-id-2")
        tracks = [mock_track1, mock_track10, mock_track2]
        song_attribute_ranges = mock_song_attribute_ranges()
        mock_recommendations = dict()
        mock_recommendations[mock_track1] = 1.0
        mock_recommendations[mock_track10] = 0.5
        mock_recommendations[mock_track2] = 0.5
        self.music_util._get_recommendations_based_on_tracks_in_batches = MagicMock(
            return_value=mock_recommendations)

        recommendations_by_percent = self.music_util.get_recommendations_based_on_tracks(
            tracks, song_attribute_ranges)

        self.assertEqual(2, len(recommendations_by_percent))
        self.assertIn(1.0, recommendations_by_percent)
        self.assertIn(0.5, recommendations_by_percent)
        self.assertEqual(1, len(recommendations_by_percent[1.0]))
        self.assertIn(mock_track1, recommendations_by_percent[1.0])
        self.assertEqual(2, len(recommendations_by_percent[0.5]))
        self.assertIn(mock_track10, recommendations_by_percent[0.5])
        self.assertIn(mock_track2, recommendations_by_percent[0.5])
Ejemplo n.º 2
0
    def test_populate_popularity_if_absent__no_popularity__populates(self):
        tracks_without_popularity = [mock_track(popularity=None)]
        track_with_popularity = [mock_track(popularity=1)]
        self.mock_spotify.get_tracks = MagicMock(
            return_value=track_with_popularity)

        self.music_util.populate_popularity_if_absent(
            tracks_without_popularity)

        self.assertIsNotNone(tracks_without_popularity[0].popularity)
        self.assertEqual(1, tracks_without_popularity[0].popularity)
Ejemplo n.º 3
0
    def test_filter_out_if_not_in_albums__some_tracks_in_albums__filter_out_others(
            self):
        mock_tracks = [
            mock_track(album=mock_album(spotify_id="album123")),
            mock_track(album=mock_album(spotify_id="some other album")),
        ]
        mock_albums = [mock_album(spotify_id="album123")]

        tracks = self.music_util.filter_out_if_not_in_albums(
            mock_tracks, mock_albums)

        self.assertEqual(len(tracks), 1)
        self.assertEqual(tracks[0].spotify_album_id, "album123")
Ejemplo n.º 4
0
    def test_get_audio_feature_representative_range__tracks_with_same_value(
            self):
        audio_features_1 = mock_audio_features(danceability=0.75)
        audio_features_2 = mock_audio_features(danceability=0.75)
        playlist = mock_playlist(tracks=[
            mock_track(audio_features=audio_features_1),
            mock_track(audio_features=audio_features_2),
        ])

        audio_feature_range = self.playlist_stats.get_audio_feature_representative_range(
            playlist)

        self.assertEqual(0.75, audio_feature_range[0].danceability)
        self.assertEqual(0.75, audio_feature_range[1].danceability)
Ejemplo n.º 5
0
    def test_get_audio_feature_representative_range__multiple_tracks__matches_range(
            self):
        audio_features_1 = mock_audio_features(danceability=0.95)
        audio_features_2 = mock_audio_features(danceability=0.05)
        playlist = mock_playlist(tracks=[
            mock_track(audio_features=audio_features_1),
            mock_track(audio_features=audio_features_2),
        ])

        audio_feature_range = self.playlist_stats.get_audio_feature_representative_range(
            playlist)

        self.assertLessEqual(0, audio_feature_range[0].danceability)
        self.assertGreaterEqual(1, audio_feature_range[1].danceability)
Ejemplo n.º 6
0
    def test__get_recommendations_based_on_tracks_in_batches__recommended_for_half_the_tracks__specifies_50_percent(
            self):
        track_ids = ["mock-seed-track-1", "mock-seed-track-2"]
        song_attribute_ranges = mock_song_attribute_ranges()
        self.mock_spotify.get_recommendation_seed_limit = MagicMock(
            return_value=1)
        recommended_track = mock_track(spotify_id="mock-recommendation")

        def mock_get_recommendations(track_ids, _):
            if track_ids == ["mock-seed-track-1"]:
                return [recommended_track]
            elif track_ids == ["mock-seed-track-2"]:
                return []
            raise NotImplemented()

        self.mock_spotify.get_recommendations_based_on_tracks = MagicMock(
            side_effect=mock_get_recommendations)

        recommendations_with_percentage = self.music_util._get_recommendations_based_on_tracks_in_batches(
            track_ids, song_attribute_ranges)

        self.assertEqual(1, len(recommendations_with_percentage))
        self.assertIn(recommended_track, recommendations_with_percentage)
        self.assertEqual(0.5,
                         recommendations_with_percentage[recommended_track])
Ejemplo n.º 7
0
    def test_populate_popularity_if_absent__already_contains_popularity__no_change(
            self):
        input_tracks = [
            mock_track(spotify_id="popularity is 2", popularity=2),
            mock_track(spotify_id="popularity is 1", popularity=1)
        ]
        self.mock_spotify.get_tracks = MagicMock(return_value=[])

        self.music_util.populate_popularity_if_absent(input_tracks)

        self.assertIsNotNone(input_tracks[0].popularity)
        self.assertEqual(2, len(input_tracks))
        self.assertEqual(2, input_tracks[0].popularity)
        self.assertEqual("popularity is 2", input_tracks[0].spotify_id)
        self.assertEqual(1, input_tracks[1].popularity)
        self.assertEqual("popularity is 1", input_tracks[1].spotify_id)
Ejemplo n.º 8
0
    def test_populate_track_audio_features(self):
        track = mock_track(spotify_id="1ZZMu5cxiU4eFUNVwMnCJq",
                           audio_features=None)

        self.spotify.set_track_audio_features([track])

        self.assertIsNotNone(track.audio_features)
Ejemplo n.º 9
0
    def test_filter_out_if_not_in_albums__no_albums__none_returned(self):
        mock_tracks = [mock_track()]
        mock_albums = []

        tracks = self.music_util.filter_out_if_not_in_albums(
            mock_tracks, mock_albums)

        self.assertEqual(len(tracks), 0)
Ejemplo n.º 10
0
    def test_filter_out_if_not_in_albums__all_tracks_in_albums__does_not_filter_any(
            self):
        mock_tracks = [mock_track(album=mock_album(spotify_id="album123"))]
        mock_albums = [mock_album(spotify_id="album123")]

        tracks = self.music_util.filter_out_if_not_in_albums(
            mock_tracks, mock_albums)

        self.assertEqual(tracks, mock_tracks)
Ejemplo n.º 11
0
    def test_populate_popularity_if_absent__some_no_popularity__only_fetches_data_for_those(
            self):
        track_with_popularity_missing = mock_track(
            spotify_id="popularity=None", popularity=None)
        track_with_popularity_missing_2 = mock_track(
            spotify_id="popularity=None #2", popularity=None)
        input_tracks = [
            track_with_popularity_missing,
            mock_track(spotify_id="popularity is set", popularity=1),
            track_with_popularity_missing_2,
        ]
        self.mock_spotify.get_tracks = MagicMock(return_value=[])

        self.music_util.populate_popularity_if_absent(input_tracks)

        self.mock_spotify.get_tracks.assert_called_once()
        self.mock_spotify.get_tracks.assert_called_once_with(
            [track_with_popularity_missing, track_with_popularity_missing_2])
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    def test_get_audio_feature_representative_range__single_track__allows_full_range(
            self):
        audio_features = mock_audio_features(danceability=0.75)
        playlist = mock_playlist(
            tracks=[mock_track(audio_features=audio_features)])

        audio_feature_range = self.playlist_stats.get_audio_feature_representative_range(
            playlist)

        self.assertEqual(0, audio_feature_range[0].danceability)
        self.assertEqual(1, audio_feature_range[1].danceability)
Ejemplo n.º 14
0
    def test_populate_popularity_if_absent__some_no_popularity__populates(
            self):
        track_with_popularity_missing = mock_track(
            spotify_id="popularity=None", popularity=None)
        input_tracks = [
            track_with_popularity_missing,
            mock_track(spotify_id="popularity is set", popularity=1)
        ]
        track_with_popularity__after_fetched = [
            mock_track(spotify_id="popularity=None", popularity=2),
        ]
        self.mock_spotify.get_tracks = MagicMock(
            return_value=track_with_popularity__after_fetched)

        self.music_util.populate_popularity_if_absent(input_tracks)

        self.assertIsNotNone(input_tracks[0].popularity)
        self.assertEqual(2, len(input_tracks))
        self.assertEqual(2, input_tracks[0].popularity)
        self.assertEqual("popularity=None", input_tracks[0].spotify_id)
        self.assertEqual(1, input_tracks[1].popularity)
        self.assertEqual("popularity is set", input_tracks[1].spotify_id)
Ejemplo n.º 15
0
    def test__get_recommendations_based_on_tracks_in_batches__multiple_percentages__recommends_all(
            self):
        track_ids = [
            "mock-seed-track-1", "mock-seed-track-2", "mock-seed-track-3"
        ]
        song_attribute_ranges = mock_song_attribute_ranges()
        self.mock_spotify.get_recommendation_seed_limit = MagicMock(
            return_value=1)
        recommended_track1 = mock_track(spotify_id="mock-recommendation-1")
        recommended_track2 = mock_track(spotify_id="mock-recommendation-2")

        def mock_get_recommendations(track_ids, _):
            if track_ids == ["mock-seed-track-1"]:
                return [recommended_track1, recommended_track2]
            elif track_ids == ["mock-seed-track-2"]:
                return [recommended_track1]
            elif track_ids == ["mock-seed-track-3"]:
                return []
            raise NotImplemented()

        self.mock_spotify.get_recommendations_based_on_tracks = MagicMock(
            side_effect=mock_get_recommendations)

        recommendations_with_percentage = self.music_util._get_recommendations_based_on_tracks_in_batches(
            track_ids, song_attribute_ranges)

        self.mock_spotify.get_recommendations_based_on_tracks.assert_any_call(
            ["mock-seed-track-1"], song_attribute_ranges)
        self.mock_spotify.get_recommendations_based_on_tracks.assert_any_call(
            ["mock-seed-track-2"], song_attribute_ranges)
        self.mock_spotify.get_recommendations_based_on_tracks.assert_any_call(
            ["mock-seed-track-3"], song_attribute_ranges)
        self.assertEqual(2, len(recommendations_with_percentage))
        self.assertEqual(2.0 / 3.0,
                         recommendations_with_percentage[recommended_track1])
        self.assertEqual(1.0 / 3.0,
                         recommendations_with_percentage[recommended_track2])
Ejemplo n.º 16
0
    def test__get_recommendations_based_on_tracks_in_batches__recommended_for_all_tracks__specifies_100_percent(
            self):
        track_ids, song_attribute_ranges = ["mock-seed-track"
                                            ], mock_song_attribute_ranges()
        self.mock_spotify.get_recommendation_seed_limit = MagicMock(
            return_value=1)
        recommended_track = mock_track(spotify_id="mock-recommendation")
        self.mock_spotify.get_recommendations_based_on_tracks = MagicMock(
            return_value=[recommended_track])

        recommendations_with_percentage = self.music_util._get_recommendations_based_on_tracks_in_batches(
            track_ids, song_attribute_ranges)

        self.assertEqual(1, len(recommendations_with_percentage))
        self.assertIn(recommended_track, recommendations_with_percentage)
        self.assertEqual(1.0,
                         recommendations_with_percentage[recommended_track])