OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be 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 » Name string |
| 45 » Multi []string |
45 } | 46 } |
46 | 47 |
47 func TestDatastoreSingleReadWriter(t *testing.T) { | 48 func TestDatastoreSingleReadWriter(t *testing.T) { |
48 t.Parallel() | 49 t.Parallel() |
49 | 50 |
50 Convey("Datastore single reads and writes", t, func() { | 51 Convey("Datastore single reads and writes", t, func() { |
51 c := Use(context.Background()) | 52 c := Use(context.Background()) |
52 ds := dsS.Get(c) | 53 ds := dsS.Get(c) |
53 So(ds, ShouldNotBeNil) | 54 So(ds, ShouldNotBeNil) |
54 | 55 |
55 Convey("getting objects that DNE is an error", func() { | 56 Convey("getting objects that DNE is an error", func() { |
56 So(ds.Get(&Foo{ID: 1}), ShouldEqual, dsS.ErrNoSuchEntity
) | 57 So(ds.Get(&Foo{ID: 1}), ShouldEqual, dsS.ErrNoSuchEntity
) |
57 }) | 58 }) |
58 | 59 |
59 Convey("bad namespaces fail", func() { | 60 Convey("bad namespaces fail", func() { |
60 _, err := infoS.Get(c).Namespace("$$blzyall") | 61 _, err := infoS.Get(c).Namespace("$$blzyall") |
61 So(err.Error(), ShouldContainSubstring, "namespace \"$$b
lzyall\" does not match") | 62 So(err.Error(), ShouldContainSubstring, "namespace \"$$b
lzyall\" does not match") |
62 }) | 63 }) |
63 | 64 |
64 Convey("Can Put stuff", func() { | 65 Convey("Can Put stuff", func() { |
65 // with an incomplete key! | 66 // with an incomplete key! |
66 » » » f := &Foo{Val: 10} | 67 » » » f := &Foo{Val: 10, Multi: []string{"foo", "bar"}} |
67 So(ds.Put(f), ShouldBeNil) | 68 So(ds.Put(f), ShouldBeNil) |
68 k := ds.KeyForObj(f) | 69 k := ds.KeyForObj(f) |
69 So(k.String(), ShouldEqual, "dev~app::/Foo,1") | 70 So(k.String(), ShouldEqual, "dev~app::/Foo,1") |
70 | 71 |
71 Convey("and Get it back", func() { | 72 Convey("and Get it back", func() { |
72 newFoo := &Foo{ID: 1} | 73 newFoo := &Foo{ID: 1} |
73 So(ds.Get(newFoo), ShouldBeNil) | 74 So(ds.Get(newFoo), ShouldBeNil) |
74 So(newFoo, ShouldResemble, f) | 75 So(newFoo, ShouldResemble, f) |
75 | 76 |
76 Convey("but it's hidden from a different namespa
ce", func() { | 77 Convey("but it's hidden from a different namespa
ce", func() { |
77 c, err := infoS.Get(c).Namespace("whomba
t") | 78 c, err := infoS.Get(c).Namespace("whomba
t") |
78 So(err, ShouldBeNil) | 79 So(err, ShouldBeNil) |
79 ds = dsS.Get(c) | 80 ds = dsS.Get(c) |
80 So(ds.Get(f), ShouldEqual, dsS.ErrNoSuch
Entity) | 81 So(ds.Get(f), ShouldEqual, dsS.ErrNoSuch
Entity) |
81 }) | 82 }) |
82 | 83 |
83 Convey("and we can Delete it", func() { | 84 Convey("and we can Delete it", func() { |
84 So(ds.Delete(k), ShouldBeNil) | 85 So(ds.Delete(k), ShouldBeNil) |
85 So(ds.Get(newFoo), ShouldEqual, dsS.ErrN
oSuchEntity) | 86 So(ds.Get(newFoo), ShouldEqual, dsS.ErrN
oSuchEntity) |
86 }) | 87 }) |
87 | 88 |
88 }) | 89 }) |
| 90 Convey("Can Get it back as a PropertyMap", func() { |
| 91 pmap := dsS.PropertyMap{ |
| 92 "$id": propNI(1), |
| 93 "$kind": propNI("Foo"), |
| 94 } |
| 95 So(ds.Get(pmap), ShouldBeNil) |
| 96 So(pmap, ShouldResemble, dsS.PropertyMap{ |
| 97 "$id": propNI(1), |
| 98 "$kind": propNI("Foo"), |
| 99 "Name": prop(""), |
| 100 "Val": prop(10), |
| 101 "Multi": dsS.PropertySlice{prop("foo"),
prop("bar")}, |
| 102 }) |
| 103 }) |
89 Convey("Deleteing with a bogus key is bad", func() { | 104 Convey("Deleteing with a bogus key is bad", func() { |
90 So(ds.Delete(ds.NewKey("Foo", "wat", 100, nil)),
ShouldEqual, dsS.ErrInvalidKey) | 105 So(ds.Delete(ds.NewKey("Foo", "wat", 100, nil)),
ShouldEqual, dsS.ErrInvalidKey) |
91 }) | 106 }) |
92 Convey("Deleteing a DNE entity is fine", func() { | 107 Convey("Deleteing a DNE entity is fine", func() { |
93 So(ds.Delete(ds.NewKey("Foo", "wat", 0, nil)), S
houldBeNil) | 108 So(ds.Delete(ds.NewKey("Foo", "wat", 0, nil)), S
houldBeNil) |
94 }) | 109 }) |
95 | 110 |
96 Convey("Deleting entities from a nonexistant namespace w
orks", func() { | 111 Convey("Deleting entities from a nonexistant namespace w
orks", func() { |
97 aid := infoS.Get(c).FullyQualifiedAppID() | 112 aid := infoS.Get(c).FullyQualifiedAppID() |
98 keys := make([]*dsS.Key, 10) | 113 keys := make([]*dsS.Key, 10) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 Convey("can Get", func() { | 160 Convey("can Get", func() { |
146 vals := make([]dsS.PropertyMap, len(keys
)) | 161 vals := make([]dsS.PropertyMap, len(keys
)) |
147 for i := range vals { | 162 for i := range vals { |
148 vals[i] = dsS.PropertyMap{} | 163 vals[i] = dsS.PropertyMap{} |
149 So(vals[i].SetMeta("key", keys[i
]), ShouldBeTrue) | 164 So(vals[i].SetMeta("key", keys[i
]), ShouldBeTrue) |
150 } | 165 } |
151 So(ds.GetMulti(vals), ShouldBeNil) | 166 So(ds.GetMulti(vals), ShouldBeNil) |
152 | 167 |
153 for i, val := range vals { | 168 for i, val := range vals { |
154 So(val, ShouldResemble, dsS.Prop
ertyMap{ | 169 So(val, ShouldResemble, dsS.Prop
ertyMap{ |
155 » » » » » » » "Val": {dsS.MkProperty(
10)}, | 170 » » » » » » » "Val": dsS.MkProperty(1
0), |
156 » » » » » » » "Name": {dsS.MkProperty(
"")}, | 171 » » » » » » » "Name": dsS.MkProperty("
"), |
157 » » » » » » » "$key": {dsS.MkPropertyN
I(keys[i])}, | 172 » » » » » » » "$key": dsS.MkPropertyNI
(keys[i]), |
158 }) | 173 }) |
159 } | 174 } |
160 }) | 175 }) |
161 | 176 |
162 }) | 177 }) |
163 | 178 |
164 Convey("allocating ids prevents their use", func() { | 179 Convey("allocating ids prevents their use", func() { |
165 keys := ds.NewIncompleteKeys(100, "Foo", nil) | 180 keys := ds.NewIncompleteKeys(100, "Foo", nil) |
166 So(ds.AllocateIDs(keys), ShouldBeNil) | 181 So(ds.AllocateIDs(keys), ShouldBeNil) |
167 So(len(keys), ShouldEqual, 100) | 182 So(len(keys), ShouldEqual, 100) |
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 type Model struct { | 714 type Model struct { |
700 ID int64 `gae:"$id"` | 715 ID int64 `gae:"$id"` |
701 Value []int64 | 716 Value []int64 |
702 } | 717 } |
703 So(ds.Put(&Model{ID: 1, Value: []int64{20, 30}}), ShouldBeNil) | 718 So(ds.Put(&Model{ID: 1, Value: []int64{20, 30}}), ShouldBeNil) |
704 | 719 |
705 vals := []dsS.PropertyMap{} | 720 vals := []dsS.PropertyMap{} |
706 So(ds.GetAll(dsS.NewQuery("Model").Project("Value"), &vals), Sho
uldBeNil) | 721 So(ds.GetAll(dsS.NewQuery("Model").Project("Value"), &vals), Sho
uldBeNil) |
707 So(len(vals), ShouldEqual, 2) | 722 So(len(vals), ShouldEqual, 2) |
708 | 723 |
709 » » So(vals[0]["Value"][0].Value(), ShouldEqual, 20) | 724 » » So(vals[0].Slice("Value")[0].Value(), ShouldEqual, 20) |
710 » » So(vals[1]["Value"][0].Value(), ShouldEqual, 30) | 725 » » So(vals[1].Slice("Value")[0].Value(), ShouldEqual, 30) |
711 }) | 726 }) |
712 } | 727 } |
713 | 728 |
714 func TestAddIndexes(t *testing.T) { | 729 func TestAddIndexes(t *testing.T) { |
715 t.Parallel() | 730 t.Parallel() |
716 | 731 |
717 Convey("Test Testable.AddIndexes", t, func() { | 732 Convey("Test Testable.AddIndexes", t, func() { |
718 ctx := UseWithAppID(context.Background(), "aid") | 733 ctx := UseWithAppID(context.Background(), "aid") |
719 namespaces := []string{"", "good", "news", "everyone"} | 734 namespaces := []string{"", "good", "news", "everyone"} |
720 | 735 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 // Add "foos" to a new namespace, then confirm that it g
ets indexed. | 774 // Add "foos" to a new namespace, then confirm that it g
ets indexed. |
760 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).PutMulti
(foos), ShouldBeNil) | 775 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).PutMulti
(foos), ShouldBeNil) |
761 dsS.Get(ctx).Testable().CatchupIndexes() | 776 dsS.Get(ctx).Testable().CatchupIndexes() |
762 | 777 |
763 results = nil | 778 results = nil |
764 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).GetAll(q
, &results), ShouldBeNil) | 779 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).GetAll(q
, &results), ShouldBeNil) |
765 So(len(results), ShouldEqual, 2) | 780 So(len(results), ShouldEqual, 2) |
766 }) | 781 }) |
767 }) | 782 }) |
768 } | 783 } |
OLD | NEW |