-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_test.go
288 lines (214 loc) · 5.98 KB
/
app_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
// helper to set up testing json file and tell the app to use it
func setupDataFileForTest() {
copy("data.json.example", "test.json")
setupGlobalOptions("test.json")
}
// this is ridiculous. this is how to copy a file in go.
func copy(src string, dest string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func TestRootRoute(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response was incorrect, got: %d, want: 200.", w.Code)
}
}
func TestCollectionRoute(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/notes", nil)
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response was incorrect, got: %d, want: 200.", w.Code)
}
}
func TestCollectionRouteWhenNoneFound(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/derp", nil)
router.ServeHTTP(w, req)
if w.Code != 404 {
t.Errorf("Response was incorrect, got: %d, want: 404.", w.Code)
}
}
func TestCollectionRecordRoute(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/notes/1", nil)
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response was incorrect, got: %d, want: 200.", w.Code)
}
}
func TestCollectionRecordRouteForNonExistantID(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/notes/12345", nil)
router.ServeHTTP(w, req)
if w.Code != 404 {
t.Errorf("Response was incorrect, got: %d, want: 404.", w.Code)
}
}
func TestCreateRecord(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
var jsonStr = []byte(`{"title":"new record"}`)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/notes", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != 201 {
t.Errorf("Response was incorrect, got: %d, want: 201.", w.Code)
}
}
func TestUpdateRecordViaPut(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
var jsonStr = []byte(`{"title":"new record"}`)
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/notes/1", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response was incorrect, got: %d, want: 200.", w.Code)
}
}
func TestUpdateRecordViaPatch(t *testing.T) {
setupDataFileForTest()
router := setupAPI()
var jsonStr = []byte(`{"title":"new record"}`)
w := httptest.NewRecorder()
req, _ := http.NewRequest("PATCH", "/notes/1", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response was incorrect, got: %d, want: 200.", w.Code)
}
}
// unit tests
func TestFindByIdWithExistingRecord(t *testing.T) {
setupDataFileForTest()
_, err := findById("notes", "1")
if err != nil {
t.Errorf("Record exists but was not found")
}
}
func TestFindByIdWithNonInt(t *testing.T) {
setupDataFileForTest()
_, err := findById("notes", "ABCD")
if err == nil {
t.Errorf("Not getting an error when 'ABCD' used as key.")
}
}
func TestFindByIdWithNonExistingRecord(t *testing.T) {
setupDataFileForTest()
_, err := findById("notes", "1234")
if err == nil {
t.Errorf("Should be error when doesn't exist")
}
}
func TestRemoveRecordByIdWorks(t *testing.T) {
setupDataFileForTest()
removeById("notes", "1")
// see if it persisted
results, _ := getData()
children, _ := results.S("notes").Children()
for _, child := range children {
if child.S("id").Data().(float64) == 1 {
t.Errorf("New record still found in the json - failed")
break
}
}
}
func TestRemoveRecordByIdWithInvalidID(t *testing.T) {
setupDataFileForTest()
_, err := removeById("notes", "1234")
if err == nil {
t.Errorf("Should be error when doesn't exist")
}
}
func TestCreateNewRecord(t *testing.T) {
setupDataFileForTest()
var result interface{}
createNewRecord("notes", []byte(`{"title" : "test"}`))
// see if it persisted
results, _ := getData()
children, _ := results.S("notes").Children()
// find the index of the record we have to delete
for _, child := range children {
// if we find it....
if child.S("title").Data().(string) == "test" {
// save the record we found as the result along with the index
result = child
break
}
}
if result == nil {
t.Errorf("New record wasn't found in the json - failed")
}
}
func TestUpdateRecordAtID(t *testing.T) {
setupDataFileForTest()
var result = false
updateById("notes", "1", []byte(`{"title" : "blah blah blah"}`))
// see if it persisted
records, _ := getData()
children, _ := records.S("notes").Children()
// find the index of the record we have to delete
for _, child := range children {
// if we find it....
if child.S("id").Data().(float64) == 1 && child.S("title").Data().(string) == "blah blah blah" {
// save the record we found as the result along with the index
result = true
break
}
}
if !result {
t.Errorf("New record wasn't found in the json - failed")
}
}
func TestUpdateRecordAtBadID(t *testing.T) {
setupDataFileForTest()
_, err := updateById("notes", "1234", []byte(`{"title" : "blah blah blah"}`))
if err == nil {
t.Errorf("Update for ID 1234 should have failed but didn't")
}
}
func TestUpdateRecordAtinvalidID(t *testing.T) {
setupDataFileForTest()
_, err := updateById("notes", "ABCD", []byte(`{"title" : "blah blah blah"}`))
if err == nil {
t.Errorf("Update for ID ABCD should have failed but didn't")
}
}