more tests and standardize struct field casing
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -9,7 +10,10 @@ import (
|
||||
|
||||
func testServer(expectedkey string) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Query().Get("bibkeys") == expectedkey {
|
||||
if r.URL.Query().Get("bibkeys") == "SPECIAL:MISSING" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("{}"))
|
||||
} else if r.URL.Query().Get("bibkeys") == expectedkey {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(fmt.Sprintf(exampleResponse, expectedkey)))
|
||||
} else {
|
||||
@@ -34,6 +38,7 @@ func TestFetch(t *testing.T) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchFailure(t *testing.T) {
|
||||
r := testServer("ISBN:1234567890")
|
||||
defer r.Close()
|
||||
@@ -41,12 +46,72 @@ func TestFetchFailure(t *testing.T) {
|
||||
c := Client{}
|
||||
c.SetAPIPath(r.URL)
|
||||
_, err := c.GetByISBN("9780980200447")
|
||||
if err == nil {
|
||||
t.Log("expected error, received nil")
|
||||
if err == nil || !errors.Is(err, NetworkError) {
|
||||
t.Logf("expected error, received nil: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestBookNotFound(t *testing.T) {
|
||||
r := testServer("")
|
||||
defer r.Close()
|
||||
|
||||
c := Client{}
|
||||
c.SetAPIPath(r.URL)
|
||||
_, err := c.GetByRawKey("SPECIAL:MISSING")
|
||||
if err == nil || !errors.Is(err, NotFoundError) {
|
||||
t.Logf("expected error, received nil: %v", err)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyRejections(t *testing.T) {
|
||||
c := Client{}
|
||||
cases := map[string][]string{
|
||||
"ISBN": {"", "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789", "12345678901", "123456789012", "12345678901234"},
|
||||
"OCLC": {""},
|
||||
"LCCN": {""},
|
||||
"OLID": {""},
|
||||
}
|
||||
for id, ts := range cases {
|
||||
switch id {
|
||||
case "ISBN":
|
||||
for i := range ts {
|
||||
_, err := c.GetByISBN(ts[i])
|
||||
if !errors.Is(err, FormatError) {
|
||||
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
case "OCLC":
|
||||
for i := range ts {
|
||||
_, err := c.GetByOCLC(ts[i])
|
||||
if !errors.Is(err, FormatError) {
|
||||
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
case "LCCN":
|
||||
for i := range ts {
|
||||
_, err := c.GetByLCCN(ts[i])
|
||||
if !errors.Is(err, FormatError) {
|
||||
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
case "OLID":
|
||||
for i := range ts {
|
||||
_, err := c.GetByOLID(ts[i])
|
||||
if !errors.Is(err, FormatError) {
|
||||
t.Logf("%s received %v, expected %v", id, err, FormatError)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// example comes from the official API docs
|
||||
var exampleResponse = `{
|
||||
"%s": {
|
||||
"publishers": [
|
||||
|
Reference in New Issue
Block a user