/
api_test.go
191 lines (159 loc) · 5.47 KB
/
api_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
package testifytutorial
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
"google.golang.org/appengine/datastore"
)
func TestAddLocation(t *testing.T) {
inst, err := aetest.NewInstance(
&aetest.Options{StronglyConsistentDatastore: true})
if err != nil {
t.Fatalf("Error creating aetest instance: %v", err)
}
defer inst.Close() // Make sure the aetest instance always closes
rt := initRouter() // Get our router
loc, err := json.Marshal(&Location{
Name: "Cambridge Fresh Pond",
Lat: 42.385658,
Lng: -71.149308,
})
if err != nil {
t.Errorf("Error marshalling Location into JSON: %v", err)
}
req, err := inst.NewRequest("POST", "/add-location",
ioutil.NopCloser(bytes.NewBuffer(loc)))
if err != nil {
t.Fatalf("Error preparing request: %v", err)
}
rec := httptest.NewRecorder()
rt.ServeHTTP(rec, req)
const expectedResponse = `{"addLocation":"success"}`
if string(rec.Body.Bytes()) != expectedResponse {
t.Errorf("Expected response to be %s, got %s",
expectedResponse, string(rec.Body.Bytes()))
}
dbReq, err := inst.NewRequest("GET", "/", nil)
if err != nil {
t.Fatalf("Error preparing request: %v", err)
}
ctx := appengine.NewContext(dbReq)
q := datastore.NewQuery("Location")
numLocs, err := q.Count(ctx)
if err != nil {
t.Fatalf("Error preparing request: %v", err)
}
if numLocs != 1 {
t.Errorf("Expected number of locations to be 1, got %d", numLocs)
}
}
func TestAddLocationTestify(t *testing.T) {
inst, err := aetest.NewInstance(
&aetest.Options{StronglyConsistentDatastore: true})
require.Nil(t, err, "Error creating aetest instance: %v", err)
defer inst.Close()
rt := initRouter()
loc, err := json.Marshal(&Location{
Name: "Cambridge Fresh Pond",
Lat: 42.5,
Lng: -71.5,
})
assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)
req, err := inst.NewRequest("POST", "/add-location",
ioutil.NopCloser(bytes.NewBuffer(loc)))
require.Nil(t, err, "Error preparing request: %v", err)
rec := httptest.NewRecorder()
rt.ServeHTTP(rec, req)
const expectedResponse = `{"addLocation":"success"}`
assert.Equal(t, expectedResponse, string(rec.Body.Bytes()),
"Expected response to be %s, got %s", expectedResponse, string(rec.Body.Bytes()))
dbReq, err := inst.NewRequest("GET", "/", nil)
require.Nil(t, err, "Error preparing request: %v", err)
ctx := appengine.NewContext(dbReq)
q := datastore.NewQuery("Location")
numLocs, err := q.Count(ctx)
require.Nil(t, err, "Error preparing request: %v", err)
assert.Equal(t, 1, numLocs, "Expected number of locations to be 1, got %d", numLocs)
}
func assertExpectedGot(t *testing.T, expected, got interface{}, msg string) {
assert.Equal(t, expected, got, msg, expected, got)
}
func TestAddLocationExpectedGot(t *testing.T) {
inst, err := aetest.NewInstance(
&aetest.Options{StronglyConsistentDatastore: true})
require.Nil(t, err, "Error creating aetest instance: %v", err)
defer inst.Close()
rt := initRouter()
loc, err := json.Marshal(&Location{
Name: "Cambridge Fresh Pond",
Lat: 42.5,
Lng: -71.5,
})
assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)
req, err := inst.NewRequest("POST", "/add-location",
ioutil.NopCloser(bytes.NewBuffer(loc)))
require.Nil(t, err, "Error preparing request: %v", err)
rec := httptest.NewRecorder()
rt.ServeHTTP(rec, req)
const expectedResponse = `{"addLocation":"success"}`
assertExpectedGot(t, expectedResponse, string(rec.Body.Bytes()),
"Expected response to be %s, got %s")
dbReq, err := inst.NewRequest("GET", "/", nil)
require.Nil(t, err, "Error preparing request: %v", err)
ctx := appengine.NewContext(dbReq)
q := datastore.NewQuery("Location")
numLocs, err := q.Count(ctx)
require.Nil(t, err, "Error preparing request: %v", err)
assertExpectedGot(t, 1, numLocs, "Expected number of locations to be %d, got %d")
}
type TestSuite struct {
suite.Suite
inst aetest.Instance
rt *mux.Router
}
func (s *TestSuite) SetupSuite() {
var err error
s.inst, err = aetest.NewInstance(
&aetest.Options{StronglyConsistentDatastore: true})
require.Nil(s.T(), err, "Error creating aetest instance: %v", err)
s.rt = initRouter()
}
func (s *TestSuite) TearDownSuite() {
err := s.inst.Close()
assert.Nil(s.T(), err, "Error closing aetest instance: %v", err)
}
func (s *TestSuite) TestAddLocation() {
t := s.T()
loc, err := json.Marshal(&Location{
Name: "Cambridge Fresh Pond",
Lat: 42.5,
Lng: -71.5,
})
assert.Nil(t, err, "Error marshalling Location into JSON: %v", err)
req, err := s.inst.NewRequest("POST", "/add-location",
ioutil.NopCloser(bytes.NewBuffer(loc)))
require.Nil(t, err, "Error preparing request: %v", err)
rec := httptest.NewRecorder()
s.rt.ServeHTTP(rec, req)
const expectedResponse = `{"addLocation":"success"}`
assert.Equal(t, expectedResponse, string(rec.Body.Bytes()),
"Expected response to be %s, got %s", expectedResponse, string(rec.Body.Bytes()))
dbReq, err := s.inst.NewRequest("GET", "/", nil)
require.Nil(t, err, "Error preparing request: %v", err)
ctx := appengine.NewContext(dbReq)
q := datastore.NewQuery("Location")
numLocs, err := q.Count(ctx)
require.Nil(t, err, "Error preparing request: %v", err)
assert.Equal(t, 1, numLocs, "Expected number of locations to be 1, got %d", numLocs)
}
func TestRunSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}