-
Notifications
You must be signed in to change notification settings - Fork 1
/
location_test.go
219 lines (187 loc) · 5.64 KB
/
location_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package brewerydb
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"testing"
)
func TestLocationGet(t *testing.T) {
setup()
defer teardown()
data := loadTestData("location.get.json", t)
defer data.Close()
const id = "z9H6HJ"
mux.HandleFunc("/location/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "GET")
checkURLSuffix(t, r, id)
io.Copy(w, data)
})
l, err := client.Location.Get(id)
if err != nil {
t.Fatal(err)
}
if l.ID != id {
t.Fatalf("Location ID = %v, want %v", l.ID, id)
}
testBadURL(t, func() error {
_, err := client.Location.Get(id)
return err
})
}
func TestLocationList(t *testing.T) {
setup()
defer teardown()
data := loadTestData("location.list.json", t)
defer data.Close()
const (
page = 1
region = "Maryland"
)
mux.HandleFunc("/locations", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "GET")
checkPage(t, r, page)
if v := r.FormValue("region"); v != region {
t.Fatalf("Request.FormValue region = %v, want %v", v, region)
}
// TODO: check more request query values
io.Copy(w, data)
})
ll, err := client.Location.List(&LocationListRequest{Page: page, Region: region})
if err != nil {
t.Fatal(err)
}
if len(ll.Locations) <= 0 {
t.Fatal("Expected >0 locations")
}
for _, l := range ll.Locations {
if n := 6; n != len(l.ID) {
t.Fatalf("Location ID len = %d, wanted %d", len(l.ID), n)
}
if l.Latitude == 0.0 {
t.Fatal("Expected non-zero latitude")
}
if l.Longitude == 0.0 {
t.Fatal("Expected non-zero longitude")
}
}
testBadURL(t, func() error {
_, err := client.Location.List(&LocationListRequest{Page: page, Region: region})
return err
})
}
func makeTestLocation() *Location {
return &Location{
ID: "z9H6Hj",
Name: "Bethesda",
StreetAddress: "7900 Norfolk Ave",
Locality: "Bethesda",
Region: "Maryland",
PostalCode: "20814",
Phone: "301-652-1311",
Website: "http://www.rockbottom.com/bethesda",
HoursOfOperationExplicit: []string{"Sunday - Thursday: 11am - 1am", "Friday - Saturday: 11am - 2am"},
Latitude: 38.988988,
Longitude: -77.097413,
IsPrimary: true,
InPlanning: false,
IsClosed: false,
OpenToPublic: true,
LocationType: "brewpub",
LocationTypeDisplay: "Brewpub",
CountryISOCode: "US",
Country: Country{
ISOCode: "US",
Name: "UNITED STATES",
DisplayName: "United States",
ISOThree: "USA",
NumberCode: 840,
},
YearOpened: "1980",
BreweryID: "D1UQzj",
Brewery: Brewery{},
}
}
func TestLocationUpdate(t *testing.T) {
setup()
defer teardown()
location := makeTestLocation()
mux.HandleFunc("/location/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "PUT")
checkURLSuffix(t, r, location.ID)
if err := r.ParseForm(); err != nil {
http.Error(w, "failed to parse form", http.StatusBadRequest)
}
checkPostFormValue(t, r, "name", location.Name)
checkPostFormValue(t, r, "streetAddress", location.StreetAddress)
checkPostFormValue(t, r, "locality", location.Locality)
checkPostFormValue(t, r, "region", location.Region)
checkPostFormValue(t, r, "postalCode", location.PostalCode)
checkPostFormValue(t, r, "phone", location.Phone)
checkPostFormValue(t, r, "website", location.Website)
checkPostFormValue(t, r, "hoursOfOperationExplicit", location.HoursOfOperationExplicit[0])
checkPostFormValue(t, r, "latitude", fmt.Sprintf("%f", location.Latitude))
checkPostFormValue(t, r, "longitude", fmt.Sprintf("%f", location.Longitude))
checkPostFormValue(t, r, "isPrimary", "Y")
checkPostFormValue(t, r, "openToPublic", "Y")
checkPostFormValue(t, r, "locationType", string(location.LocationType))
checkPostFormValue(t, r, "countryIsoCode", location.CountryISOCode)
// Check that fields tagged with "-" or "omitempty" are NOT encoded
checkPostFormDNE(t, r, "id", "ID", "extendedAddress",
"ExtendedAddress", "hoursOfOperation", "hoursOfOperationNotes", "tourInfo",
"LocationTypeDisplay", "country", "Country", "yearClosed",
"breweryID", "BreweryID", "brewery", "Brewery",
"status", "Status", "inPlanning", "isClosed")
})
if err := client.Location.Update(location.ID, location); err != nil {
t.Fatal(err)
}
if client.Location.Update(location.ID, nil) == nil {
t.Fatal("expected error regarding nil parameter")
}
testBadURL(t, func() error {
return client.Location.Update(location.ID, location)
})
}
func TestLocationDelete(t *testing.T) {
setup()
defer teardown()
const id = "z9H6HJ"
mux.HandleFunc("/location/", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "DELETE")
split := strings.Split(r.URL.Path, "/")
if split[1] != "location" {
t.Fatal("bad URL, expected \"/location/:locationId\"")
}
if split[2] != id {
http.Error(w, "invalid Location ID", http.StatusNotFound)
}
})
if err := client.Location.Delete(id); err != nil {
t.Fatal(err)
}
if err := client.Location.Delete("******"); err == nil {
t.Fatal("expected HTTP 404")
}
testBadURL(t, func() error {
return client.Location.Delete(id)
})
}
// List all brewpubs in Boston
func ExampleLocationService_List() {
c := NewClient(os.Getenv("BREWERYDB_API_KEY"))
req := &LocationListRequest{
Locality: "Boston",
Region: "Massachusetts",
LocationType: LocationBrewpub,
}
for l, err := c.Location.List(req); l.CurrentPage < l.NumberOfPages; req.Page++ {
if err != nil {
panic(err)
}
for _, loc := range l.Locations {
fmt.Println(loc.Name)
}
}
}