Skip to content

Commit

Permalink
DSCGS-35 More and fixed data from database, tests (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
irlndts authored Nov 5, 2019
1 parent d987189 commit f499b48
Show file tree
Hide file tree
Showing 31 changed files with 3,983 additions and 46 deletions.
64 changes: 38 additions & 26 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Release struct {
Title string `json:"title"`
ID int `json:"id"`
Artists []ArtistSource `json:"artists"`
ArtistsSort string `json:"artists_sort"`
DataQuality string `json:"data_quality"`
Thumb string `json:"thumb"`
Community Community `json:"community"`
Expand All @@ -53,13 +54,13 @@ type Release struct {
Released string `json:"released"`
ReleasedFormatted string `json:"released_formatted"`
ResourceURL string `json:"resource_url"`
// Series
Status string `json:"status"`
Styles []string `json:"styles"`
Tracklist []Track `json:"tracklist"`
URI string `json:"uri"`
Videos []Video `json:"videos"`
Year int `json:"year"`
Series []string `json:"series"`
Status string `json:"status"`
Styles []string `json:"styles"`
Tracklist []Track `json:"tracklist"`
URI string `json:"uri"`
Videos []Video `json:"videos"`
Year int `json:"year"`
}

// Release returns release by release's ID
Expand All @@ -85,18 +86,23 @@ func (s *DatabaseService) ReleaseRating(releaseID int) (*ReleaseRating, error) {
return rating, err
}

// Artist ...
// Artist resource represents a person in the Discogs database
// who contributed to a Release in some capacity.
// More information https://www.discogs.com/developers#page:database,header:database-artist
type Artist struct {
ID int `json:"id"`
Name string `json:"name"`
Realname string `json:"realname"`
Members []Member `json:"members,omitempty"`
Aliases []Alias `json:"aliases,omitempty"`
Namevariations []string `json:"namevariations"`
Images []Image `json:"images"`
Profile string `json:"profile"`
ReleasesURL string `json:"releases_url"`
ResourceURL string `json:"resource_url"`
URI string `json:"uri"`
URLs []string `json:"urls"`
DataQuality string `json:"data_quality"`
ID int `json:"id"`
Images []Image `json:"images"`
Members []Member `json:"members"`
}

// Artist represents a person in the discogs database
Expand Down Expand Up @@ -157,22 +163,28 @@ func (s *DatabaseService) LabelReleases(labelID int, pagination *Pagination) (*L

// Master resource represents a set of similar releases.
// Masters (also known as `master releases`) have a `main release` which is often the chronologically earliest.
// More information https://www.discogs.com/developers#page:database,header:database-master-release
type Master struct {
Styles []string `json:"styles"`
Genres []string `json:"genres"`
Videos []Video `json:"videos"`
Title string `json:"title"`
MainRelease int `json:"main_release"`
MainReleaseURL string `json:"main_release_url"`
URI string `json:"uri"`
Artists []Artist `json:"artists"`
VersionURL string `json:"version_url"`
Year int `json:"year"`
Images []Image `json:"images"`
ResourceURL string `json:"resource_url"`
Tracklist []Track `json:"tracklist"`
ID int `json:"id"`
DataQuality string `json:"data_quality"`
ID int `json:"id"`
Styles []string `json:"styles"`
Genres []string `json:"genres"`
Title string `json:"title"`
Year int `json:"year"`
Tracklist []Track `json:"tracklist"`
Notes string `json:"notes"`
Artists []ArtistSource `json:"artists"`
Images []Image `json:"images"`
Videos []Video `json:"videos"`
NumForSale int `json:"num_for_sale"`
LowestPrice float64 `json:"lowest_price"`
URI string `json:"uri"`
MainRelease int `json:"main_release"`
MainReleaseURL string `json:"main_release_url"`
MostRecentRelease int `json:"most_recent_release"`
MostRecentReleaseURL string `json:"most_recent_release_url"`
VersionsURL string `json:"versions_url"`
ResourceURL string `json:"resource_url"`
DataQuality string `json:"data_quality"`
}

// Master returns a master release
Expand Down
95 changes: 86 additions & 9 deletions database_test.go
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)
}
4 changes: 2 additions & 2 deletions examples/discogs_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func main() {
return
}

release, err := d.Search.Search(discogs.SearchRequest{Q: "middle", PerPage: 3})
master, err := d.Database.Master(718441)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", release)
fmt.Printf("%+v\n", master)
}
4 changes: 3 additions & 1 deletion go.mod
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
2 changes: 2 additions & 0 deletions go.sum
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=
24 changes: 16 additions & 8 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ type Track struct {
Position string `json:"position"`
Title string `json:"title"`
Type string `json:"type_"`
Extraartists []ArtistSource `json:"extraartists"`
Artists []ArtistSource `json:"artists"`
Extraartists []ArtistSource `json:"extraartists,omitempty"`
Artists []ArtistSource `json:"artists,omitempty"`
}

// LabelSource ...
type LabelSource struct {
Catno string `json:"catno"`
EntityType string `json:"entity_type"`
ID int `json:"id"`
Name string `json:"name"`
ResourceURL string `json:"resource_url"`
Catno string `json:"catno"`
EntityType string `json:"entity_type"`
EntityTypeName string `json:"entity_type_name"`
ID int `json:"id"`
Name string `json:"name"`
ResourceURL string `json:"resource_url"`
}

// Identifier ...
Expand Down Expand Up @@ -80,7 +81,7 @@ type Company struct {
// Community ...
type Community struct {
Contributors []Contributor `json:"contributors"`
DataQuality string `json:"string"`
DataQuality string `json:"data_quality"`
Have int `json:"have"`
Rating Rating `json:"rating"`
Status string `json:"status"`
Expand Down Expand Up @@ -143,6 +144,13 @@ type Member struct {
ResourceURL string `json:"resource_url"`
}

// Alias ...
type Alias struct {
ID int `json:"id"`
Name string `json:"name"`
ResourceURL string `json:"resource_url"`
}

// Sublable ...
type Sublable struct {
ResourceURL string `json:"url"`
Expand Down
Loading

0 comments on commit f499b48

Please sign in to comment.