| 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 "bytes" | 10 "bytes" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 func fakeDatastoreFactory(c context.Context, wantTxn bool) RawInterface { | 25 func fakeDatastoreFactory(c context.Context, wantTxn bool) RawInterface { |
| 26 i := info.Get(c) | 26 i := info.Get(c) |
| 27 fds := fakeDatastore{ | 27 fds := fakeDatastore{ |
| 28 aid: i.FullyQualifiedAppID(), | 28 aid: i.FullyQualifiedAppID(), |
| 29 } | 29 } |
| 30 fds.ns, _ = i.GetNamespace() | 30 fds.ns, _ = i.GetNamespace() |
| 31 return &fds | 31 return &fds |
| 32 } | 32 } |
| 33 | 33 |
| 34 var ( |
| 35 errFail = errors.New("Individual element fail") |
| 36 errFailAll = errors.New("Operation fail") |
| 37 ) |
| 38 |
| 34 type fakeDatastore struct { | 39 type fakeDatastore struct { |
| 35 RawInterface | 40 RawInterface |
| 36 aid string | 41 aid string |
| 37 ns string | 42 ns string |
| 38 } | 43 } |
| 39 | 44 |
| 40 func (f *fakeDatastore) mkKey(elems ...interface{}) *Key { | 45 func (f *fakeDatastore) mkKey(elems ...interface{}) *Key { |
| 41 return MakeKey(f.aid, f.ns, elems...) | 46 return MakeKey(f.aid, f.ns, elems...) |
| 42 } | 47 } |
| 43 | 48 |
| 49 func (f *fakeDatastore) newKey(kind, stringID string, intID int64, parent *Key)
*Key { |
| 50 return NewKey(f.aid, f.ns, kind, stringID, intID, parent) |
| 51 } |
| 52 |
| 53 func (f *fakeDatastore) AllocateIDs(keys []*Key, cb PutMultiCB) error { |
| 54 if keys[0].Kind() == "FailAll" { |
| 55 return errFailAll |
| 56 } |
| 57 for i, k := range keys { |
| 58 if k.Kind() == "Fail" { |
| 59 cb(nil, errFail) |
| 60 } else { |
| 61 cb(f.newKey(k.Kind(), "", int64(i+1), k.Parent()), nil) |
| 62 } |
| 63 } |
| 64 return nil |
| 65 } |
| 66 |
| 44 func (f *fakeDatastore) Run(fq *FinalizedQuery, cb RawRunCB) error { | 67 func (f *fakeDatastore) Run(fq *FinalizedQuery, cb RawRunCB) error { |
| 45 lim, _ := fq.Limit() | 68 lim, _ := fq.Limit() |
| 46 | 69 |
| 47 cursCB := func() (Cursor, error) { | 70 cursCB := func() (Cursor, error) { |
| 48 return fakeCursor("CURSOR"), nil | 71 return fakeCursor("CURSOR"), nil |
| 49 } | 72 } |
| 50 | 73 |
| 51 for i := int32(0); i < lim; i++ { | 74 for i := int32(0); i < lim; i++ { |
| 52 if v, ok := fq.eqFilts["$err_single"]; ok { | 75 if v, ok := fq.eqFilts["$err_single"]; ok { |
| 53 idx := fq.eqFilts["$err_single_idx"][0].Value().(int64) | 76 idx := fq.eqFilts["$err_single_idx"][0].Value().(int64) |
| 54 if idx == int64(i) { | 77 if idx == int64(i) { |
| 55 return errors.New(v[0].Value().(string)) | 78 return errors.New(v[0].Value().(string)) |
| 56 } | 79 } |
| 57 } | 80 } |
| 58 k := f.mkKey("Kind", i+1) | 81 k := f.mkKey("Kind", i+1) |
| 59 if i == 10 { | 82 if i == 10 { |
| 60 k = f.mkKey("Kind", "eleven") | 83 k = f.mkKey("Kind", "eleven") |
| 61 } | 84 } |
| 62 pm := PropertyMap{"Value": {MkProperty(i)}} | 85 pm := PropertyMap{"Value": {MkProperty(i)}} |
| 63 if err := cb(k, pm, cursCB); err != nil { | 86 if err := cb(k, pm, cursCB); err != nil { |
| 64 if err == Stop { | 87 if err == Stop { |
| 65 err = nil | 88 err = nil |
| 66 } | 89 } |
| 67 return err | 90 return err |
| 68 } | 91 } |
| 69 } | 92 } |
| 70 return nil | 93 return nil |
| 71 } | 94 } |
| 72 | 95 |
| 73 var ( | |
| 74 errFail = errors.New("Individual element fail") | |
| 75 errFailAll = errors.New("Operation fail") | |
| 76 ) | |
| 77 | |
| 78 func (f *fakeDatastore) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { | 96 func (f *fakeDatastore) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { |
| 79 if keys[0].Kind() == "FailAll" { | 97 if keys[0].Kind() == "FailAll" { |
| 80 return errFailAll | 98 return errFailAll |
| 81 } | 99 } |
| 82 _, assertExtra := vals[0].GetMeta("assertExtra") | 100 _, assertExtra := vals[0].GetMeta("assertExtra") |
| 83 for i, k := range keys { | 101 for i, k := range keys { |
| 84 err := error(nil) | 102 err := error(nil) |
| 85 if k.Kind() == "Fail" { | 103 if k.Kind() == "Fail" { |
| 86 err = errFail | 104 err = errFail |
| 87 } else { | 105 } else { |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 | 406 |
| 389 func TestPopulateKey(t *testing.T) { | 407 func TestPopulateKey(t *testing.T) { |
| 390 t.Parallel() | 408 t.Parallel() |
| 391 | 409 |
| 392 Convey("Test PopulateKey", t, func() { | 410 Convey("Test PopulateKey", t, func() { |
| 393 k := NewKey("app", "namespace", "kind", "", 1337, nil) | 411 k := NewKey("app", "namespace", "kind", "", 1337, nil) |
| 394 | 412 |
| 395 Convey("Can set the key of a common struct.", func() { | 413 Convey("Can set the key of a common struct.", func() { |
| 396 var cs CommonStruct | 414 var cs CommonStruct |
| 397 | 415 |
| 398 » » » PopulateKey(&cs, k) | 416 » » » So(PopulateKey(&cs, k), ShouldBeTrue) |
| 399 So(cs.ID, ShouldEqual, 1337) | 417 So(cs.ID, ShouldEqual, 1337) |
| 400 }) | 418 }) |
| 401 | 419 |
| 402 Convey("Will not set the value of a singleton struct.", func() { | 420 Convey("Will not set the value of a singleton struct.", func() { |
| 403 var ss SingletonStruct | 421 var ss SingletonStruct |
| 404 | 422 |
| 405 » » » PopulateKey(&ss, k) | 423 » » » So(PopulateKey(&ss, k), ShouldBeFalse) |
| 406 So(ss.id, ShouldEqual, 0) | 424 So(ss.id, ShouldEqual, 0) |
| 407 }) | 425 }) |
| 408 | 426 |
| 409 Convey("Will panic when setting the key of a bad struct.", func(
) { | 427 Convey("Will panic when setting the key of a bad struct.", func(
) { |
| 410 var bs badStruct | 428 var bs badStruct |
| 411 | 429 |
| 412 So(func() { PopulateKey(&bs, k) }, ShouldPanic) | 430 So(func() { PopulateKey(&bs, k) }, ShouldPanic) |
| 413 }) | 431 }) |
| 414 | 432 |
| 415 Convey("Will panic when setting the key of a broken PLS struct."
, func() { | 433 Convey("Will panic when setting the key of a broken PLS struct."
, func() { |
| 416 var broken permaBad | 434 var broken permaBad |
| 417 | 435 |
| 418 So(func() { PopulateKey(&broken, k) }, ShouldPanic) | 436 So(func() { PopulateKey(&broken, k) }, ShouldPanic) |
| 419 }) | 437 }) |
| 420 }) | 438 }) |
| 421 } | 439 } |
| 422 | 440 |
| 441 func TestAllocateIDs(t *testing.T) { |
| 442 t.Parallel() |
| 443 |
| 444 Convey("A testing environment", t, func() { |
| 445 c := info.Set(context.Background(), fakeInfo{}) |
| 446 c = SetRawFactory(c, fakeDatastoreFactory) |
| 447 ds := Get(c) |
| 448 |
| 449 Convey("Testing AllocateIDs", func() { |
| 450 Convey("Will return nil if no entities are supplied.", f
unc() { |
| 451 So(ds.AllocateIDs(), ShouldBeNil) |
| 452 }) |
| 453 |
| 454 Convey("single struct", func() { |
| 455 cs := CommonStruct{Value: 1} |
| 456 So(ds.AllocateIDs(&cs), ShouldBeNil) |
| 457 So(cs.ID, ShouldEqual, 1) |
| 458 }) |
| 459 |
| 460 Convey("struct slice", func() { |
| 461 csSlice := []*CommonStruct{{Value: 1}, {Value: 2
}} |
| 462 So(ds.AllocateIDs(csSlice), ShouldBeNil) |
| 463 So(csSlice, ShouldResemble, []*CommonStruct{{ID:
1, Value: 1}, {ID: 2, Value: 2}}) |
| 464 }) |
| 465 |
| 466 Convey("single key", func() { |
| 467 singleKey := ds.MakeKey("FooParent", "BarParent"
, "Foo", "Bar") |
| 468 So(ds.AllocateIDs(singleKey), ShouldBeNil) |
| 469 So(singleKey.Equal(ds.MakeKey("FooParent", "BarP
arent", "Foo", 1)), ShouldBeTrue) |
| 470 }) |
| 471 |
| 472 Convey("key slice", func() { |
| 473 keySlice := []*Key{ds.MakeKey("Foo", "Bar"), ds.
MakeKey("Baz", "Qux")} |
| 474 So(ds.AllocateIDs(keySlice), ShouldBeNil) |
| 475 So(keySlice[0].Equal(ds.MakeKey("Foo", 1)), Shou
ldBeTrue) |
| 476 So(keySlice[1].Equal(ds.MakeKey("Baz", 2)), Shou
ldBeTrue) |
| 477 }) |
| 478 |
| 479 Convey("fail all key slice", func() { |
| 480 keySlice := []*Key{ds.MakeKey("FailAll", "oops")
, ds.MakeKey("Baz", "Qux")} |
| 481 So(ds.AllocateIDs(keySlice), ShouldEqual, errFai
lAll) |
| 482 So(keySlice[0].StringID(), ShouldEqual, "oops") |
| 483 So(keySlice[1].StringID(), ShouldEqual, "Qux") |
| 484 }) |
| 485 |
| 486 Convey("fail key slice", func() { |
| 487 keySlice := []*Key{ds.MakeKey("Fail", "oops"), d
s.MakeKey("Baz", "Qux")} |
| 488 So(ds.AllocateIDs(keySlice), ShouldResemble, err
ors.MultiError{errFail, nil}) |
| 489 So(keySlice[0].StringID(), ShouldEqual, "oops") |
| 490 So(keySlice[1].IntID(), ShouldEqual, 2) |
| 491 }) |
| 492 |
| 493 Convey("vararg with errors", func() { |
| 494 successSlice := []CommonStruct{{Value: 0}, {Valu
e: 1}} |
| 495 failSlice := []FakePLS{{Kind: "Fail"}, {Value: 3
}} |
| 496 emptySlice := []CommonStruct(nil) |
| 497 cs0 := CommonStruct{Value: 4} |
| 498 cs1 := FakePLS{Kind: "Fail", Value: 5} |
| 499 singleKey := ds.MakeKey("FooParent", "BarParent"
, "Foo", "Bar") |
| 500 keySlice := []*Key{ds.MakeKey("Foo", "Bar"), ds.
MakeKey("Baz", "Qux")} |
| 501 fpls := FakePLS{StringID: "ohai", Value: 6} |
| 502 |
| 503 err := ds.AllocateIDs(successSlice, failSlice, e
mptySlice, &cs0, &cs1, singleKey, keySlice, &fpls) |
| 504 So(err, ShouldResemble, errors.MultiError{ |
| 505 nil, errors.MultiError{errFail, nil}, ni
l, nil, errFail, nil, nil, nil}) |
| 506 So(successSlice[0].ID, ShouldEqual, 1) |
| 507 So(successSlice[1].ID, ShouldEqual, 2) |
| 508 So(failSlice[1].IntID, ShouldEqual, 4) |
| 509 So(cs0.ID, ShouldEqual, 5) |
| 510 So(singleKey.Equal(ds.MakeKey("FooParent", "BarP
arent", "Foo", 7)), ShouldBeTrue) |
| 511 So(keySlice[0].Equal(ds.MakeKey("Foo", 8)), Shou
ldBeTrue) |
| 512 So(keySlice[1].Equal(ds.MakeKey("Baz", 9)), Shou
ldBeTrue) |
| 513 So(fpls.IntID, ShouldEqual, 10) |
| 514 }) |
| 515 }) |
| 516 }) |
| 517 } |
| 518 |
| 423 func TestPut(t *testing.T) { | 519 func TestPut(t *testing.T) { |
| 424 t.Parallel() | 520 t.Parallel() |
| 425 | 521 |
| 426 Convey("A testing environment", t, func() { | 522 Convey("A testing environment", t, func() { |
| 427 c := info.Set(context.Background(), fakeInfo{}) | 523 c := info.Set(context.Background(), fakeInfo{}) |
| 428 c = SetRawFactory(c, fakeDatastoreFactory) | 524 c = SetRawFactory(c, fakeDatastoreFactory) |
| 429 ds := Get(c) | 525 ds := Get(c) |
| 430 | 526 |
| 431 Convey("Testing Put", func() { | 527 Convey("Testing Put", func() { |
| 432 Convey("bad", func() { | 528 Convey("bad", func() { |
| (...skipping 1368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1801 if err != nil { | 1897 if err != nil { |
| 1802 panic(fmt.Errorf("failed to find absolute path f
or `%s`", sameLevelDir)) | 1898 panic(fmt.Errorf("failed to find absolute path f
or `%s`", sameLevelDir)) |
| 1803 } | 1899 } |
| 1804 | 1900 |
| 1805 ids, err := FindAndParseIndexYAML(abs) | 1901 ids, err := FindAndParseIndexYAML(abs) |
| 1806 So(err, ShouldBeNil) | 1902 So(err, ShouldBeNil) |
| 1807 So(ids[1].Kind, ShouldEqual, "Test Foo") | 1903 So(ids[1].Kind, ShouldEqual, "Test Foo") |
| 1808 }) | 1904 }) |
| 1809 }) | 1905 }) |
| 1810 } | 1906 } |
| OLD | NEW |