@@ -13,8 +13,8 @@ import (
13
13
"time"
14
14
)
15
15
16
- // dbPost post on behalf of adminToken by default (use params[0] true for root)
17
- func dbPost (t * testing.T , hf func (http.ResponseWriter , * http.Request ), repo string , v interface {}, params ... bool ) * http.Response {
16
+ // dbReq post on behalf of adminToken by default (use params[0] true for root)
17
+ func dbReq (t * testing.T , hf func (http.ResponseWriter , * http.Request ), method , path string , v interface {}, params ... bool ) * http.Response {
18
18
if params == nil {
19
19
params = make ([]bool , 0 )
20
20
}
@@ -28,7 +28,7 @@ func dbPost(t *testing.T, hf func(http.ResponseWriter, *http.Request), repo stri
28
28
t .Fatal ("error marshaling post data:" , err )
29
29
}
30
30
31
- req := httptest .NewRequest ("POST" , "/db/" + repo , bytes .NewReader (b ))
31
+ req := httptest .NewRequest (method , path , bytes .NewReader (b ))
32
32
w := httptest .NewRecorder ()
33
33
34
34
req .Header .Set ("SB-PUBLIC-KEY" , pubKey )
@@ -97,6 +97,7 @@ type Task struct {
97
97
Title string `json:"title"`
98
98
Done bool `json:"done"`
99
99
Created time.Time `json:"created"`
100
+ Count int `json:"count"`
100
101
}
101
102
102
103
func TestDBCreate (t * testing.T ) {
@@ -106,7 +107,8 @@ func TestDBCreate(t *testing.T) {
106
107
Created : time .Now (),
107
108
}
108
109
109
- resp := dbPost (t , database .add , "tasks" , task )
110
+ resp := dbReq (t , database .add , "POST" , "/db/tasks" , task )
111
+ defer resp .Body .Close ()
110
112
111
113
if resp .StatusCode > 299 {
112
114
t .Fatal (GetResponseBody (t , resp ))
@@ -154,3 +156,49 @@ func TestDBListCollections(t *testing.T) {
154
156
t .Errorf ("expected len to be > than 2 got %d" , len (names ))
155
157
}
156
158
}
159
+
160
+ func TestDBIncrease (t * testing.T ) {
161
+ task :=
162
+ Task {
163
+ Title : "item created" ,
164
+ Created : time .Now (),
165
+ Count : 1 ,
166
+ }
167
+
168
+ resp := dbReq (t , database .add , "POST" , "/db/tasks" , task )
169
+ defer resp .Body .Close ()
170
+
171
+ if resp .StatusCode > 299 {
172
+ t .Fatal (GetResponseBody (t , resp ))
173
+ }
174
+
175
+ var createdTask Task
176
+ if err := parseBody (resp .Body , & createdTask ); err != nil {
177
+ t .Fatal (err )
178
+ }
179
+
180
+ var data = new (struct {
181
+ Field string `json:"field"`
182
+ Range int `json:"range"`
183
+ })
184
+ data .Field = "count"
185
+ data .Range = 4
186
+
187
+ resp = dbReq (t , database .increase , "PUT" , "/inc/tasks/" + createdTask .ID , data )
188
+
189
+ if resp .StatusCode > 299 {
190
+ t .Fatal (GetResponseBody (t , resp ))
191
+ }
192
+
193
+ resp = dbReq (t , database .get , "GET" , "/db/tasks/" + createdTask .ID , nil )
194
+ if resp .StatusCode > 299 {
195
+ t .Fatal (GetResponseBody (t , resp ))
196
+ }
197
+
198
+ var increased Task
199
+ if err := parseBody (resp .Body , & increased ); err != nil {
200
+ t .Fatal (err )
201
+ } else if increased .Count != 5 {
202
+ t .Errorf ("expected count to be 5 got %d" , increased .Count )
203
+ }
204
+ }
0 commit comments