diff --git a/scrobble/lastfm/client_test.go b/scrobble/lastfm/client_test.go index 0a1de7e..6787727 100644 --- a/scrobble/lastfm/client_test.go +++ b/scrobble/lastfm/client_test.go @@ -145,6 +145,113 @@ func TestArtistGetInfo_clientRequestFails(t *testing.T) { require.Zero(actual) } +//go:embed testdata/artist_get_top_tracks_response.xml +var artistGetTopTracksResponse string + +func TestArtistGetTopTracks(t *testing.T) { + // arrange + require := require.New(t) + httpClient, shutdown := httpClientMock(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(http.MethodGet, r.Method) + require.Equal(url.Values{ + "method": []string{"artist.getTopTracks"}, + "api_key": []string{"apiKey1"}, + "artist": []string{"artist1"}, + }, r.URL.Query()) + require.Equal("/2.0/", r.URL.Path) + require.Equal(baseURL, "https://"+r.Host+r.URL.Path) + + w.WriteHeader(http.StatusOK) + w.Write([]byte(artistGetTopTracksResponse)) + })) + defer shutdown() + + client := Client{&httpClient} + + // act + actual, err := client.ArtistGetTopTracks("apiKey1", "artist1") + + // assert + require.NoError(err) + require.Equal(TopTracks{ + Artist: "Artist 1", + XMLName: xml.Name{ + Local: "toptracks", + }, + Tracks: []Track{ + { + Image: []struct { + Text string `xml:",chardata"` + Size string `xml:"size,attr"` + }{ + { + Text: "https://last.fm/track-1-small.png", + Size: "small", + }, + { + Text: "https://last.fm/track-1-large.png", + Size: "large", + }, + }, + Listeners: 2, + MBID: "fdfc47cb-69d3-4318-ba71-d54fbc20169a", + Name: "Track 1", + PlayCount: 1, + Rank: 1, + URL: "https://www.last.fm/music/Artist+1/_/Track+1", + }, + { + Image: []struct { + Text string `xml:",chardata"` + Size string `xml:"size,attr"` + }{ + { + Text: "https://last.fm/track-2-small.png", + Size: "small", + }, + { + Text: "https://last.fm/track-2-large.png", + Size: "large", + }, + }, + Listeners: 3, + MBID: "cf32e694-1ea6-4ba0-9e8b-d5f1950da9c8", + Name: "Track 2", + PlayCount: 2, + Rank: 2, + URL: "https://www.last.fm/music/Artist+1/_/Track+2", + }, + }, + }, actual) +} + +func TestArtistGetTopTracks_clientRequestFails(t *testing.T) { + // arrange + require := require.New(t) + httpClient, shutdown := httpClientMock(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(http.MethodGet, r.Method) + require.Equal(url.Values{ + "method": []string{"artist.getTopTracks"}, + "api_key": []string{"apiKey1"}, + "artist": []string{"artist1"}, + }, r.URL.Query()) + require.Equal("/2.0/", r.URL.Path) + require.Equal(baseURL, "https://"+r.Host+r.URL.Path) + + w.WriteHeader(http.StatusInternalServerError) + })) + defer shutdown() + + client := Client{&httpClient} + + // act + actual, err := client.ArtistGetTopTracks("apiKey1", "artist1") + + // assert + require.Error(err) + require.Zero(actual) +} + func TestGetParamSignature(t *testing.T) { params := url.Values{} params.Add("ccc", "CCC") diff --git a/scrobble/lastfm/testdata/artist_get_top_tracks_response.xml b/scrobble/lastfm/testdata/artist_get_top_tracks_response.xml new file mode 100644 index 0000000..e4b23d7 --- /dev/null +++ b/scrobble/lastfm/testdata/artist_get_top_tracks_response.xml @@ -0,0 +1,35 @@ + + + + + Track 1 + 1 + 2 + fdfc47cb-69d3-4318-ba71-d54fbc20169a + https://www.last.fm/music/Artist+1/_/Track+1 + 0 + + Artist 1 + 366c1119-ec4f-4312-b729-a5637d148e3e + https://www.last.fm/music/Artist+1 + + https://last.fm/track-1-small.png + https://last.fm/track-1-large.png + + + Track 2 + 2 + 3 + cf32e694-1ea6-4ba0-9e8b-d5f1950da9c8 + https://www.last.fm/music/Artist+1/_/Track+2 + 0 + + Artist 1 + 366c1119-ec4f-4312-b729-a5637d148e3e + https://www.last.fm/music/Artist+1 + + https://last.fm/track-2-small.png + https://last.fm/track-2-large.png + + + \ No newline at end of file