This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
forked from seven5/seven5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbs_rest_test.go
329 lines (285 loc) · 8.77 KB
/
qbs_rest_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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package seven5
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/coocood/qbs"
_ "github.com/lib/pq"
)
const (
none = 0
force_error = 1
force_panic = 2
)
/*---- type of actual DB action ----*/
type House struct {
Id int64
Address string
Zip int64 /*0->99999, inclusive*/
}
/*---- wire type for the tests ----*/
type HouseWire struct {
Id int64
Addr string
ZipCode int64
}
type testObj struct {
testCallCount int
failPost int
}
/*these funcs are use to test that if you meet the QBSRest interfaces you
can be wrapped by the qbs code in Seven5 */
func (self *testObj) IndexQbs(pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWire{}, nil
}
func (self *testObj) FindQbs(id int64, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWire{}, nil
}
func (self *testObj) DeleteQbs(id int64, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWire{}, nil
}
func (self *testObj) PutQbs(id int64, value interface{}, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWire{}, nil
}
func (self *testObj) PostQbs(value interface{}, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
in := value.(*HouseWire)
house := &House{Address: in.Addr, Zip: in.ZipCode}
if _, err := q.Save(house); err != nil {
return nil, err
}
//simulate an error AFTER the save!
switch self.failPost {
case force_panic:
panic("testing panic handling")
case force_error:
return nil, errors.New("testing error handling")
}
return &HouseWire{Id: house.Id, Addr: house.Address, ZipCode: house.Zip}, nil
}
/* */
/*---- wire type for the udid tests ----*/
/* */
type HouseWireUdid struct {
Id string
Addr string
ZipCode int64
}
type HouseUdid struct {
Id string `qbs:"pk"`
Address string
Zip int64 /*0->99999, inclusive*/
}
type testObjUdid struct {
testCallCount int
failPost int
}
/*these funcs are use to test that if you meet the QBSRest interfaces you
can be wrapped by the qbs code in Seven5 */
func (self *testObjUdid) IndexQbs(pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWireUdid{}, nil
}
func (self *testObjUdid) FindQbs(id string, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWireUdid{}, nil
}
func (self *testObjUdid) DeleteQbs(id string, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWireUdid{}, nil
}
func (self *testObjUdid) PutQbs(id string, value interface{}, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
return &HouseWireUdid{}, nil
}
func (self *testObjUdid) PostQbs(value interface{}, pb PBundle, q *qbs.Qbs) (interface{}, error) {
self.testCallCount++
in := value.(*HouseWireUdid)
house := &HouseUdid{Id: in.Id, Address: in.Addr, Zip: in.ZipCode}
if house.Id == "" {
house.Id = UDID()
}
if _, err := q.Save(house); err != nil {
return nil, err
}
return &HouseWireUdid{Id: house.Id, Addr: house.Address, ZipCode: house.Zip}, nil
}
/*-------------------------------------------------------------------------*/
/* TEST CODE */
/*-------------------------------------------------------------------------*/
const (
APP_NAME = "testapp"
)
func checkNumberHouses(T *testing.T, store *QbsStore, expected int) {
houses := []*House{}
q, err := qbs.GetQbs()
if err != nil {
T.Fatalf("couldn't get QBS: %v", err)
}
defer q.Close()
if err := q.FindAll(&houses); err != nil {
T.Fatalf("Error during find: %s", err)
}
if len(houses) != expected {
T.Errorf("Wrong number of houses! expected %d but got %d", expected, len(houses))
panic("XXX")
}
}
func TestTxn(T *testing.T) {
//raw, mux := setupDispatcher()
store := setupTestStore()
obj := &testObj{}
wrapped := QbsWrapAll(obj, store)
//insure that there are no houses at start
q, err := qbs.GetQbs()
if err != nil {
T.Fatalf("couldn't get QBS: %v", err)
}
defer q.Close()
var houses []*House
if err := q.FindAll(&houses); err != nil {
T.Fatalf("Error during find: %s", err)
}
for _, h := range houses {
if _, err := q.Delete(h); err != nil {
T.Fatalf("Can't start test out right: %v", err)
}
}
for _, choice := range []int{force_panic, force_error} {
obj.failPost = choice
checkNumberHouses(T, store, 0)
_, err := wrapped.Post(&HouseWire{Id: 0, Addr: "123 evergreen terrace", ZipCode: 98607}, nil)
if err == nil {
T.Fatalf("expected an error but didn't get one!")
}
e, ok := err.(*Error)
if !ok {
T.Fatalf("unexpected error type %T", err)
}
if e.StatusCode != http.StatusInternalServerError {
T.Error("Wrong error code, expected %d but got %d", 500, e.StatusCode)
}
checkNumberHouses(T, store, 0)
}
}
func setupDispatcher() (*RawDispatcher, *ServeMux) {
io := NewRawIOHook(&JsonDecoder{}, &JsonEncoder{}, nil)
raw := NewRawDispatcher(io, nil, nil, "/rest")
serveMux := NewServeMux()
serveMux.Dispatch("/rest/", raw)
return raw, serveMux
}
func setupTestStore() *QbsStore {
dsn := GetDSNOrDie()
return NewQbsStoreFromDSN(dsn)
}
func checkNetworkCalls(T *testing.T, portSpec string, serveMux *ServeMux, obj *testObj, udid *testObjUdid) {
if udid != nil {
if udid.testCallCount != 0 {
T.Fatalf("sanity check at start failed: %d", obj.testCallCount)
}
} else {
if obj.testCallCount != 0 {
T.Fatalf("sanity check at start failed: %d", obj.testCallCount)
}
}
T.Logf("launching serever...%s", portSpec)
go func() {
http.ListenAndServe(portSpec, serveMux)
}()
client := new(http.Client)
var messageData [][]string
messageDataUID := [][]string{
[]string{"GET", fmt.Sprintf("http://localhost%s/rest/house", portSpec), ""},
[]string{"GET", fmt.Sprintf("http://localhost%s/rest/house/1", portSpec), ""},
[]string{"DELETE", fmt.Sprintf("http://localhost%s/rest/house/1", portSpec), ""},
[]string{"POST", fmt.Sprintf("http://localhost%s/rest/house", portSpec), "{}"},
[]string{"PUT", fmt.Sprintf("http://localhost%s/rest/house/1", portSpec), "{}"},
}
messageDataUdid := [][]string{
[]string{"GET", fmt.Sprintf("http://localhost%s/rest/house", portSpec), ""},
[]string{"GET", fmt.Sprintf("http://localhost%s/rest/house/e68e9891-1a19-d48b-4f0a-c4aaf5fffef2", portSpec), ""},
[]string{"DELETE", fmt.Sprintf("http://localhost%s/rest/house/e68e9891-1a19-d48b-4f0a-c4aaf5fffef2", portSpec), ""},
[]string{"POST", fmt.Sprintf("http://localhost%s/rest/house", portSpec), "{}"},
[]string{"PUT", fmt.Sprintf("http://localhost%s/rest/house/e68e9891-1a19-d48b-4f0a-c4aaf5fffef2", portSpec), "{}"},
}
if udid != nil {
messageData = messageDataUdid
} else {
messageData = messageDataUID
}
for i, callCount := range []int{1, 2, 3, 4, 5} {
req := makeReq(T, messageData[i][0], messageData[i][1], messageData[i][2])
resp, err := client.Do(req)
checkResponse(T, err, resp)
if udid != nil {
if udid.testCallCount != callCount {
T.Fatalf("did not call QBS level resource (expected %d calls but found %d)", callCount, udid.testCallCount)
}
} else {
if obj.testCallCount != callCount {
T.Fatalf("did not call QBS level resource (expected %d calls but found %d)", callCount, obj.testCallCount)
}
}
}
}
func TestWrappingAll(T *testing.T) {
raw, mux := setupDispatcher()
store := setupTestStore()
obj := &testObj{}
raw.Resource("house", &HouseWire{}, QbsWrapAll(obj, store))
checkNetworkCalls(T, ":8991", mux, obj, nil)
//clean up
q, err := qbs.GetQbs()
if err != nil {
T.Fatalf("couldn't get a qbs: %v", err)
}
q.WhereEqual("Zip", 0).Delete(&House{})
q.Close()
}
func TestWrappingUdid(T *testing.T) {
raw, mux := setupDispatcher()
store := setupTestStore()
obj := &testObjUdid{}
raw.ResourceUdid("house", &HouseWireUdid{}, QbsWrapAllUdid(obj, store))
checkNetworkCalls(T, ":8993", mux, nil, obj)
//clean up
q, err := qbs.GetQbs()
if err != nil {
T.Fatalf("couldn't get a qbs: %v", err)
}
q.WhereEqual("Zip", 0).Delete(&HouseUdid{})
q.Close()
}
func TestWrappingSeparate(T *testing.T) {
raw, mux := setupDispatcher()
store := setupTestStore()
obj := &testObj{}
raw.ResourceSeparate("house", &HouseWire{},
QbsWrapIndex(obj, store),
QbsWrapFind(obj, store),
QbsWrapPost(obj, store),
QbsWrapPut(obj, store),
QbsWrapDelete(obj, store))
checkNetworkCalls(T, ":8992", mux, obj, nil)
//clean up
q, err := qbs.GetQbs()
if err != nil {
T.Fatalf("couldn't get a qbs: %v", err)
}
q.WhereEqual("Zip", 0).Delete(&House{})
q.Close()
}
func checkResponse(T *testing.T, err error, resp *http.Response) {
if err != nil {
T.Fatalf("failed on %s with error %v", "GET", err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
T.Fatalf("failed on %s with status %d", "GET", resp.StatusCode)
}
}