-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DSCGS-35 More and fixed data from database, tests (#36)
- Loading branch information
Showing
31 changed files
with
3,983 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,108 @@ | ||
package discogs | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func ReleaseServer(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
if _, err := io.WriteString(w, `{"title":"Elephant Riddim"}`); err != nil { | ||
panic(err) | ||
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 TestReleaseServiceRelease(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(ReleaseServer)) | ||
func TestDatabaseServiceRelease(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(DatabaseServer)) | ||
defer ts.Close() | ||
|
||
expectedTitle := "Elephant Riddim" | ||
d := initDiscogsClient(t, &Options{URL: ts.URL}) | ||
release, err := d.Database.Release(8138518) | ||
if err != nil { | ||
t.Fatalf("failed to get release: %s", err) | ||
} | ||
|
||
if release.Title != expectedTitle { | ||
t.Fatalf("release title got=%s want=%s ", expectedTitle, release.Title) | ||
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.Database.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.Database.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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/irlndts/go-discogs | ||
|
||
go 1.12 | ||
go 1.13.4 | ||
|
||
require github.com/google/go-cmp v0.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= | ||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.