Added several tests
This commit is contained in:
		@@ -2,7 +2,6 @@ package discogs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
@@ -33,7 +32,7 @@ func NewClient(o *Options) (*Client, error) {
 | 
			
		||||
	header = &http.Header{}
 | 
			
		||||
 | 
			
		||||
	if o == nil || o.UserAgent == "" {
 | 
			
		||||
		return nil, fmt.Errorf("failed to set user-agent")
 | 
			
		||||
		return nil, ErrUserAgentInvalid
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	header.Add("User-Agent", o.UserAgent)
 | 
			
		||||
@@ -68,7 +67,7 @@ func currency(c string) (string, error) {
 | 
			
		||||
	case "":
 | 
			
		||||
		return "USD", nil
 | 
			
		||||
	default:
 | 
			
		||||
		return "", fmt.Errorf("invalid currency abbreviation")
 | 
			
		||||
		return "", ErrCurrencyNotSupported
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -29,3 +29,63 @@ func initDiscogsClient(t *testing.T, options *Options) *Client {
 | 
			
		||||
 | 
			
		||||
	return client
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestNewClient(t *testing.T) {
 | 
			
		||||
	tests := map[string]struct {
 | 
			
		||||
		options *Options
 | 
			
		||||
		err     error
 | 
			
		||||
	}{
 | 
			
		||||
		"normal": {&Options{
 | 
			
		||||
			UserAgent: testUserAgent,
 | 
			
		||||
			Currency:  "USD",
 | 
			
		||||
		}, nil},
 | 
			
		||||
		"incorrect user-agent": {&Options{
 | 
			
		||||
			UserAgent: "",
 | 
			
		||||
			Currency:  "USD",
 | 
			
		||||
		}, ErrUserAgentInvalid},
 | 
			
		||||
		"incorrect currency": {&Options{
 | 
			
		||||
			UserAgent: testUserAgent,
 | 
			
		||||
			Currency:  "RUR",
 | 
			
		||||
		}, ErrCurrencyNotSupported},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for name, tt := range tests {
 | 
			
		||||
		t.Run(name, func(t *testing.T) {
 | 
			
		||||
			if _, err := NewClient(tt.options); err != tt.err {
 | 
			
		||||
				t.Errorf("err got=%s; want=%s", err, tt.err)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestCurrency(t *testing.T) {
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		currency string
 | 
			
		||||
		want     string
 | 
			
		||||
		err      error
 | 
			
		||||
	}{
 | 
			
		||||
		{currency: "", want: "USD", err: nil},
 | 
			
		||||
		{currency: "USD", want: "USD", err: nil},
 | 
			
		||||
		{currency: "GBP", want: "GBP", err: nil},
 | 
			
		||||
		{currency: "EUR", want: "EUR", err: nil},
 | 
			
		||||
		{currency: "CAD", want: "CAD", err: nil},
 | 
			
		||||
		{currency: "AUD", want: "AUD", err: nil},
 | 
			
		||||
		{currency: "JPY", want: "JPY", err: nil},
 | 
			
		||||
		{currency: "CHF", want: "CHF", err: nil},
 | 
			
		||||
		{currency: "MXN", want: "MXN", err: nil},
 | 
			
		||||
		{currency: "BRL", want: "BRL", err: nil},
 | 
			
		||||
		{currency: "NZD", want: "NZD", err: nil},
 | 
			
		||||
		{currency: "SEK", want: "SEK", err: nil},
 | 
			
		||||
		{currency: "ZAR", want: "ZAR", err: nil},
 | 
			
		||||
		{currency: "RUR", want: "", err: ErrCurrencyNotSupported},
 | 
			
		||||
	}
 | 
			
		||||
	for i, tt := range tests {
 | 
			
		||||
		cur, err := currency(tt.currency)
 | 
			
		||||
		if err != tt.err {
 | 
			
		||||
			t.Errorf("#%d err got=%s; want=%s", i, err, tt.err)
 | 
			
		||||
		}
 | 
			
		||||
		if cur != tt.want {
 | 
			
		||||
			t.Errorf("#%d currency got=%s; want=%s", i, cur, tt.want)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										41
									
								
								errors.go
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								errors.go
									
									
									
									
									
								
							@@ -2,39 +2,20 @@ package discogs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// APIError represents a Discogs API Error response
 | 
			
		||||
type APIError struct {
 | 
			
		||||
	Message string `json:"message"`
 | 
			
		||||
// Error represents a Discogs API error
 | 
			
		||||
type Error struct {
 | 
			
		||||
	Message string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Error ...
 | 
			
		||||
func (e APIError) Error() string {
 | 
			
		||||
	if e.Message != "" {
 | 
			
		||||
		return fmt.Sprintf("discogs: %v", e.Message)
 | 
			
		||||
	}
 | 
			
		||||
	return ""
 | 
			
		||||
func (e *Error) Error() string {
 | 
			
		||||
	return fmt.Sprintf("%s", strings.ToLower(e.Message))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Empty returns true if empty. Otherwise, at least 1 error message/code is
 | 
			
		||||
// present and false is returned.
 | 
			
		||||
func (e APIError) Empty() bool {
 | 
			
		||||
	if e.Message == "" {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// relevantError returns any non-nil http-related error (creating the request,
 | 
			
		||||
// getting the response, decoding) if any. If the decoded apiError is non-zero
 | 
			
		||||
// the apiError is returned. Otherwise, no errors occurred, returns nil.
 | 
			
		||||
func relevantError(httpError error, apiError APIError) error {
 | 
			
		||||
	if httpError != nil {
 | 
			
		||||
		return httpError
 | 
			
		||||
	}
 | 
			
		||||
	if apiError.Empty() {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return apiError
 | 
			
		||||
}
 | 
			
		||||
// APIErrors
 | 
			
		||||
var (
 | 
			
		||||
	ErrCurrencyNotSupported = &Error{"currency does not supported"}
 | 
			
		||||
	ErrUserAgentInvalid     = &Error{"invalid user-agent"}
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,7 @@ import (
 | 
			
		||||
func main() {
 | 
			
		||||
	d, err := discogs.NewClient(&discogs.Options{
 | 
			
		||||
		UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
 | 
			
		||||
		Currency:  "EUR",
 | 
			
		||||
		Currency:  "AAA",
 | 
			
		||||
		Token:     "",
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user