Fix LastFM rate limiting dropping scrobbles (f91fdd53 follow-up)
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
from datetime import datetime, timedelta
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pylast
|
||||
import pytest
|
||||
import pytz
|
||||
from django.contrib.auth import get_user_model
|
||||
@ -62,6 +63,44 @@ def mock_track_find_or_create():
|
||||
yield mock_find
|
||||
|
||||
|
||||
def make_pylast_scrobble(artist, title, timestamp, album="Emotion"):
|
||||
track = MagicMock()
|
||||
artist_mock = MagicMock()
|
||||
artist_mock.name = artist
|
||||
track.artist = artist_mock
|
||||
track.title = title
|
||||
scrobble = MagicMock()
|
||||
scrobble.track = track
|
||||
scrobble.album = album
|
||||
scrobble.timestamp = str(int(timestamp.timestamp()))
|
||||
return scrobble
|
||||
|
||||
|
||||
class TestGetLastScrobbles:
|
||||
@pytest.mark.django_db
|
||||
def test_artist_used_when_enrichment_fails(self, lfm_user, mock_lastfm_network):
|
||||
timestamp = datetime(2023, 2, 15, 12, 0, 0, tzinfo=UTC)
|
||||
scrobble = make_pylast_scrobble("Carly Rae Jepsen", "Emotion", timestamp)
|
||||
# Enrichment (duration/mbid) hits Last.fm's rate limit, but the
|
||||
# artist from the recent-tracks response must still be used.
|
||||
scrobble.track.get_duration.side_effect = pylast.WSError(
|
||||
None, 429, "rate limited"
|
||||
)
|
||||
scrobble.track.get_mbid.side_effect = pylast.WSError(None, 429, "rate limited")
|
||||
|
||||
lastfm = LastFM(lfm_user)
|
||||
lastfm.user.get_recent_tracks.return_value = [scrobble]
|
||||
with patch("scrobbles.importers.lastfm.time.sleep"):
|
||||
parsed = lastfm.get_last_scrobbles()
|
||||
|
||||
assert len(parsed) == 1
|
||||
assert parsed[0]["artist"] == "Carly Rae Jepsen"
|
||||
assert parsed[0]["title"] == "Emotion"
|
||||
assert parsed[0]["run_time_seconds"] is None
|
||||
assert parsed[0]["mbid"] is None
|
||||
assert parsed[0]["timestamp"] == timestamp
|
||||
|
||||
|
||||
class TestImportFromLastfm:
|
||||
@pytest.mark.django_db
|
||||
def test_import_creates_scrobble(
|
||||
|
||||
Reference in New Issue
Block a user