Index: service/datastore/datastore_test.go |
diff --git a/service/datastore/datastore_test.go b/service/datastore/datastore_test.go |
index 09fa63515e1fbae50678fe7fe59c0eb9c70356f9..3625961293f6e39de0a54a756c1c1313cc4d511a 100644 |
--- a/service/datastore/datastore_test.go |
+++ b/service/datastore/datastore_test.go |
@@ -31,6 +31,11 @@ func fakeDatastoreFactory(c context.Context, wantTxn bool) RawInterface { |
return &fds |
} |
+var ( |
+ errFail = errors.New("Individual element fail") |
+ errFailAll = errors.New("Operation fail") |
+) |
+ |
type fakeDatastore struct { |
RawInterface |
aid string |
@@ -41,6 +46,24 @@ func (f *fakeDatastore) mkKey(elems ...interface{}) *Key { |
return MakeKey(f.aid, f.ns, elems...) |
} |
+func (f *fakeDatastore) newKey(kind, stringID string, intID int64, parent *Key) *Key { |
+ return NewKey(f.aid, f.ns, kind, stringID, intID, parent) |
+} |
+ |
+func (f *fakeDatastore) AllocateIDs(keys []*Key, cb PutMultiCB) error { |
+ if keys[0].Kind() == "FailAll" { |
+ return errFailAll |
+ } |
+ for i, k := range keys { |
+ if k.Kind() == "Fail" { |
+ cb(nil, errFail) |
+ } else { |
+ cb(f.newKey(k.Kind(), "", int64(i+1), k.Parent()), nil) |
+ } |
+ } |
+ return nil |
+} |
+ |
func (f *fakeDatastore) Run(fq *FinalizedQuery, cb RawRunCB) error { |
lim, _ := fq.Limit() |
@@ -70,11 +93,6 @@ func (f *fakeDatastore) Run(fq *FinalizedQuery, cb RawRunCB) error { |
return nil |
} |
-var ( |
- errFail = errors.New("Individual element fail") |
- errFailAll = errors.New("Operation fail") |
-) |
- |
func (f *fakeDatastore) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB) error { |
if keys[0].Kind() == "FailAll" { |
return errFailAll |
@@ -395,14 +413,14 @@ func TestPopulateKey(t *testing.T) { |
Convey("Can set the key of a common struct.", func() { |
var cs CommonStruct |
- PopulateKey(&cs, k) |
+ So(PopulateKey(&cs, k), ShouldBeTrue) |
So(cs.ID, ShouldEqual, 1337) |
}) |
Convey("Will not set the value of a singleton struct.", func() { |
var ss SingletonStruct |
- PopulateKey(&ss, k) |
+ So(PopulateKey(&ss, k), ShouldBeFalse) |
So(ss.id, ShouldEqual, 0) |
}) |
@@ -420,6 +438,84 @@ func TestPopulateKey(t *testing.T) { |
}) |
} |
+func TestAllocateIDs(t *testing.T) { |
+ t.Parallel() |
+ |
+ Convey("A testing environment", t, func() { |
+ c := info.Set(context.Background(), fakeInfo{}) |
+ c = SetRawFactory(c, fakeDatastoreFactory) |
+ ds := Get(c) |
+ |
+ Convey("Testing AllocateIDs", func() { |
+ Convey("Will return nil if no entities are supplied.", func() { |
+ So(ds.AllocateIDs(), ShouldBeNil) |
+ }) |
+ |
+ Convey("single struct", func() { |
+ cs := CommonStruct{Value: 1} |
+ So(ds.AllocateIDs(&cs), ShouldBeNil) |
+ So(cs.ID, ShouldEqual, 1) |
+ }) |
+ |
+ Convey("struct slice", func() { |
+ csSlice := []*CommonStruct{{Value: 1}, {Value: 2}} |
+ So(ds.AllocateIDs(csSlice), ShouldBeNil) |
+ So(csSlice, ShouldResemble, []*CommonStruct{{ID: 1, Value: 1}, {ID: 2, Value: 2}}) |
+ }) |
+ |
+ Convey("single key", func() { |
+ singleKey := ds.MakeKey("FooParent", "BarParent", "Foo", "Bar") |
+ So(ds.AllocateIDs(singleKey), ShouldBeNil) |
+ So(singleKey.Equal(ds.MakeKey("FooParent", "BarParent", "Foo", 1)), ShouldBeTrue) |
+ }) |
+ |
+ Convey("key slice", func() { |
+ keySlice := []*Key{ds.MakeKey("Foo", "Bar"), ds.MakeKey("Baz", "Qux")} |
+ So(ds.AllocateIDs(keySlice), ShouldBeNil) |
+ So(keySlice[0].Equal(ds.MakeKey("Foo", 1)), ShouldBeTrue) |
+ So(keySlice[1].Equal(ds.MakeKey("Baz", 2)), ShouldBeTrue) |
+ }) |
+ |
+ Convey("fail all key slice", func() { |
+ keySlice := []*Key{ds.MakeKey("FailAll", "oops"), ds.MakeKey("Baz", "Qux")} |
+ So(ds.AllocateIDs(keySlice), ShouldEqual, errFailAll) |
+ So(keySlice[0].StringID(), ShouldEqual, "oops") |
+ So(keySlice[1].StringID(), ShouldEqual, "Qux") |
+ }) |
+ |
+ Convey("fail key slice", func() { |
+ keySlice := []*Key{ds.MakeKey("Fail", "oops"), ds.MakeKey("Baz", "Qux")} |
+ So(ds.AllocateIDs(keySlice), ShouldResemble, errors.MultiError{errFail, nil}) |
+ So(keySlice[0].StringID(), ShouldEqual, "oops") |
+ So(keySlice[1].IntID(), ShouldEqual, 2) |
+ }) |
+ |
+ Convey("vararg with errors", func() { |
+ successSlice := []CommonStruct{{Value: 0}, {Value: 1}} |
+ failSlice := []FakePLS{{Kind: "Fail"}, {Value: 3}} |
+ emptySlice := []CommonStruct(nil) |
+ cs0 := CommonStruct{Value: 4} |
+ cs1 := FakePLS{Kind: "Fail", Value: 5} |
+ singleKey := ds.MakeKey("FooParent", "BarParent", "Foo", "Bar") |
+ keySlice := []*Key{ds.MakeKey("Foo", "Bar"), ds.MakeKey("Baz", "Qux")} |
+ fpls := FakePLS{StringID: "ohai", Value: 6} |
+ |
+ err := ds.AllocateIDs(successSlice, failSlice, emptySlice, &cs0, &cs1, singleKey, keySlice, &fpls) |
+ So(err, ShouldResemble, errors.MultiError{ |
+ nil, errors.MultiError{errFail, nil}, nil, nil, errFail, nil, nil, nil}) |
+ So(successSlice[0].ID, ShouldEqual, 1) |
+ So(successSlice[1].ID, ShouldEqual, 2) |
+ So(failSlice[1].IntID, ShouldEqual, 4) |
+ So(cs0.ID, ShouldEqual, 5) |
+ So(singleKey.Equal(ds.MakeKey("FooParent", "BarParent", "Foo", 7)), ShouldBeTrue) |
+ So(keySlice[0].Equal(ds.MakeKey("Foo", 8)), ShouldBeTrue) |
+ So(keySlice[1].Equal(ds.MakeKey("Baz", 9)), ShouldBeTrue) |
+ So(fpls.IntID, ShouldEqual, 10) |
+ }) |
+ }) |
+ }) |
+} |
+ |
func TestPut(t *testing.T) { |
t.Parallel() |