Skip to content

Commit

Permalink
add test for LastFM top tracks (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzurowski authored Jun 29, 2023
1 parent 7982ffc commit 2e879f6
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
107 changes: 107 additions & 0 deletions scrobble/lastfm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 35 additions & 0 deletions scrobble/lastfm/testdata/artist_get_top_tracks_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<lfm status="ok">
<toptracks artist="Artist 1" page="1" perPage="50" totalPages="5" total="225">
<track rank="1">
<name>Track 1</name>
<playcount>1</playcount>
<listeners>2</listeners>
<mbid>fdfc47cb-69d3-4318-ba71-d54fbc20169a</mbid>
<url>https://www.last.fm/music/Artist+1/_/Track+1</url>
<streamable>0</streamable>
<artist>
<name>Artist 1</name>
<mbid>366c1119-ec4f-4312-b729-a5637d148e3e</mbid>
<url>https://www.last.fm/music/Artist+1</url>
</artist>
<image size="small">https://last.fm/track-1-small.png</image>
<image size="large">https://last.fm/track-1-large.png</image>
</track>
<track rank="2">
<name>Track 2</name>
<playcount>2</playcount>
<listeners>3</listeners>
<mbid>cf32e694-1ea6-4ba0-9e8b-d5f1950da9c8</mbid>
<url>https://www.last.fm/music/Artist+1/_/Track+2</url>
<streamable>0</streamable>
<artist>
<name>Artist 1</name>
<mbid>366c1119-ec4f-4312-b729-a5637d148e3e</mbid>
<url>https://www.last.fm/music/Artist+1</url>
</artist>
<image size="small">https://last.fm/track-2-small.png</image>
<image size="large">https://last.fm/track-2-large.png</image>
</track>
</toptracks>
</lfm>

0 comments on commit 2e879f6

Please sign in to comment.