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 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "testing" | 9 "testing" |
10 "time" | 10 "time" |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 return mg.Version | 34 return mg.Version |
35 } | 35 } |
36 | 36 |
37 var pls = dsS.GetPLS | 37 var pls = dsS.GetPLS |
38 | 38 |
39 type Foo struct { | 39 type Foo struct { |
40 ID int64 `gae:"$id"` | 40 ID int64 `gae:"$id"` |
41 Parent *dsS.Key `gae:"$parent"` | 41 Parent *dsS.Key `gae:"$parent"` |
42 | 42 |
43 » Val int | 43 » Val int |
| 44 » Name string |
44 } | 45 } |
45 | 46 |
46 func TestDatastoreSingleReadWriter(t *testing.T) { | 47 func TestDatastoreSingleReadWriter(t *testing.T) { |
47 t.Parallel() | 48 t.Parallel() |
48 | 49 |
49 Convey("Datastore single reads and writes", t, func() { | 50 Convey("Datastore single reads and writes", t, func() { |
50 c := Use(context.Background()) | 51 c := Use(context.Background()) |
51 ds := dsS.Get(c) | 52 ds := dsS.Get(c) |
52 So(ds, ShouldNotBeNil) | 53 So(ds, ShouldNotBeNil) |
53 | 54 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 vals := make([]dsS.PropertyMap, len(keys
)) | 146 vals := make([]dsS.PropertyMap, len(keys
)) |
146 for i := range vals { | 147 for i := range vals { |
147 vals[i] = dsS.PropertyMap{} | 148 vals[i] = dsS.PropertyMap{} |
148 So(vals[i].SetMeta("key", keys[i
]), ShouldBeTrue) | 149 So(vals[i].SetMeta("key", keys[i
]), ShouldBeTrue) |
149 } | 150 } |
150 So(ds.GetMulti(vals), ShouldBeNil) | 151 So(ds.GetMulti(vals), ShouldBeNil) |
151 | 152 |
152 for i, val := range vals { | 153 for i, val := range vals { |
153 So(val, ShouldResemble, dsS.Prop
ertyMap{ | 154 So(val, ShouldResemble, dsS.Prop
ertyMap{ |
154 "Val": {dsS.MkProperty(
10)}, | 155 "Val": {dsS.MkProperty(
10)}, |
| 156 "Name": {dsS.MkProperty(
"")}, |
155 "$key": {dsS.MkPropertyN
I(keys[i])}, | 157 "$key": {dsS.MkPropertyN
I(keys[i])}, |
156 }) | 158 }) |
157 } | 159 } |
158 }) | 160 }) |
159 | 161 |
160 }) | 162 }) |
161 | 163 |
162 Convey("allocating ids prevents their use", func() { | 164 Convey("allocating ids prevents their use", func() { |
163 start, err := ds.AllocateIDs(ds.MakeKey("Foo", 0
), 100) | 165 start, err := ds.AllocateIDs(ds.MakeKey("Foo", 0
), 100) |
164 So(err, ShouldBeNil) | 166 So(err, ShouldBeNil) |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 So(ds.Put(&Model{ID: 1, Value: []int64{20, 30}}), ShouldBeNil) | 626 So(ds.Put(&Model{ID: 1, Value: []int64{20, 30}}), ShouldBeNil) |
625 | 627 |
626 vals := []dsS.PropertyMap{} | 628 vals := []dsS.PropertyMap{} |
627 So(ds.GetAll(dsS.NewQuery("Model").Project("Value"), &vals), Sho
uldBeNil) | 629 So(ds.GetAll(dsS.NewQuery("Model").Project("Value"), &vals), Sho
uldBeNil) |
628 So(len(vals), ShouldEqual, 2) | 630 So(len(vals), ShouldEqual, 2) |
629 | 631 |
630 So(vals[0]["Value"][0].Value(), ShouldEqual, 20) | 632 So(vals[0]["Value"][0].Value(), ShouldEqual, 20) |
631 So(vals[1]["Value"][0].Value(), ShouldEqual, 30) | 633 So(vals[1]["Value"][0].Value(), ShouldEqual, 30) |
632 }) | 634 }) |
633 } | 635 } |
| 636 |
| 637 func TestAddIndexes(t *testing.T) { |
| 638 t.Parallel() |
| 639 |
| 640 Convey("Test Testable.AddIndexes", t, func() { |
| 641 ctx := UseWithAppID(context.Background(), "aid") |
| 642 namespaces := []string{"", "good", "news", "everyone"} |
| 643 |
| 644 Convey("After adding datastore entries, can query against indexe
s in various namespaces", func() { |
| 645 foos := []*Foo{ |
| 646 {ID: 1, Val: 1, Name: "foo"}, |
| 647 {ID: 2, Val: 2, Name: "bar"}, |
| 648 {ID: 3, Val: 2, Name: "baz"}, |
| 649 } |
| 650 for _, ns := range namespaces { |
| 651 So(dsS.Get(infoS.Get(ctx).MustNamespace(ns)).Put
Multi(foos), ShouldBeNil) |
| 652 } |
| 653 |
| 654 // Initial query, no indexes, will fail. |
| 655 dsS.Get(ctx).Testable().CatchupIndexes() |
| 656 |
| 657 var results []*Foo |
| 658 q := dsS.NewQuery("Foo").Eq("Val", 2).Gte("Name", "bar") |
| 659 So(dsS.Get(ctx).GetAll(q, &results), ShouldErrLike, "Ins
ufficient indexes") |
| 660 |
| 661 // Add index for default namespace. |
| 662 dsS.Get(ctx).Testable().AddIndexes(&dsS.IndexDefinition{ |
| 663 Kind: "Foo", |
| 664 SortBy: []dsS.IndexColumn{ |
| 665 {Property: "Val"}, |
| 666 {Property: "Name"}, |
| 667 }, |
| 668 }) |
| 669 dsS.Get(ctx).Testable().CatchupIndexes() |
| 670 |
| 671 for _, ns := range namespaces { |
| 672 results = nil |
| 673 So(dsS.Get(infoS.Get(ctx).MustNamespace(ns)).Get
All(q, &results), ShouldBeNil) |
| 674 So(len(results), ShouldEqual, 2) |
| 675 } |
| 676 |
| 677 // Add "foos" to a new namesapce, then confirm that it g
ets indexed. |
| 678 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).PutMulti
(foos), ShouldBeNil) |
| 679 dsS.Get(ctx).Testable().CatchupIndexes() |
| 680 |
| 681 results = nil |
| 682 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).GetAll(q
, &results), ShouldBeNil) |
| 683 So(len(results), ShouldEqual, 2) |
| 684 }) |
| 685 }) |
| 686 } |
OLD | NEW |