This commit is contained in:
irlndts 2018-02-20 20:00:24 +03:00
parent 6af125badd
commit 7d6af61b78
6 changed files with 32 additions and 14 deletions

View File

@ -8,17 +8,17 @@ import (
func TestArtistService_Artist(t *testing.T) {
expectedId := 1000
d := NewClient(testUserAgent, testToken)
d, _ := NewClient(&Options{})
artist, _, err := d.Artist.Artist(&ArtistParams{Artist_id: "1000"})
check(t, err)
assert(t, artist.Id == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, artist.Id))
assert(t, artist.Id == expectedId, fmt.Sprintf("Release.Title looked for %d, and received %d ", expectedId, artist.Id))
}
func TestArtistService_Releases(t *testing.T) {
expectedArtist := "Dave Clarke"
d := NewClient(testUserAgent, testToken)
d, _ := NewClient(&Options{})
releases, _, err := d.Artist.Releases(&ArtistParams{Artist_id: "1000", Sort: "year", Sort_order: "desc"})
check(t, err)

View File

@ -12,6 +12,7 @@ const (
)
type Options struct {
URL string
Currency string
UserAgent string
Token string
@ -47,7 +48,7 @@ func NewClient(o *Options) (*Client, error) {
}
return &Client{
Release: newReleaseService(header, cur),
Release: newReleaseService(o.URL+"/releases/", header, cur),
Artist: newArtistService(base.New()),
Label: newLabelService(base.New()),
Master: newMasterService(base.New()),
@ -62,8 +63,9 @@ func currency(c string) (string, error) {
switch c {
case "USD", "GBP", "EUR", "CAD", "AUD", "JPY", "CHF", "MXN", "BRL", "NZD", "SEK", "ZAR":
return c, nil
case "":
return "USD", nil
default:
return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.")
}
return "USD", nil
}

View File

@ -8,17 +8,17 @@ import (
func TestLabelService_Label(t *testing.T) {
expectedId := 1000
d := NewClient(testUserAgent, testToken)
d, _ := NewClient(&Options{})
label, _, err := d.Label.Label(&LabelParams{Label_id: "1000"})
check(t, err)
assert(t, label.Id == expectedId, fmt.Sprintf("Release.Title looked for %s, and received %s ", expectedId, label.Id))
assert(t, label.Id == expectedId, fmt.Sprintf("Release.Title looked for %d, and received %d ", expectedId, label.Id))
}
func TestLabelService_Releases(t *testing.T) {
expectedId := "Ghetto Sol"
d := NewClient(testUserAgent, testToken)
d, _ := NewClient(&Options{})
label, _, err := d.Label.Releases(&LabelParams{Label_id: "1000"})
check(t, err)

View File

@ -8,7 +8,7 @@ import (
func TestMasterService_Master(t *testing.T) {
expectedTitle := "Elephant Riddim"
d := NewClient(testUserAgent, testToken)
d, _ := NewClient(nil)
master, _, err := d.Master.Master(&MasterParams{Master_id: "960657"})
check(t, err)
@ -18,7 +18,7 @@ func TestMasterService_Master(t *testing.T) {
func TestMasterService_Versions(t *testing.T) {
expectedTitle := "Stardiver"
d := NewClient(testUserAgent, testToken)
d, _ := NewClient(nil)
versions, _, err := d.Master.Versions(&MasterVersionParams{Master_id: "1000", Page: 1, Per_page: 1})
check(t, err)

View File

@ -51,12 +51,14 @@ type ReqRelease struct {
// ReleaseService ...
type ReleaseService struct {
url string
header *http.Header
currency string
}
func newReleaseService(header *http.Header, currency string) *ReleaseService {
func newReleaseService(url string, header *http.Header, currency string) *ReleaseService {
return &ReleaseService{
url: url,
header: header,
currency: currency,
}
@ -76,7 +78,7 @@ func (s *ReleaseService) Release(releaseID int) (*Release, error) {
}
func (s *ReleaseService) request(path string, params url.Values, resp interface{}) error {
r, err := http.NewRequest("GET", discogsAPI+"releases/"+path+"?"+params.Encode(), nil)
r, err := http.NewRequest("GET", s.url+path+"?"+params.Encode(), nil)
if err != nil {
return err
}

View File

@ -2,13 +2,27 @@ package discogs
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestReleaseService_Release(t *testing.T) {
func ReleaseServer(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, `{"title":"Elephant Riddim"}`)
}
func TestReleaseServiceRelease(t *testing.T) {
expectedTitle := "Elephant Riddim"
d := NewClient(testUserAgent, testToken)
ts := httptest.NewServer(http.HandlerFunc(ReleaseServer))
defer ts.Close()
d, err := NewClient(&Options{URL: ts.URL})
if err != nil {
t.Fatalf("failed to create client: %s", err)
}
release, err := d.Release.Release(8138518)
check(t, err)