-
Notifications
You must be signed in to change notification settings - Fork 1
/
hop_test.go
97 lines (81 loc) · 1.62 KB
/
hop_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package brewerydb
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"testing"
)
func TestHopGet(t *testing.T) {
setup()
defer teardown()
data := loadTestData("hop.get.json", t)
defer data.Close()
const id = 42
mux.HandleFunc("/hop/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "GET")
checkURLSuffix(t, r, strconv.Itoa(id))
io.Copy(w, data)
})
h, err := client.Hop.Get(id)
if err != nil {
t.Fatal(err)
}
if h.ID != id {
t.Fatalf("Hop ID = %v, want %v", h.ID, id)
}
testBadURL(t, func() error {
_, err := client.Hop.Get(id)
return err
})
}
func TestHopList(t *testing.T) {
setup()
defer teardown()
data := loadTestData("hop.list.json", t)
defer data.Close()
const page = 1
mux.HandleFunc("/hops", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "GET")
checkPage(t, r, page)
io.Copy(w, data)
})
hl, err := client.Hop.List(page)
if err != nil {
t.Fatal(err)
}
if len(hl.Hops) <= 0 {
t.Fatal("Expected >0 hops")
}
c := "hop"
for _, h := range hl.Hops {
if c != h.Category {
t.Fatalf("Hop Category = %s, wanted %s", h.Category, c)
}
}
testBadURL(t, func() error {
_, err := client.Hop.List(page)
return err
})
}
// Get a specific variety of hop with a given ID
func ExampleHopService_Get() {
c := NewClient(os.Getenv("BREWERYDB_API_KEY"))
h, err := c.Hop.Get(84)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", h)
}
// Get all types of hops
func ExampleHopService_List() {
c := NewClient(os.Getenv("BREWERYDB_API_KEY"))
hl, err := c.Hop.List(1)
if err != nil {
panic(err)
}
for _, h := range hl.Hops {
fmt.Println(h.Name)
}
}