diff --git a/scrobble/lastfm/client_test.go b/scrobble/lastfm/client_test.go index 6787727c..78212c85 100644 --- a/scrobble/lastfm/client_test.go +++ b/scrobble/lastfm/client_test.go @@ -75,7 +75,7 @@ func TestArtistGetInfo(t *testing.T) { Playcount: "2", }, URL: "https://www.last.fm/music/Artist+1", - Image: []ArtistImage{ + Image: []Image{ { Size: "small", Text: "https://last.fm/artist-1-small.png", @@ -96,7 +96,7 @@ func TestArtistGetInfo(t *testing.T) { }, Name: "Similar Artist 1", URL: "https://www.last.fm/music/Similar+Artist+1", - Image: []ArtistImage{ + Image: []Image{ { Size: "small", Text: "https://last.fm/similar-artist-1-small.png", @@ -180,10 +180,7 @@ func TestArtistGetTopTracks(t *testing.T) { }, Tracks: []Track{ { - Image: []struct { - Text string `xml:",chardata"` - Size string `xml:"size,attr"` - }{ + Image: []Image{ { Text: "https://last.fm/track-1-small.png", Size: "small", @@ -201,10 +198,7 @@ func TestArtistGetTopTracks(t *testing.T) { URL: "https://www.last.fm/music/Artist+1/_/Track+1", }, { - Image: []struct { - Text string `xml:",chardata"` - Size string `xml:"size,attr"` - }{ + Image: []Image{ { Text: "https://last.fm/track-2-small.png", Size: "small", @@ -252,6 +246,106 @@ func TestArtistGetTopTracks_clientRequestFails(t *testing.T) { require.Zero(actual) } +//go:embed testdata/track_get_similar_response.xml +var trackGetSimilaResponse string + +func TestTrackGetSimilarTracks(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{"track.getSimilar"}, + "api_key": []string{"apiKey1"}, + "artist": []string{"artist1"}, + "track": []string{"track1"}, + }, 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(trackGetSimilaResponse)) + })) + defer shutdown() + + client := Client{&httpClient} + + // act + actual, err := client.TrackGetSimilarTracks("apiKey1", "artist1", "track1") + + // assert + require.NoError(err) + require.Equal(SimilarTracks{ + Artist: "Artist 1", + Track: "Track 1", + XMLName: xml.Name{ + Local: "similartracks", + }, + Tracks: []Track{ + { + Image: []Image{ + { + Text: "https://last.fm/track-1-small.png", + Size: "small", + }, + { + Text: "https://last.fm/track-1-large.png", + Size: "large", + }, + }, + MBID: "7096931c-bf82-4896-b1e7-42b60a0e16ea", + Name: "Track 1", + PlayCount: 1, + URL: "https://www.last.fm/music/Artist+1/_/Track+1", + }, + { + Image: []Image{ + { + Text: "https://last.fm/track-2-small.png", + Size: "small", + }, + { + Text: "https://last.fm/track-2-large.png", + Size: "large", + }, + }, + MBID: "2aff1321-149f-4000-8762-3468c917600c", + Name: "Track 2", + PlayCount: 2, + URL: "https://www.last.fm/music/Artist+2/_/Track+2", + }, + }, + }, actual) +} + +func TestTrackGetSimilarTracks_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{"track.getSimilar"}, + "api_key": []string{"apiKey1"}, + "artist": []string{"artist1"}, + "track": []string{"track1"}, + }, 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.TrackGetSimilarTracks("apiKey1", "artist1", "track1") + + // assert + require.Error(err) + require.Zero(actual) +} + func TestGetParamSignature(t *testing.T) { params := url.Values{} params.Add("ccc", "CCC") diff --git a/scrobble/lastfm/model.go b/scrobble/lastfm/model.go index a2731bed..1ddf3795 100644 --- a/scrobble/lastfm/model.go +++ b/scrobble/lastfm/model.go @@ -26,29 +26,26 @@ type ( } SimilarArtist struct { - XMLName xml.Name `xml:"artist"` - Name string `xml:"name"` - MBID string `xml:"mbid"` - URL string `xml:"url"` - Image []struct { - Text string `xml:",chardata"` - Size string `xml:"size,attr"` - } `xml:"image"` - Streamable string `xml:"streamable"` + XMLName xml.Name `xml:"artist"` + Name string `xml:"name"` + MBID string `xml:"mbid"` + URL string `xml:"url"` + Image []Image `xml:"image"` + Streamable string `xml:"streamable"` } - ArtistImage struct { + Image struct { Text string `xml:",chardata"` Size string `xml:"size,attr"` } Artist struct { - XMLName xml.Name `xml:"artist"` - Name string `xml:"name"` - MBID string `xml:"mbid"` - URL string `xml:"url"` - Image []ArtistImage `xml:"image"` - Streamable string `xml:"streamable"` + XMLName xml.Name `xml:"artist"` + Name string `xml:"name"` + MBID string `xml:"mbid"` + URL string `xml:"url"` + Image []Image `xml:"image"` + Streamable string `xml:"streamable"` Stats struct { Listeners string `xml:"listeners"` Playcount string `xml:"playcount"` @@ -100,9 +97,6 @@ type ( PlayCount int `xml:"playcount"` Listeners int `xml:"listeners"` URL string `xml:"url"` - Image []struct { - Text string `xml:",chardata"` - Size string `xml:"size,attr"` - } `xml:"image"` + Image []Image `xml:"image"` } ) diff --git a/scrobble/lastfm/testdata/track_get_similar_response.xml b/scrobble/lastfm/testdata/track_get_similar_response.xml new file mode 100644 index 00000000..f13b0ad0 --- /dev/null +++ b/scrobble/lastfm/testdata/track_get_similar_response.xml @@ -0,0 +1,37 @@ + + + + + Track 1 + 1 + 7096931c-bf82-4896-b1e7-42b60a0e16ea + 1.000 + https://www.last.fm/music/Artist+1/_/Track+1 + 0 + 80 + + 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 + 2aff1321-149f-4000-8762-3468c917600c + 0.422 + https://www.last.fm/music/Artist+2/_/Track+2 + 0 + 80 + + Artist+2 + 9842b07f-956b-4c36-8ce1-884b4b96254d + 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