add tests for LastFM similar tracks (#347)

* Rename and reuse image struct

* Add LastFM test for similar tracks
This commit is contained in:
Gregor Zurowski
2023-08-13 23:33:52 +02:00
committed by GitHub
parent 78c7fbe493
commit 09413ea06e
3 changed files with 155 additions and 30 deletions

View File

@@ -75,7 +75,7 @@ func TestArtistGetInfo(t *testing.T) {
Playcount: "2", Playcount: "2",
}, },
URL: "https://www.last.fm/music/Artist+1", URL: "https://www.last.fm/music/Artist+1",
Image: []ArtistImage{ Image: []Image{
{ {
Size: "small", Size: "small",
Text: "https://last.fm/artist-1-small.png", Text: "https://last.fm/artist-1-small.png",
@@ -96,7 +96,7 @@ func TestArtistGetInfo(t *testing.T) {
}, },
Name: "Similar Artist 1", Name: "Similar Artist 1",
URL: "https://www.last.fm/music/Similar+Artist+1", URL: "https://www.last.fm/music/Similar+Artist+1",
Image: []ArtistImage{ Image: []Image{
{ {
Size: "small", Size: "small",
Text: "https://last.fm/similar-artist-1-small.png", Text: "https://last.fm/similar-artist-1-small.png",
@@ -180,10 +180,7 @@ func TestArtistGetTopTracks(t *testing.T) {
}, },
Tracks: []Track{ Tracks: []Track{
{ {
Image: []struct { Image: []Image{
Text string `xml:",chardata"`
Size string `xml:"size,attr"`
}{
{ {
Text: "https://last.fm/track-1-small.png", Text: "https://last.fm/track-1-small.png",
Size: "small", Size: "small",
@@ -201,10 +198,7 @@ func TestArtistGetTopTracks(t *testing.T) {
URL: "https://www.last.fm/music/Artist+1/_/Track+1", URL: "https://www.last.fm/music/Artist+1/_/Track+1",
}, },
{ {
Image: []struct { Image: []Image{
Text string `xml:",chardata"`
Size string `xml:"size,attr"`
}{
{ {
Text: "https://last.fm/track-2-small.png", Text: "https://last.fm/track-2-small.png",
Size: "small", Size: "small",
@@ -252,6 +246,106 @@ func TestArtistGetTopTracks_clientRequestFails(t *testing.T) {
require.Zero(actual) 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) { func TestGetParamSignature(t *testing.T) {
params := url.Values{} params := url.Values{}
params.Add("ccc", "CCC") params.Add("ccc", "CCC")

View File

@@ -30,14 +30,11 @@ type (
Name string `xml:"name"` Name string `xml:"name"`
MBID string `xml:"mbid"` MBID string `xml:"mbid"`
URL string `xml:"url"` URL string `xml:"url"`
Image []struct { Image []Image `xml:"image"`
Text string `xml:",chardata"`
Size string `xml:"size,attr"`
} `xml:"image"`
Streamable string `xml:"streamable"` Streamable string `xml:"streamable"`
} }
ArtistImage struct { Image struct {
Text string `xml:",chardata"` Text string `xml:",chardata"`
Size string `xml:"size,attr"` Size string `xml:"size,attr"`
} }
@@ -47,7 +44,7 @@ type (
Name string `xml:"name"` Name string `xml:"name"`
MBID string `xml:"mbid"` MBID string `xml:"mbid"`
URL string `xml:"url"` URL string `xml:"url"`
Image []ArtistImage `xml:"image"` Image []Image `xml:"image"`
Streamable string `xml:"streamable"` Streamable string `xml:"streamable"`
Stats struct { Stats struct {
Listeners string `xml:"listeners"` Listeners string `xml:"listeners"`
@@ -100,9 +97,6 @@ type (
PlayCount int `xml:"playcount"` PlayCount int `xml:"playcount"`
Listeners int `xml:"listeners"` Listeners int `xml:"listeners"`
URL string `xml:"url"` URL string `xml:"url"`
Image []struct { Image []Image `xml:"image"`
Text string `xml:",chardata"`
Size string `xml:"size,attr"`
} `xml:"image"`
} }
) )

View File

@@ -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>