forked from irlndts/go-discogs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_test.go
108 lines (92 loc) · 2.49 KB
/
database_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package discogs
import (
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
)
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)
}
}
func TestDatabaseServiceRelease(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer))
defer ts.Close()
d := initDiscogsClient(t, &Options{URL: ts.URL})
release, err := d.Release(8138518)
if err != nil {
t.Fatalf("failed to get release: %s", err)
}
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})
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})
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)
}
compareJson(t, string(json), artistJson)
}