def test_fetch(self, mock_paginated_endpoint_fetch): expected_tracks = [ Track(track_name="Strawberry Fields Forever", artist="The Beatles"), Track(track_name="Badge", artist="Cream"), Track(track_name="Black Dog", artist="Led Zeppelin"), ] mock_paginated_endpoint_fetch.return_value = [ { "recenttracks": { "track": [ { "name": "Strawberry Fields Forever", "artist": { "name": "The Beatles" }, }, { "name": "Badge", "artist": { "name": "Cream" } }, ], "@attr": { "totalPages": "2" }, } }, { "recenttracks": { "track": [{ "name": "Black Dog", "artist": { "name": "Led Zeppelin" } }], "@attr": { "totalPages": "2" }, } }, ] fetched_tracks = recent_tracks.fetch_recent_tracks( user=self.dummy_user, api_key=self.dummy_api_key) self.assertCountEqual(fetched_tracks, expected_tracks)
def test_fetch_single_page(self): expected_saved_tracks = [ Track(track_name="Penny Lane", artist="The Beatles", spotify_id="123456789") ] self.mock_spotify.current_user_saved_tracks = MagicMock() mock_saved_tracks_response = { "items": [{ "id": "123456789", "name": "Penny Lane", "artists": [{ "name": "The Beatles" }], }] } self.mock_spotify.current_user_saved_tracks.side_effect = [ mock_saved_tracks_response, { "items": [] }, ] fetched_saved_tracks = get_saved_tracks(self.mock_spotify) self.assertCountEqual(fetched_saved_tracks, expected_saved_tracks)
def test_fetch_multiple_pages(self): expected_saved_tracks = [ Track(track_name="Penny Lane", artist="The Beatles", spotify_id="123456789"), Track( track_name="A Day in the Life", artist="The Beatles", spotify_id="987654321", ), ] self.mock_spotify.current_user_saved_tracks = MagicMock() mock_saved_tracks_response_page_1 = { "items": [{ "id": "123456789", "name": "Penny Lane", "artists": [{ "name": "The Beatles" }], }] } mock_saved_tracks_response_page_2 = { "items": [{ "id": "987654321", "name": "A Day in the Life", "artists": [{ "name": "The Beatles" }], }] } self.mock_spotify.current_user_saved_tracks.side_effect = [ mock_saved_tracks_response_page_1, mock_saved_tracks_response_page_2, { "items": [] }, ] fetched_saved_tracks = get_saved_tracks(self.mock_spotify) self.assertCountEqual(fetched_saved_tracks, expected_saved_tracks)
def fetch_recent_tracks(user, api_key): """Fetches recent tracks for the given user""" logging.info("Fetching recent tracks for " + user) paginated_json_responses = fetch_paginated_response( URL, user, api_key, "recenttracks" ) recent_tracks = [] for json_response in paginated_json_responses: for track in json_response["recenttracks"]["track"]: recent_tracks.append(Track(parse_track_name(track), parse_artist(track))) logging.info("Fetched " + str(len(recent_tracks)) + " recent tracks") logging.debug("Fetched tracks: " + str(recent_tracks)) return recent_tracks