-
Notifications
You must be signed in to change notification settings - Fork 4
/
crud_test.go
290 lines (231 loc) · 6.03 KB
/
crud_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
289
290
package goal_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"testing"
"github.com/thomasdao/goal"
)
// Define HTTP methods to support
func (user *testuser) Get(w http.ResponseWriter, request *http.Request) (int, interface{}, error) {
return goal.Read(reflect.TypeOf(user), request)
}
func (user *testuser) Post(w http.ResponseWriter, request *http.Request) (int, interface{}, error) {
return goal.Create(reflect.TypeOf(user), request)
}
func (user *testuser) Put(w http.ResponseWriter, request *http.Request) (int, interface{}, error) {
return goal.Update(reflect.TypeOf(user), request)
}
func (user *testuser) Delete(w http.ResponseWriter, request *http.Request) (int, interface{}, error) {
return goal.Delete(reflect.TypeOf(user), request)
}
func (user *testuser) CurrentRevision() int64 {
return user.Rev
}
func (user *testuser) SetNextRevision() {
user.Rev = user.Rev + 1
}
func TestCreate(t *testing.T) {
setup()
defer tearDown()
var json = []byte(`{"Name":"Thomas", "Age": 28, "Rev": 1}`)
req, _ := http.NewRequest("POST", userURL(), bytes.NewBuffer(json))
// Get response
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Error("Request Failed ", res.StatusCode)
return
}
// Make sure db has one object
var user testuser
db.Where("name = ?", "Thomas").First(&user)
if &user == nil {
t.Error("Fail to save object to database")
return
}
if user.Name != "Thomas" || user.Age != 28 {
t.Error("Save wrong data or missing data")
}
// Make sure data exists in Redis
if goal.SharedCache != nil {
key := goal.CacheKey(user)
var redisUser testuser
goal.SharedCache.Get(key, &redisUser)
if !reflect.DeepEqual(user, redisUser) {
t.Error("Incorrect data in redis, ", user, redisUser)
}
}
}
func TestGet(t *testing.T) {
setup()
defer tearDown()
user := &testuser{}
user.Name = "Thomas"
user.Age = 28
user.Rev = 1
db.Create(user)
req, _ := http.NewRequest("GET", idURL(user.ID), nil)
// Get response
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Error("Request Failed")
return
}
defer res.Body.Close()
content, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
return
}
var result testuser
err = json.Unmarshal(content, &result)
if err != nil {
t.Error(err)
return
}
if result.ID != user.ID || result.Name != user.Name || result.Age != user.Age {
t.Error("Response is invalid")
}
// Make sure data exists in Redis
if goal.SharedCache != nil {
key := goal.CacheKey(user)
// Test data exists in Redis
if exist, _ := goal.SharedCache.Exists(key); !exist {
t.Error("Data should be saved into Redis")
}
var redisUser testuser
goal.SharedCache.Get(key, &redisUser)
if !reflect.DeepEqual(user, &redisUser) {
t.Error("Incorrect data in redis, ", user, &redisUser)
}
}
}
func TestPut(t *testing.T) {
setup()
defer tearDown()
user := &testuser{}
user.Name = "Thomas"
user.Age = 28
user.Rev = 1
db.Create(user)
var json = []byte(`{"Name":"Thomas Dao", "ID":1, "Rev":1}`)
req, _ := http.NewRequest("PUT", idURL(user.ID), bytes.NewBuffer(json))
req.Close = true
// Get response
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
t.Error(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
t.Errorf("Request Failed %d %s", res.StatusCode, string(body))
return
}
var result testuser
if db.Where("name = ?", "Thomas Dao").First(&result).RecordNotFound() {
t.Error("Update unsuccessful")
}
if result.ID != user.ID || result.Age != user.Age || result.Rev != user.Rev+1 {
t.Errorf("Incorrect update %+v", result)
}
// Make sure data exists in Redis
if goal.SharedCache != nil {
key := goal.CacheKey(user)
var redisUser testuser
goal.SharedCache.Get(key, &redisUser)
if !reflect.DeepEqual(result, redisUser) {
t.Error("Incorrect data in redis, ", result, redisUser)
}
}
}
func TestPutConflict(t *testing.T) {
setup()
defer tearDown()
user := &testuser{}
user.Name = "Thomas"
user.Age = 28
user.Rev = 2
db.Create(user)
var json = []byte(`{"Name":"Thomas Dao", "ID":1, "Rev":1}`)
req, _ := http.NewRequest("PUT", idURL(user.ID), bytes.NewBuffer(json))
req.Close = true
// Get response
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
t.Error(err)
}
defer res.Body.Close()
if res.StatusCode != 409 {
t.Errorf("This should be conflict %d", res.StatusCode)
return
}
var result testuser
if db.Where("name = ?", "Thomas").First(&result).RecordNotFound() {
t.Error("Update unsuccessful")
}
if result.ID != user.ID || result.Age != user.Age {
t.Error("Incorrect update")
}
// Make sure data exists in Redis
if goal.SharedCache != nil {
key := goal.CacheKey(user)
var redisUser testuser
goal.SharedCache.Get(key, &redisUser)
if !reflect.DeepEqual(result, redisUser) {
t.Error("Incorrect data in redis, ", result, redisUser)
}
}
json1 := []byte(`{"Name":"Thomas Dao", "ID":1}`)
req1, _ := http.NewRequest("PUT", idURL(user.ID), bytes.NewBuffer(json1))
res1, err1 := client.Do(req1)
if err1 != nil {
t.Error(err1)
}
if res1.StatusCode != 400 {
t.Errorf("Revision should be required %d", res1.StatusCode)
return
}
}
func TestDelete(t *testing.T) {
setup()
defer tearDown()
user := &testuser{}
user.Name = "Thomas"
user.Age = 28
db.Create(user)
req, _ := http.NewRequest("DELETE", idURL(user.ID), nil)
// Get response
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Error("Request Failed")
return
}
var result testuser
if !db.Where("name = ?", "Thomas").First(&result).RecordNotFound() {
t.Error("Delete is not successful. Expected result delete from db")
}
// Make sure no more data in redis
if goal.SharedCache != nil {
key := goal.CacheKey(user)
if exist, _ := goal.SharedCache.Exists(key); exist {
t.Error("Data should be deleted from Redis when object is deleted")
}
}
}