mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-25 22:07:56 +02:00
Test ListenBrainz client
This commit is contained in:
parent
cf8a6d2ab6
commit
3ac3fac317
5 changed files with 153 additions and 3 deletions
|
@ -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),
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
115
backends/listenbrainz/testdata/listens.json
vendored
Normal file
115
backends/listenbrainz/testdata/listens.json
vendored
Normal 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"
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue