Test ListenBrainz client

This commit is contained in:
Philipp Wolfer 2023-11-11 22:40:43 +01:00
parent cf8a6d2ab6
commit 3ac3fac317
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
5 changed files with 153 additions and 3 deletions

View file

@ -34,7 +34,7 @@ const DefaultItemsPerGet = 25
const MaxItemsPerGet = 1000
type Client struct {
resty *resty.Client
HttpClient *resty.Client
MaxResults int
}
@ -45,7 +45,7 @@ func New(token string) Client {
resty.SetAuthToken(token)
resty.SetHeader("Accept", "application/json")
client := Client{
resty: resty,
HttpClient: resty,
MaxResults: DefaultItemsPerGet,
}
@ -55,7 +55,7 @@ func New(token string) Client {
func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (GetListensResult, error) {
const path = "/user/{username}/listens"
result := &GetListensResult{}
_, err := c.resty.R().
_, err := c.HttpClient.R().
SetPathParam("username", user).
SetQueryParams(map[string]string{
"max_ts": strconv.FormatInt(maxTime.Unix(), 10),

View file

@ -22,14 +22,46 @@ THE SOFTWARE.
package listenbrainz_test
import (
"net/http"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uploadedlobster.com/scotty/backends/listenbrainz"
)
func TestNewClient(t *testing.T) {
token := "foobar123"
client := listenbrainz.New(token)
assert.Equal(t, token, client.HttpClient.Token)
assert.Equal(t, listenbrainz.DefaultItemsPerGet, client.MaxResults)
}
func TestGetListens(t *testing.T) {
defer httpmock.DeactivateAndReset()
token := "thetoken"
client := listenbrainz.New(token)
setupHttpMock(t, client.HttpClient.GetClient(),
"https://api.listenbrainz.org/1/user/outsidecontext/listens",
"testdata/listens.json")
result, err := client.GetListens("outsidecontext", time.Now(), time.Now().Add(-2*time.Hour))
require.NoError(t, err)
assert := assert.New(t)
assert.Equal(2, result.Payload.Count)
assert.Equal("Shadowplay", result.Payload.Listens[0].TrackMetadata.TrackName)
}
func setupHttpMock(t *testing.T, client *http.Client, url string, testDataPath string) {
httpmock.ActivateNonDefault(client)
responder, err := httpmock.NewJsonResponder(200, httpmock.File(testDataPath))
if err != nil {
t.Fatal(err)
}
httpmock.RegisterResponder("GET", url, responder)
}

View file

@ -0,0 +1,115 @@
{
"payload": {
"count": 2,
"latest_listen_ts": 1699718723,
"listens": [
{
"inserted_at": 1699719320,
"listened_at": 1699718723,
"recording_msid": "94794568-ddd5-43be-a770-b6da011c6872",
"track_metadata": {
"additional_info": {
"artist_names": [
"Joy Division"
],
"discnumber": 1,
"duration_ms": 242933,
"isrc": "NLEM80819612",
"music_service": "spotify.com",
"origin_url": "https://open.spotify.com/track/4pzYKPOjn1ITfEanoWIvrn",
"recording_msid": "94794568-ddd5-43be-a770-b6da011c6872",
"release_artist_name": "Warsaw",
"release_artist_names": [
"Warsaw"
],
"spotify_album_artist_ids": [
"https://open.spotify.com/artist/0SS65FajB9S7ZILHdNOCsp"
],
"spotify_album_id": "https://open.spotify.com/album/3kDMRpbBe5eFMMo1pSYFhN",
"spotify_artist_ids": [
"https://open.spotify.com/artist/432R46LaYsJZV2Gmc4jUV5"
],
"spotify_id": "https://open.spotify.com/track/4pzYKPOjn1ITfEanoWIvrn",
"submission_client": "listenbrainz",
"tracknumber": 1
},
"artist_name": "Joy Division",
"mbid_mapping": {
"artist_mbids": [
"9a58fda3-f4ed-4080-a3a5-f457aac9fcdd"
],
"artists": [
{
"artist_credit_name": "Joy Division",
"artist_mbid": "9a58fda3-f4ed-4080-a3a5-f457aac9fcdd",
"join_phrase": ""
}
],
"caa_id": 3880053972,
"caa_release_mbid": "d2f506bb-cfb5-327e-b8d6-cf4036c77cfa",
"recording_mbid": "17ddd699-a35f-4f80-8064-9a807ad2799f",
"recording_name": "Shadowplay",
"release_mbid": "d2f506bb-cfb5-327e-b8d6-cf4036c77cfa"
},
"release_name": "Warsaw",
"track_name": "Shadowplay"
},
"user_name": "outsidecontext"
},
{
"inserted_at": 1699718945,
"listened_at": 1699718480,
"recording_msid": "5b6a3471-8f22-414b-a061-e45627ed26b8",
"track_metadata": {
"additional_info": {
"artist_names": [
"SubRosa"
],
"discnumber": 1,
"duration_ms": 350760,
"isrc": "USN681110018",
"music_service": "spotify.com",
"origin_url": "https://open.spotify.com/track/0L0oz4yFk5hMmo52qAUQRF",
"recording_msid": "5b6a3471-8f22-414b-a061-e45627ed26b8",
"release_artist_name": "SubRosa",
"release_artist_names": [
"SubRosa"
],
"spotify_album_artist_ids": [
"https://open.spotify.com/artist/4hAqIOkN2Q4apnbcOUUb7h"
],
"spotify_album_id": "https://open.spotify.com/album/3mYNFe9G85URf09SmoX2sB",
"spotify_artist_ids": [
"https://open.spotify.com/artist/4hAqIOkN2Q4apnbcOUUb7h"
],
"spotify_id": "https://open.spotify.com/track/0L0oz4yFk5hMmo52qAUQRF",
"submission_client": "listenbrainz",
"tracknumber": 1
},
"artist_name": "SubRosa",
"mbid_mapping": {
"artist_mbids": [
"aa1c41d7-7836-42d0-8e0e-b5d565767db6"
],
"artists": [
{
"artist_credit_name": "SubRosa",
"artist_mbid": "aa1c41d7-7836-42d0-8e0e-b5d565767db6",
"join_phrase": ""
}
],
"caa_id": 6163307004,
"caa_release_mbid": "eb5dec80-ec5d-49a3-a622-3c02eefa0774",
"recording_mbid": "c2374d60-7bfa-44ef-b5dc-f7bc6004b4a7",
"recording_name": "Borrowed Time, Borrowed Eyes",
"release_mbid": "9fba6ca8-4acb-44a9-951a-6c1fb8511443"
},
"release_name": "No Help for the Mighty Ones",
"track_name": "Borrowed Time, Borrowed Eyes"
},
"user_name": "outsidecontext"
}
],
"user_id": "outsidecontext"
}
}

1
go.mod
View file

@ -8,6 +8,7 @@ require (
github.com/go-resty/resty/v2 v2.10.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jarcoal/httpmock v1.3.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect

2
go.sum
View file

@ -126,6 +126,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=