OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // adapted from github.com/golang/appengine/datastore | 5 // adapted from github.com/golang/appengine/datastore |
6 | 6 |
7 package datastore | 7 package datastore |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 break | 54 break |
55 } | 55 } |
56 } | 56 } |
57 return nil | 57 return nil |
58 } | 58 } |
59 | 59 |
60 func (f *fakeDatastore) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { | 60 func (f *fakeDatastore) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { |
61 if keys[0].Kind() == "FailAll" { | 61 if keys[0].Kind() == "FailAll" { |
62 return errors.New("PutMulti fail all") | 62 return errors.New("PutMulti fail all") |
63 } | 63 } |
64 » assertExtra := false | 64 » _, assertExtra := vals[0].GetMeta("assertExtra") |
65 » if _, err := vals[0].GetMeta("assertExtra"); err == nil { | |
66 » » assertExtra = true | |
67 » } | |
68 for i, k := range keys { | 65 for i, k := range keys { |
69 err := error(nil) | 66 err := error(nil) |
70 if k.Kind() == "Fail" { | 67 if k.Kind() == "Fail" { |
71 err = errors.New("PutMulti fail") | 68 err = errors.New("PutMulti fail") |
72 } else { | 69 } else { |
73 So(vals[i]["Value"], ShouldResemble, []Property{MkProper
ty(i)}) | 70 So(vals[i]["Value"], ShouldResemble, []Property{MkProper
ty(i)}) |
74 if assertExtra { | 71 if assertExtra { |
75 So(vals[i]["Extra"], ShouldResemble, []Property{
MkProperty("whoa")}) | 72 So(vals[i]["Extra"], ShouldResemble, []Property{
MkProperty("whoa")}) |
76 } | 73 } |
77 if k.Incomplete() { | 74 if k.Incomplete() { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 func (f *FakePLS) Save(withMeta bool) (PropertyMap, error) { | 159 func (f *FakePLS) Save(withMeta bool) (PropertyMap, error) { |
163 if f.failSave { | 160 if f.failSave { |
164 return nil, errors.New("FakePLS.Save") | 161 return nil, errors.New("FakePLS.Save") |
165 } | 162 } |
166 ret := PropertyMap{ | 163 ret := PropertyMap{ |
167 "Value": {MkProperty(f.Value)}, | 164 "Value": {MkProperty(f.Value)}, |
168 "Extra": {MkProperty("whoa")}, | 165 "Extra": {MkProperty("whoa")}, |
169 } | 166 } |
170 if withMeta { | 167 if withMeta { |
171 id, _ := f.GetMeta("id") | 168 id, _ := f.GetMeta("id") |
172 » » So(ret.SetMeta("id", id), ShouldBeNil) | 169 » » So(ret.SetMeta("id", id), ShouldBeTrue) |
173 if f.Kind == "" { | 170 if f.Kind == "" { |
174 » » » So(ret.SetMeta("kind", "FakePLS"), ShouldBeNil) | 171 » » » So(ret.SetMeta("kind", "FakePLS"), ShouldBeTrue) |
175 } else { | 172 } else { |
176 » » » So(ret.SetMeta("kind", f.Kind), ShouldBeNil) | 173 » » » So(ret.SetMeta("kind", f.Kind), ShouldBeTrue) |
177 } | 174 } |
178 » » So(ret.SetMeta("assertExtra", true), ShouldBeNil) | 175 » » So(ret.SetMeta("assertExtra", true), ShouldBeTrue) |
179 } | 176 } |
180 return ret, nil | 177 return ret, nil |
181 } | 178 } |
182 | 179 |
183 func (f *FakePLS) GetMetaDefault(key string, dflt interface{}) interface{} { | 180 func (f *FakePLS) GetMeta(key string) (interface{}, bool) { |
184 » return GetMetaDefaultImpl(f.GetMeta, key, dflt) | |
185 } | |
186 | |
187 func (f *FakePLS) GetMeta(key string) (interface{}, error) { | |
188 if f.failGetMeta { | 181 if f.failGetMeta { |
189 » » return nil, errors.New("FakePLS.GetMeta") | 182 » » return nil, false |
190 } | 183 } |
191 switch key { | 184 switch key { |
192 case "id": | 185 case "id": |
193 if f.StringID != "" { | 186 if f.StringID != "" { |
194 » » » return f.StringID, nil | 187 » » » return f.StringID, true |
195 } | 188 } |
196 » » return f.IntID, nil | 189 » » return f.IntID, true |
197 case "kind": | 190 case "kind": |
198 if f.Kind == "" { | 191 if f.Kind == "" { |
199 » » » return "FakePLS", nil | 192 » » » return "FakePLS", true |
200 } | 193 } |
201 » » return f.Kind, nil | 194 » » return f.Kind, true |
202 } | 195 } |
203 » return nil, ErrMetaFieldUnset | 196 » return nil, false |
204 } | 197 } |
205 | 198 |
206 func (f *FakePLS) GetAllMeta() PropertyMap { | 199 func (f *FakePLS) GetAllMeta() PropertyMap { |
207 ret := PropertyMap{} | 200 ret := PropertyMap{} |
208 » if id, err := f.GetMeta("id"); err != nil { | 201 » if id, ok := f.GetMeta("id"); !ok { |
209 » » So(ret.SetMeta("id", id), ShouldBeNil) | 202 » » ret.SetMeta("id", id) |
210 } | 203 } |
211 » if kind, err := f.GetMeta("kind"); err != nil { | 204 » if kind, ok := f.GetMeta("kind"); !ok { |
212 » » So(ret.SetMeta("kind", kind), ShouldBeNil) | 205 » » ret.SetMeta("kind", kind) |
213 } | 206 } |
214 return ret | 207 return ret |
215 } | 208 } |
216 | 209 |
217 func (f *FakePLS) SetMeta(key string, val interface{}) error { | 210 func (f *FakePLS) SetMeta(key string, val interface{}) bool { |
218 if f.failSetMeta { | 211 if f.failSetMeta { |
219 » » return errors.New("FakePL.SetMeta") | 212 » » return false |
220 } | 213 } |
221 if key == "id" { | 214 if key == "id" { |
222 switch x := val.(type) { | 215 switch x := val.(type) { |
223 case int64: | 216 case int64: |
224 f.IntID = x | 217 f.IntID = x |
225 case string: | 218 case string: |
226 f.StringID = x | 219 f.StringID = x |
227 } | 220 } |
228 » » return nil | 221 » » return true |
229 } | 222 } |
230 if key == "kind" { | 223 if key == "kind" { |
231 f.Kind = val.(string) | 224 f.Kind = val.(string) |
232 » » return nil | 225 » » return true |
233 } | 226 } |
234 » return ErrMetaFieldUnset | 227 » return false |
235 } | 228 } |
236 | 229 |
237 func (f *FakePLS) Problem() error { | 230 func (f *FakePLS) Problem() error { |
238 if f.failProblem { | 231 if f.failProblem { |
239 return errors.New("FakePLS.Problem") | 232 return errors.New("FakePLS.Problem") |
240 } | 233 } |
241 return nil | 234 return nil |
242 } | 235 } |
243 | 236 |
244 type MGSWithNoKind struct { | 237 type MGSWithNoKind struct { |
245 S string | 238 S string |
246 } | 239 } |
247 | 240 |
248 func (s *MGSWithNoKind) GetMetaDefault(key string, dflt interface{}) interface{}
{ | 241 func (s *MGSWithNoKind) GetMeta(key string) (interface{}, bool) { |
249 » return GetMetaDefaultImpl(s.GetMeta, key, dflt) | 242 » return nil, false |
250 } | |
251 | |
252 func (s *MGSWithNoKind) GetMeta(key string) (interface{}, error) { | |
253 » return nil, ErrMetaFieldUnset | |
254 } | 243 } |
255 | 244 |
256 func (s *MGSWithNoKind) GetAllMeta() PropertyMap { | 245 func (s *MGSWithNoKind) GetAllMeta() PropertyMap { |
257 return PropertyMap{} | 246 return PropertyMap{} |
258 } | 247 } |
259 | 248 |
260 func (s *MGSWithNoKind) SetMeta(key string, val interface{}) error { | 249 func (s *MGSWithNoKind) SetMeta(key string, val interface{}) bool { |
261 » return ErrMetaFieldUnset | 250 » return false |
262 } | 251 } |
263 | 252 |
264 var _ MetaGetterSetter = (*MGSWithNoKind)(nil) | 253 var _ MetaGetterSetter = (*MGSWithNoKind)(nil) |
265 | 254 |
266 func TestKeyForObj(t *testing.T) { | 255 func TestKeyForObj(t *testing.T) { |
267 t.Parallel() | 256 t.Parallel() |
268 | 257 |
269 Convey("Test interface.KeyForObj", t, func() { | 258 Convey("Test interface.KeyForObj", t, func() { |
270 c := info.Set(context.Background(), fakeInfo{}) | 259 c := info.Set(context.Background(), fakeInfo{}) |
271 c = SetRawFactory(c, fakeDatastoreFactory) | 260 c = SetRawFactory(c, fakeDatastoreFactory) |
(...skipping 21 matching lines...) Expand all Loading... |
293 }) | 282 }) |
294 | 283 |
295 Convey("struct containing $id and $parent", func() { | 284 Convey("struct containing $id and $parent", func() { |
296 So(ds.KeyForObj(&CommonStruct{ID: 4}).String(),
ShouldEqual, `s~aid:ns:/CommonStruct,4`) | 285 So(ds.KeyForObj(&CommonStruct{ID: 4}).String(),
ShouldEqual, `s~aid:ns:/CommonStruct,4`) |
297 | 286 |
298 So(ds.KeyForObj(&CommonStruct{ID: 4, Parent: k})
.String(), ShouldEqual, `s~aid:ns:/Hello,"world"/CommonStruct,4`) | 287 So(ds.KeyForObj(&CommonStruct{ID: 4, Parent: k})
.String(), ShouldEqual, `s~aid:ns:/Hello,"world"/CommonStruct,4`) |
299 }) | 288 }) |
300 | 289 |
301 Convey("a propmap with $key", func() { | 290 Convey("a propmap with $key", func() { |
302 pm := PropertyMap{} | 291 pm := PropertyMap{} |
303 » » » » So(pm.SetMeta("key", k), ShouldBeNil) | 292 » » » » So(pm.SetMeta("key", k), ShouldBeTrue) |
304 So(ds.KeyForObj(pm).String(), ShouldEqual, `s~ai
d:ns:/Hello,"world"`) | 293 So(ds.KeyForObj(pm).String(), ShouldEqual, `s~ai
d:ns:/Hello,"world"`) |
305 }) | 294 }) |
306 | 295 |
307 Convey("a propmap with $id, $kind, $parent", func() { | 296 Convey("a propmap with $id, $kind, $parent", func() { |
308 pm := PropertyMap{} | 297 pm := PropertyMap{} |
309 » » » » So(pm.SetMeta("id", 100), ShouldBeNil) | 298 » » » » So(pm.SetMeta("id", 100), ShouldBeTrue) |
310 » » » » So(pm.SetMeta("kind", "Sup"), ShouldBeNil) | 299 » » » » So(pm.SetMeta("kind", "Sup"), ShouldBeTrue) |
311 So(ds.KeyForObj(pm).String(), ShouldEqual, `s~ai
d:ns:/Sup,100`) | 300 So(ds.KeyForObj(pm).String(), ShouldEqual, `s~ai
d:ns:/Sup,100`) |
312 | 301 |
313 » » » » So(pm.SetMeta("parent", k), ShouldBeNil) | 302 » » » » So(pm.SetMeta("parent", k), ShouldBeTrue) |
314 So(ds.KeyForObj(pm).String(), ShouldEqual, `s~ai
d:ns:/Hello,"world"/Sup,100`) | 303 So(ds.KeyForObj(pm).String(), ShouldEqual, `s~ai
d:ns:/Hello,"world"/Sup,100`) |
315 }) | 304 }) |
316 | 305 |
317 Convey("a pls with $id, $parent", func() { | 306 Convey("a pls with $id, $parent", func() { |
318 pls := GetPLS(&CommonStruct{ID: 1}) | 307 pls := GetPLS(&CommonStruct{ID: 1}) |
319 So(ds.KeyForObj(pls).String(), ShouldEqual, `s~a
id:ns:/CommonStruct,1`) | 308 So(ds.KeyForObj(pls).String(), ShouldEqual, `s~a
id:ns:/CommonStruct,1`) |
320 | 309 |
321 » » » » So(pls.SetMeta("parent", k), ShouldBeNil) | 310 » » » » So(pls.SetMeta("parent", k), ShouldBeTrue) |
322 So(ds.KeyForObj(pls).String(), ShouldEqual, `s~a
id:ns:/Hello,"world"/CommonStruct,1`) | 311 So(ds.KeyForObj(pls).String(), ShouldEqual, `s~a
id:ns:/Hello,"world"/CommonStruct,1`) |
323 }) | 312 }) |
324 | 313 |
325 Convey("can see if things exist", func() { | 314 Convey("can see if things exist", func() { |
326 e, err := ds.Exists(k) | 315 e, err := ds.Exists(k) |
327 So(err, ShouldBeNil) | 316 So(err, ShouldBeNil) |
328 So(e, ShouldBeTrue) | 317 So(e, ShouldBeTrue) |
329 | 318 |
330 e, err = ds.Exists(ds.MakeKey("DNE", "nope")) | 319 e, err = ds.Exists(ds.MakeKey("DNE", "nope")) |
331 So(err, ShouldBeNil) | 320 So(err, ShouldBeNil) |
332 So(e, ShouldBeFalse) | 321 So(e, ShouldBeFalse) |
333 | 322 |
334 _, err = ds.Exists(ds.MakeKey("Fail", "boom")) | 323 _, err = ds.Exists(ds.MakeKey("Fail", "boom")) |
335 So(err, ShouldErrLike, "GetMulti fail") | 324 So(err, ShouldErrLike, "GetMulti fail") |
336 }) | 325 }) |
337 | 326 |
338 }) | 327 }) |
339 | 328 |
340 Convey("bad", func() { | 329 Convey("bad", func() { |
341 Convey("a propmap without $kind", func() { | 330 Convey("a propmap without $kind", func() { |
342 pm := PropertyMap{} | 331 pm := PropertyMap{} |
343 » » » » So(pm.SetMeta("id", 100), ShouldBeNil) | 332 » » » » So(pm.SetMeta("id", 100), ShouldBeTrue) |
344 So(func() { ds.KeyForObj(pm) }, ShouldPanic) | 333 So(func() { ds.KeyForObj(pm) }, ShouldPanic) |
345 }) | 334 }) |
| 335 |
| 336 Convey("a bad object", func() { |
| 337 type BadObj struct { |
| 338 ID int64 `gae:"$id"` |
| 339 |
| 340 NonSerializableField complex64 |
| 341 } |
| 342 |
| 343 So(func() { ds.KeyForObjErr(&BadObj{ID: 1}) }, S
houldPanicLike, |
| 344 `field "NonSerializableField" has invali
d type: complex64`) |
| 345 }) |
346 }) | 346 }) |
347 }) | 347 }) |
348 } | 348 } |
349 | 349 |
350 func TestPut(t *testing.T) { | 350 func TestPut(t *testing.T) { |
351 t.Parallel() | 351 t.Parallel() |
352 | 352 |
353 Convey("Test Put/PutMulti", t, func() { | 353 Convey("Test Put/PutMulti", t, func() { |
354 c := info.Set(context.Background(), fakeInfo{}) | 354 c := info.Set(context.Background(), fakeInfo{}) |
355 c = SetRawFactory(c, fakeDatastoreFactory) | 355 c = SetRawFactory(c, fakeDatastoreFactory) |
356 ds := Get(c) | 356 ds := Get(c) |
357 | 357 |
358 Convey("bad", func() { | 358 Convey("bad", func() { |
359 Convey("static can't serialize", func() { | 359 Convey("static can't serialize", func() { |
360 bss := []badStruct{{}, {}} | 360 bss := []badStruct{{}, {}} |
361 » » » » So(ds.PutMulti(bss).Error(), ShouldContainSubstr
ing, "invalid PutMulti input") | 361 » » » » So(func() { ds.PutMulti(bss) }, ShouldPanicLike, |
| 362 » » » » » `field "Compy" has invalid type`) |
362 }) | 363 }) |
363 | 364 |
364 Convey("static ptr can't serialize", func() { | 365 Convey("static ptr can't serialize", func() { |
365 bss := []*badStruct{{}, {}} | 366 bss := []*badStruct{{}, {}} |
366 » » » » So(ds.PutMulti(bss).Error(), ShouldContainSubstr
ing, "invalid PutMulti input") | 367 » » » » So(func() { ds.PutMulti(bss) }, ShouldPanicLike, |
| 368 » » » » » `field "Compy" has invalid type: complex
64`) |
367 }) | 369 }) |
368 | 370 |
369 Convey("static bad type (non-slice)", func() { | 371 Convey("static bad type (non-slice)", func() { |
370 » » » » So(ds.PutMulti(100).Error(), ShouldContainSubstr
ing, "invalid PutMulti input") | 372 » » » » So(func() { ds.PutMulti(100) }, ShouldPanicLike, |
| 373 » » » » » "invalid argument type: expected slice,
got int") |
371 }) | 374 }) |
372 | 375 |
373 Convey("static bad type (slice of bad type)", func() { | 376 Convey("static bad type (slice of bad type)", func() { |
374 » » » » So(ds.PutMulti([]int{}).Error(), ShouldContainSu
bstring, "invalid PutMulti input") | 377 » » » » So(func() { ds.PutMulti([]int{}) }, ShouldPanicL
ike, |
| 378 » » » » » "invalid argument type: []int") |
375 }) | 379 }) |
376 | 380 |
377 Convey("dynamic can't serialize", func() { | 381 Convey("dynamic can't serialize", func() { |
378 fplss := []FakePLS{{failSave: true}, {}} | 382 fplss := []FakePLS{{failSave: true}, {}} |
379 » » » » So(ds.PutMulti(fplss).Error(), ShouldContainSubs
tring, "FakePLS.Save") | 383 » » » » So(ds.PutMulti(fplss), ShouldErrLike, "FakePLS.S
ave") |
380 }) | 384 }) |
381 | 385 |
382 Convey("can't get keys", func() { | 386 Convey("can't get keys", func() { |
383 fplss := []FakePLS{{failGetMeta: true}, {}} | 387 fplss := []FakePLS{{failGetMeta: true}, {}} |
384 » » » » So(ds.PutMulti(fplss).Error(), ShouldContainSubs
tring, "unable to extract $kind") | 388 » » » » So(ds.PutMulti(fplss), ShouldErrLike, "unable to
extract $kind") |
385 }) | 389 }) |
386 | 390 |
387 Convey("get single error for RPC failure", func() { | 391 Convey("get single error for RPC failure", func() { |
388 fplss := []FakePLS{{Kind: "FailAll"}, {}} | 392 fplss := []FakePLS{{Kind: "FailAll"}, {}} |
389 » » » » So(ds.PutMulti(fplss).Error(), ShouldEqual, "Put
Multi fail all") | 393 » » » » So(ds.PutMulti(fplss), ShouldErrLike, "PutMulti
fail all") |
390 }) | 394 }) |
391 | 395 |
392 Convey("get multi error for individual failures", func()
{ | 396 Convey("get multi error for individual failures", func()
{ |
393 fplss := []FakePLS{{}, {Kind: "Fail"}} | 397 fplss := []FakePLS{{}, {Kind: "Fail"}} |
394 So(ds.PutMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("PutMulti fail")}) | 398 So(ds.PutMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("PutMulti fail")}) |
395 }) | 399 }) |
396 | 400 |
397 Convey("put with non-modifyable type is an error", func(
) { | 401 Convey("put with non-modifyable type is an error", func(
) { |
398 cs := CommonStruct{} | 402 cs := CommonStruct{} |
399 » » » » So(ds.Put(cs).Error(), ShouldContainSubstring, "
invalid Put input type") | 403 » » » » So(ds.Put(cs), ShouldErrLike, "invalid Put input
type") |
400 }) | 404 }) |
401 | 405 |
402 Convey("struct with no $kind is an error", func() { | 406 Convey("struct with no $kind is an error", func() { |
403 s := MGSWithNoKind{} | 407 s := MGSWithNoKind{} |
404 » » » » So(ds.Put(&s).Error(), ShouldContainSubstring, "
unable to extract $kind") | 408 » » » » So(ds.Put(&s), ShouldErrLike, "unable to extract
$kind") |
405 }) | 409 }) |
406 }) | 410 }) |
407 | 411 |
408 Convey("ok", func() { | 412 Convey("ok", func() { |
409 Convey("[]S", func() { | 413 Convey("[]S", func() { |
410 css := make([]CommonStruct, 7) | 414 css := make([]CommonStruct, 7) |
411 for i := range css { | 415 for i := range css { |
412 if i == 4 { | 416 if i == 4 { |
413 css[i].ID = 200 | 417 css[i].ID = 200 |
414 } | 418 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 }) | 473 }) |
470 | 474 |
471 Convey("[]P (map)", func() { | 475 Convey("[]P (map)", func() { |
472 pms := make([]PropertyMap, 7) | 476 pms := make([]PropertyMap, 7) |
473 for i := range pms { | 477 for i := range pms { |
474 pms[i] = PropertyMap{ | 478 pms[i] = PropertyMap{ |
475 "$kind": {MkProperty("Pmap")}, | 479 "$kind": {MkProperty("Pmap")}, |
476 "Value": {MkProperty(i)}, | 480 "Value": {MkProperty(i)}, |
477 } | 481 } |
478 if i == 4 { | 482 if i == 4 { |
479 » » » » » » So(pms[i].SetMeta("id", int64(20
0)), ShouldBeNil) | 483 » » » » » » So(pms[i].SetMeta("id", int64(20
0)), ShouldBeTrue) |
480 } | 484 } |
481 } | 485 } |
482 So(ds.PutMulti(pms), ShouldBeNil) | 486 So(ds.PutMulti(pms), ShouldBeNil) |
483 for i, pm := range pms { | 487 for i, pm := range pms { |
484 expect := int64(i + 1) | 488 expect := int64(i + 1) |
485 if i == 4 { | 489 if i == 4 { |
486 expect = 200 | 490 expect = 200 |
487 } | 491 } |
488 So(ds.KeyForObj(pm).String(), ShouldEqua
l, fmt.Sprintf("s~aid:ns:/Pmap,%d", expect)) | 492 So(ds.KeyForObj(pm).String(), ShouldEqua
l, fmt.Sprintf("s~aid:ns:/Pmap,%d", expect)) |
489 } | 493 } |
(...skipping 18 matching lines...) Expand all Loading... |
508 }) | 512 }) |
509 | 513 |
510 Convey("[]*P (map)", func() { | 514 Convey("[]*P (map)", func() { |
511 pms := make([]*PropertyMap, 7) | 515 pms := make([]*PropertyMap, 7) |
512 for i := range pms { | 516 for i := range pms { |
513 pms[i] = &PropertyMap{ | 517 pms[i] = &PropertyMap{ |
514 "$kind": {MkProperty("Pmap")}, | 518 "$kind": {MkProperty("Pmap")}, |
515 "Value": {MkProperty(i)}, | 519 "Value": {MkProperty(i)}, |
516 } | 520 } |
517 if i == 4 { | 521 if i == 4 { |
518 » » » » » » So(pms[i].SetMeta("id", int64(20
0)), ShouldBeNil) | 522 » » » » » » So(pms[i].SetMeta("id", int64(20
0)), ShouldBeTrue) |
519 } | 523 } |
520 } | 524 } |
521 So(ds.PutMulti(pms), ShouldBeNil) | 525 So(ds.PutMulti(pms), ShouldBeNil) |
522 for i, pm := range pms { | 526 for i, pm := range pms { |
523 expect := int64(i + 1) | 527 expect := int64(i + 1) |
524 if i == 4 { | 528 if i == 4 { |
525 expect = 200 | 529 expect = 200 |
526 } | 530 } |
527 So(ds.KeyForObj(*pm).String(), ShouldEqu
al, fmt.Sprintf("s~aid:ns:/Pmap,%d", expect)) | 531 So(ds.KeyForObj(*pm).String(), ShouldEqu
al, fmt.Sprintf("s~aid:ns:/Pmap,%d", expect)) |
528 } | 532 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 | 600 |
597 Convey("Test Get/GetMulti", t, func() { | 601 Convey("Test Get/GetMulti", t, func() { |
598 c := info.Set(context.Background(), fakeInfo{}) | 602 c := info.Set(context.Background(), fakeInfo{}) |
599 c = SetRawFactory(c, fakeDatastoreFactory) | 603 c = SetRawFactory(c, fakeDatastoreFactory) |
600 ds := Get(c) | 604 ds := Get(c) |
601 So(ds, ShouldNotBeNil) | 605 So(ds, ShouldNotBeNil) |
602 | 606 |
603 Convey("bad", func() { | 607 Convey("bad", func() { |
604 Convey("static can't serialize", func() { | 608 Convey("static can't serialize", func() { |
605 toGet := []badStruct{{}, {}} | 609 toGet := []badStruct{{}, {}} |
606 » » » » So(ds.GetMulti(toGet).Error(), ShouldContainSubs
tring, "invalid GetMulti input") | 610 » » » » So(func() { ds.GetMulti(toGet) }, ShouldPanicLik
e, |
| 611 » » » » » `field "Compy" has invalid type: complex
64`) |
607 }) | 612 }) |
608 | 613 |
609 Convey("can't get keys", func() { | 614 Convey("can't get keys", func() { |
610 fplss := []FakePLS{{failGetMeta: true}, {}} | 615 fplss := []FakePLS{{failGetMeta: true}, {}} |
611 » » » » So(ds.GetMulti(fplss).Error(), ShouldContainSubs
tring, "unable to extract $kind") | 616 » » » » So(ds.GetMulti(fplss), ShouldErrLike, "unable to
extract $kind") |
612 }) | 617 }) |
613 | 618 |
614 Convey("get single error for RPC failure", func() { | 619 Convey("get single error for RPC failure", func() { |
615 fplss := []FakePLS{ | 620 fplss := []FakePLS{ |
616 {IntID: 1, Kind: "FailAll"}, | 621 {IntID: 1, Kind: "FailAll"}, |
617 {IntID: 2}, | 622 {IntID: 2}, |
618 } | 623 } |
619 So(ds.GetMulti(fplss).Error(), ShouldEqual, "Get
Multi fail all") | 624 So(ds.GetMulti(fplss).Error(), ShouldEqual, "Get
Multi fail all") |
620 }) | 625 }) |
621 | 626 |
622 Convey("get multi error for individual failures", func()
{ | 627 Convey("get multi error for individual failures", func()
{ |
623 fplss := []FakePLS{{IntID: 1}, {IntID: 2, Kind:
"Fail"}} | 628 fplss := []FakePLS{{IntID: 1}, {IntID: 2, Kind:
"Fail"}} |
624 So(ds.GetMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("GetMulti fail")}) | 629 So(ds.GetMulti(fplss), ShouldResemble, errors.Mu
ltiError{nil, errors.New("GetMulti fail")}) |
625 }) | 630 }) |
626 | 631 |
627 Convey("get with non-modifiable type is an error", func(
) { | 632 Convey("get with non-modifiable type is an error", func(
) { |
628 cs := CommonStruct{} | 633 cs := CommonStruct{} |
629 » » » » So(ds.Get(cs).Error(), ShouldContainSubstring, "
invalid Get input type") | 634 » » » » So(ds.Get(cs), ShouldErrLike, "invalid Get input
type") |
630 }) | 635 }) |
631 | 636 |
632 » » » Convey("failure to save metadata is an issue too", func(
) { | 637 » » » Convey("failure to save metadata is no problem though",
func() { |
633 » » » » cs := &FakePLS{failGetMeta: true} | 638 » » » » // It just won't save the key |
634 » » » » So(ds.Get(cs).Error(), ShouldContainSubstring, "
unable to extract $kind") | 639 » » » » cs := &FakePLS{IntID: 10, failSetMeta: true} |
| 640 » » » » So(ds.Get(cs), ShouldBeNil) |
635 }) | 641 }) |
636 }) | 642 }) |
637 | 643 |
638 Convey("ok", func() { | 644 Convey("ok", func() { |
639 Convey("Get", func() { | 645 Convey("Get", func() { |
640 cs := &CommonStruct{ID: 1} | 646 cs := &CommonStruct{ID: 1} |
641 So(ds.Get(cs), ShouldBeNil) | 647 So(ds.Get(cs), ShouldBeNil) |
642 So(cs.Value, ShouldEqual, 1) | 648 So(cs.Value, ShouldEqual, 1) |
643 }) | 649 }) |
644 | 650 |
(...skipping 21 matching lines...) Expand all Loading... |
666 Convey("Test GetAll", t, func() { | 672 Convey("Test GetAll", t, func() { |
667 c := info.Set(context.Background(), fakeInfo{}) | 673 c := info.Set(context.Background(), fakeInfo{}) |
668 c = SetRawFactory(c, fakeDatastoreFactory) | 674 c = SetRawFactory(c, fakeDatastoreFactory) |
669 ds := Get(c) | 675 ds := Get(c) |
670 So(ds, ShouldNotBeNil) | 676 So(ds, ShouldNotBeNil) |
671 | 677 |
672 q := NewQuery("").Limit(5) | 678 q := NewQuery("").Limit(5) |
673 | 679 |
674 Convey("bad", func() { | 680 Convey("bad", func() { |
675 Convey("nil target", func() { | 681 Convey("nil target", func() { |
676 » » » » So(ds.GetAll(q, (*[]PropertyMap)(nil)).Error(),
ShouldContainSubstring, "dst: <nil>") | 682 » » » » So(ds.GetAll(q, (*[]PropertyMap)(nil)), ShouldEr
rLike, "dst: <nil>") |
677 }) | 683 }) |
678 | 684 |
679 Convey("bad type", func() { | 685 Convey("bad type", func() { |
680 output := 100 | 686 output := 100 |
681 » » » » So(ds.GetAll(q, &output).Error(), ShouldContainS
ubstring, "invalid GetAll input type") | 687 » » » » So(func() { ds.GetAll(q, &output) }, ShouldPanic
Like, |
| 688 » » » » » "invalid argument type: expected slice,
got int") |
682 }) | 689 }) |
683 | 690 |
684 Convey("bad type (non pointer)", func() { | 691 Convey("bad type (non pointer)", func() { |
685 » » » » So(ds.GetAll(q, "moo").Error(), ShouldContainSub
string, "must have a ptr-to-slice") | 692 » » » » So(ds.GetAll(q, "moo"), ShouldErrLike, "must hav
e a ptr-to-slice") |
686 }) | 693 }) |
687 | 694 |
688 Convey("bad type (underspecified)", func() { | 695 Convey("bad type (underspecified)", func() { |
689 output := []PropertyLoadSaver(nil) | 696 output := []PropertyLoadSaver(nil) |
690 » » » » So(ds.GetAll(q, &output).Error(), ShouldContainS
ubstring, "invalid GetAll input type") | 697 » » » » So(ds.GetAll(q, &output), ShouldErrLike, "invali
d GetAll input type") |
691 }) | 698 }) |
692 }) | 699 }) |
693 | 700 |
694 Convey("ok", func() { | 701 Convey("ok", func() { |
695 Convey("*[]S", func() { | 702 Convey("*[]S", func() { |
696 output := []CommonStruct(nil) | 703 output := []CommonStruct(nil) |
697 So(ds.GetAll(q, &output), ShouldBeNil) | 704 So(ds.GetAll(q, &output), ShouldBeNil) |
698 So(len(output), ShouldEqual, 5) | 705 So(len(output), ShouldEqual, 5) |
699 for i, o := range output { | 706 for i, o := range output { |
700 So(o.ID, ShouldEqual, i+1) | 707 So(o.ID, ShouldEqual, i+1) |
(...skipping 20 matching lines...) Expand all Loading... |
721 So(o.IntID, ShouldEqual, i+1) | 728 So(o.IntID, ShouldEqual, i+1) |
722 So(o.Value, ShouldEqual, i) | 729 So(o.Value, ShouldEqual, i) |
723 } | 730 } |
724 }) | 731 }) |
725 | 732 |
726 Convey("*[]P (map)", func() { | 733 Convey("*[]P (map)", func() { |
727 output := []PropertyMap(nil) | 734 output := []PropertyMap(nil) |
728 So(ds.GetAll(q, &output), ShouldBeNil) | 735 So(ds.GetAll(q, &output), ShouldBeNil) |
729 So(len(output), ShouldEqual, 5) | 736 So(len(output), ShouldEqual, 5) |
730 for i, o := range output { | 737 for i, o := range output { |
731 » » » » » k, err := o.GetMeta("key") | 738 » » » » » k, ok := o.GetMeta("key") |
732 » » » » » So(err, ShouldBeNil) | 739 » » » » » So(ok, ShouldBeTrue) |
733 So(k.(*Key).IntID(), ShouldEqual, i+1) | 740 So(k.(*Key).IntID(), ShouldEqual, i+1) |
734 So(o["Value"][0].Value().(int64), Should
Equal, i) | 741 So(o["Value"][0].Value().(int64), Should
Equal, i) |
735 } | 742 } |
736 }) | 743 }) |
737 | 744 |
738 Convey("*[]*P", func() { | 745 Convey("*[]*P", func() { |
739 output := []*FakePLS(nil) | 746 output := []*FakePLS(nil) |
740 So(ds.GetAll(q, &output), ShouldBeNil) | 747 So(ds.GetAll(q, &output), ShouldBeNil) |
741 So(len(output), ShouldEqual, 5) | 748 So(len(output), ShouldEqual, 5) |
742 for i, o := range output { | 749 for i, o := range output { |
743 So(o.gotLoaded, ShouldBeTrue) | 750 So(o.gotLoaded, ShouldBeTrue) |
744 So(o.IntID, ShouldEqual, i+1) | 751 So(o.IntID, ShouldEqual, i+1) |
745 So(o.Value, ShouldEqual, i) | 752 So(o.Value, ShouldEqual, i) |
746 } | 753 } |
747 }) | 754 }) |
748 | 755 |
749 Convey("*[]*P (map)", func() { | 756 Convey("*[]*P (map)", func() { |
750 output := []*PropertyMap(nil) | 757 output := []*PropertyMap(nil) |
751 So(ds.GetAll(q, &output), ShouldBeNil) | 758 So(ds.GetAll(q, &output), ShouldBeNil) |
752 So(len(output), ShouldEqual, 5) | 759 So(len(output), ShouldEqual, 5) |
753 for i, op := range output { | 760 for i, op := range output { |
754 o := *op | 761 o := *op |
755 » » » » » k, err := o.GetMeta("key") | 762 » » » » » k, ok := o.GetMeta("key") |
756 » » » » » So(err, ShouldBeNil) | 763 » » » » » So(ok, ShouldBeTrue) |
757 So(k.(*Key).IntID(), ShouldEqual, i+1) | 764 So(k.(*Key).IntID(), ShouldEqual, i+1) |
758 So(o["Value"][0].Value().(int64), Should
Equal, i) | 765 So(o["Value"][0].Value().(int64), Should
Equal, i) |
759 } | 766 } |
760 }) | 767 }) |
761 | 768 |
762 Convey("*[]*Key", func() { | 769 Convey("*[]*Key", func() { |
763 output := []*Key(nil) | 770 output := []*Key(nil) |
764 So(ds.GetAll(q, &output), ShouldBeNil) | 771 So(ds.GetAll(q, &output), ShouldBeNil) |
765 So(len(output), ShouldEqual, 5) | 772 So(len(output), ShouldEqual, 5) |
766 for i, k := range output { | 773 for i, k := range output { |
(...skipping 11 matching lines...) Expand all Loading... |
778 Convey("Test Run", t, func() { | 785 Convey("Test Run", t, func() { |
779 c := info.Set(context.Background(), fakeInfo{}) | 786 c := info.Set(context.Background(), fakeInfo{}) |
780 c = SetRawFactory(c, fakeDatastoreFactory) | 787 c = SetRawFactory(c, fakeDatastoreFactory) |
781 ds := Get(c) | 788 ds := Get(c) |
782 So(ds, ShouldNotBeNil) | 789 So(ds, ShouldNotBeNil) |
783 | 790 |
784 q := NewQuery("kind").Limit(5) | 791 q := NewQuery("kind").Limit(5) |
785 | 792 |
786 Convey("bad", func() { | 793 Convey("bad", func() { |
787 assertBadTypePanics := func(cb interface{}) { | 794 assertBadTypePanics := func(cb interface{}) { |
788 » » » » defer func() { | 795 » » » » So(func() { ds.Run(q, cb) }, ShouldPanicLike, |
789 » » » » » err, _ := recover().(error) | 796 » » » » » "cb does not match the required callback
signature") |
790 » » » » » So(err, ShouldNotBeNil) | |
791 » » » » » So(err.Error(), ShouldContainSubstring, | |
792 » » » » » » "cb does not match the required
callback signature") | |
793 » » » » }() | |
794 » » » » So(ds.Run(q, cb), ShouldBeNil) | |
795 } | 797 } |
796 | 798 |
797 Convey("not a function", func() { | 799 Convey("not a function", func() { |
798 assertBadTypePanics("I am a potato") | 800 assertBadTypePanics("I am a potato") |
799 }) | 801 }) |
800 | 802 |
801 Convey("bad proto type", func() { | 803 Convey("bad proto type", func() { |
802 » » » » assertBadTypePanics(func(v int, _ CursorCB) bool
{ | 804 » » » » cb := func(v int, _ CursorCB) bool { |
803 panic("never here!") | 805 panic("never here!") |
804 » » » » }) | 806 » » » » } |
| 807 » » » » So(func() { ds.Run(q, cb) }, ShouldPanicLike, |
| 808 » » » » » "invalid argument type: int") |
805 }) | 809 }) |
806 | 810 |
807 Convey("wrong # args", func() { | 811 Convey("wrong # args", func() { |
808 assertBadTypePanics(func(v CommonStruct, _ Curso
rCB) { | 812 assertBadTypePanics(func(v CommonStruct, _ Curso
rCB) { |
809 panic("never here!") | 813 panic("never here!") |
810 }) | 814 }) |
811 }) | 815 }) |
812 | 816 |
813 Convey("wrong ret type", func() { | 817 Convey("wrong ret type", func() { |
814 assertBadTypePanics(func(v CommonStruct, _ Curso
rCB) error { | 818 assertBadTypePanics(func(v CommonStruct, _ Curso
rCB) error { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
861 } | 865 } |
862 So(fpls.Value, ShouldEqual, i) | 866 So(fpls.Value, ShouldEqual, i) |
863 i++ | 867 i++ |
864 return true | 868 return true |
865 }), ShouldBeNil) | 869 }), ShouldBeNil) |
866 }) | 870 }) |
867 | 871 |
868 Convey("*P (map)", func() { | 872 Convey("*P (map)", func() { |
869 i := 0 | 873 i := 0 |
870 So(ds.Run(q, func(pm *PropertyMap, _ CursorCB) b
ool { | 874 So(ds.Run(q, func(pm *PropertyMap, _ CursorCB) b
ool { |
871 » » » » » k, err := pm.GetMeta("key") | 875 » » » » » k, ok := pm.GetMeta("key") |
872 » » » » » So(err, ShouldBeNil) | 876 » » » » » So(ok, ShouldBeTrue) |
873 So(k.(*Key).IntID(), ShouldEqual, i+1) | 877 So(k.(*Key).IntID(), ShouldEqual, i+1) |
874 So((*pm)["Value"][0].Value(), ShouldEqua
l, i) | 878 So((*pm)["Value"][0].Value(), ShouldEqua
l, i) |
875 i++ | 879 i++ |
876 return true | 880 return true |
877 }), ShouldBeNil) | 881 }), ShouldBeNil) |
878 }) | 882 }) |
879 | 883 |
880 Convey("S", func() { | 884 Convey("S", func() { |
881 i := 0 | 885 i := 0 |
882 So(ds.Run(q, func(cs CommonStruct, _ CursorCB) b
ool { | 886 So(ds.Run(q, func(cs CommonStruct, _ CursorCB) b
ool { |
(...skipping 11 matching lines...) Expand all Loading... |
894 So(fpls.IntID, ShouldEqual, i+1) | 898 So(fpls.IntID, ShouldEqual, i+1) |
895 So(fpls.Value, ShouldEqual, i) | 899 So(fpls.Value, ShouldEqual, i) |
896 i++ | 900 i++ |
897 return true | 901 return true |
898 }), ShouldBeNil) | 902 }), ShouldBeNil) |
899 }) | 903 }) |
900 | 904 |
901 Convey("P (map)", func() { | 905 Convey("P (map)", func() { |
902 i := 0 | 906 i := 0 |
903 So(ds.Run(q, func(pm PropertyMap, _ CursorCB) bo
ol { | 907 So(ds.Run(q, func(pm PropertyMap, _ CursorCB) bo
ol { |
904 » » » » » k, err := pm.GetMeta("key") | 908 » » » » » k, ok := pm.GetMeta("key") |
905 » » » » » So(err, ShouldBeNil) | 909 » » » » » So(ok, ShouldBeTrue) |
906 So(k.(*Key).IntID(), ShouldEqual, i+1) | 910 So(k.(*Key).IntID(), ShouldEqual, i+1) |
907 So(pm["Value"][0].Value(), ShouldEqual,
i) | 911 So(pm["Value"][0].Value(), ShouldEqual,
i) |
908 i++ | 912 i++ |
909 return true | 913 return true |
910 }), ShouldBeNil) | 914 }), ShouldBeNil) |
911 }) | 915 }) |
912 | 916 |
913 Convey("Key", func() { | 917 Convey("Key", func() { |
914 i := 0 | 918 i := 0 |
915 So(ds.Run(q, func(k *Key, _ CursorCB) bool { | 919 So(ds.Run(q, func(k *Key, _ CursorCB) bool { |
916 So(k.IntID(), ShouldEqual, i+1) | 920 So(k.IntID(), ShouldEqual, i+1) |
917 i++ | 921 i++ |
918 return true | 922 return true |
919 }), ShouldBeNil) | 923 }), ShouldBeNil) |
920 }) | 924 }) |
921 | 925 |
922 }) | 926 }) |
923 }) | 927 }) |
924 } | 928 } |
OLD | NEW |