Skip to content

Commit

Permalink
add tests for LastFM similar tracks (#347)
Browse files Browse the repository at this point in the history
* Rename and reuse image struct

* Add LastFM test for similar tracks
  • Loading branch information
gzurowski committed Aug 13, 2023
1 parent 78c7fbe commit 09413ea
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 30 deletions.
114 changes: 104 additions & 10 deletions scrobble/lastfm/client_test.go
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand Down
34 changes: 14 additions & 20 deletions scrobble/lastfm/model.go
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
}
)
37 changes: 37 additions & 0 deletions scrobble/lastfm/testdata/track_get_similar_response.xml
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<lfm status="ok">
<similartracks artist="Artist 1" track="Track 1">
<track>
<name>Track 1</name>
<playcount>1</playcount>
<mbid>7096931c-bf82-4896-b1e7-42b60a0e16ea</mbid>
<match>1.000</match>
<url>https://www.last.fm/music/Artist+1/_/Track+1</url>
<streamable fulltrack="0">0</streamable>
<duration>80</duration>
<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>
<name>Track 2</name>
<playcount>2</playcount>
<mbid>2aff1321-149f-4000-8762-3468c917600c</mbid>
<match>0.422</match>
<url>https://www.last.fm/music/Artist+2/_/Track+2</url>
<streamable fulltrack="0">0</streamable>
<duration>80</duration>
<artist>
<name>Artist+2</name>
<mbid>9842b07f-956b-4c36-8ce1-884b4b96254d</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>
</similartracks>
</lfm>

0 comments on commit 09413ea

Please sign in to comment.