55 lines
2.0 KiB
Go
55 lines
2.0 KiB
Go
package qobuz
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestSearchAndGetAlbum(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.URL.Query().Get("app_id"); got != "123" {
|
|
t.Errorf("app_id = %q", got)
|
|
}
|
|
switch r.URL.Path {
|
|
case "/album/search":
|
|
if r.URL.Query().Get("query") != "Popfly" {
|
|
t.Errorf("query = %q", r.URL.Query().Get("query"))
|
|
}
|
|
_, _ = w.Write([]byte(`{"albums":{"items":[{"id":"q1","title":"Popfly","artist":{"name":"Michael Nau"},"tracks_count":10,"upc":"0617308063523","release_date_original":"2026-10-23","maximum_bit_depth":24,"maximum_sampling_rate":96}],"total":1}}`))
|
|
case "/album/get":
|
|
if r.URL.Query().Get("album_id") != "q1" {
|
|
t.Errorf("album_id = %q", r.URL.Query().Get("album_id"))
|
|
}
|
|
_, _ = w.Write([]byte(`{"id":"q1","title":"Popfly","artist":{"name":"Michael Nau"},"upc":"0617308063523","release_date_original":"2026-10-23","tracks_count":2,"tracks":{"items":[{"id":1,"title":"Lost Station Hideaway","track_number":1,"media_number":1,"isrc":"us33x2624501","duration":250},{"id":2,"title":"Hi Beam","track_number":2,"media_number":1,"isrc":"US33X2624502","duration":193}]}}`))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := &Client{http: server.Client(), appID: "123", apiBase: server.URL}
|
|
albums, err := client.SearchAlbums(context.Background(), "Popfly", 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(albums) != 1 {
|
|
t.Fatalf("albums = %d", len(albums))
|
|
}
|
|
if albums[0].ID != "q1" || albums[0].Artist != "Michael Nau" || albums[0].TrackCount != 10 || albums[0].Year != 2026 {
|
|
t.Fatalf("unexpected album: %+v", albums[0])
|
|
}
|
|
|
|
full, err := client.GetAlbum(context.Background(), "q1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if full.Title != "Popfly" || len(full.Tracks) != 2 {
|
|
t.Fatalf("unexpected album: %+v", full)
|
|
}
|
|
if full.Tracks[0].ISRC != "US33X2624501" || full.Tracks[0].TrackNumber != 1 {
|
|
t.Fatalf("unexpected track: %+v", full.Tracks[0])
|
|
}
|
|
}
|