This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
go-discogs/database_test.go

109 lines
2.5 KiB
Go
Raw Permalink Normal View History

2016-03-10 14:25:55 +00:00
package discogs
import (
"encoding/json"
2018-02-20 17:00:24 +00:00
"io"
"log"
2018-02-20 17:00:24 +00:00
"net/http"
"net/http/httptest"
2016-03-10 14:25:55 +00:00
"testing"
"github.com/google/go-cmp/cmp"
2016-03-10 14:25:55 +00:00
)
func DatabaseServer(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
switch r.URL.Path {
case "/releases/8138518":
w.WriteHeader(http.StatusOK)
if _, err := io.WriteString(w, releaseJson); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
case "/masters/718441":
w.WriteHeader(http.StatusOK)
if _, err := io.WriteString(w, masterJson); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
case "/artists/38661":
w.WriteHeader(http.StatusOK)
if _, err := io.WriteString(w, artistJson); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func compareJson(t *testing.T, got, want string) {
var g, w interface{}
if err := json.Unmarshal([]byte(got), &g); err != nil {
log.Fatalf("failed to unmarshal json: %s", err)
}
if err := json.Unmarshal([]byte(want), &w); err != nil {
log.Fatalf("failed to unmarshal json: %s", err)
}
if diff := cmp.Diff(g, w); diff != "" {
t.Errorf("(-want +got)\n%s", diff)
2019-06-06 12:18:10 +00:00
}
2018-02-20 17:00:24 +00:00
}
func TestDatabaseServiceRelease(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer))
2018-02-20 17:00:24 +00:00
defer ts.Close()
2018-03-12 16:30:58 +00:00
d := initDiscogsClient(t, &Options{URL: ts.URL})
2020-05-31 18:45:30 +00:00
release, err := d.Release(8138518)
2018-02-20 17:00:24 +00:00
if err != nil {
2018-03-12 16:30:58 +00:00
t.Fatalf("failed to get release: %s", err)
2018-02-20 17:00:24 +00:00
}
2016-03-10 14:25:55 +00:00
json, err := json.Marshal(release)
if err != nil {
t.Fatalf("failed to marshal release: %s", err)
}
compareJson(t, string(json), releaseJson)
}
func TestDatabaseServiceMaster(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer))
defer ts.Close()
d := initDiscogsClient(t, &Options{URL: ts.URL})
2020-05-31 18:45:30 +00:00
master, err := d.Master(718441)
if err != nil {
t.Fatalf("failed to get master: %s", err)
}
json, err := json.Marshal(master)
if err != nil {
t.Fatalf("failed to marshal release: %s", err)
}
compareJson(t, string(json), masterJson)
}
func TestDatabaseServiceArtist(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer))
defer ts.Close()
d := initDiscogsClient(t, &Options{URL: ts.URL})
2020-05-31 18:45:30 +00:00
artist, err := d.Artist(38661)
if err != nil {
t.Fatalf("failed to get master: %s", err)
}
json, err := json.Marshal(artist)
if err != nil {
t.Fatalf("failed to marshal artist: %s", err)
2018-03-12 16:30:58 +00:00
}
compareJson(t, string(json), artistJson)
2016-03-10 14:25:55 +00:00
}