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" |
11 | 11 |
12 dsS "github.com/luci/gae/service/datastore" | 12 dsS "github.com/luci/gae/service/datastore" |
13 "github.com/luci/gae/service/datastore/dskey" | |
14 "github.com/luci/gae/service/datastore/serialize" | 13 "github.com/luci/gae/service/datastore/serialize" |
15 infoS "github.com/luci/gae/service/info" | 14 infoS "github.com/luci/gae/service/info" |
16 . "github.com/smartystreets/goconvey/convey" | 15 . "github.com/smartystreets/goconvey/convey" |
17 "golang.org/x/net/context" | 16 "golang.org/x/net/context" |
18 ) | 17 ) |
19 | 18 |
20 func TestDatastoreKinder(t *testing.T) { | |
21 t.Parallel() | |
22 | |
23 Convey("Datastore keys", t, func() { | |
24 c := Use(context.Background()) | |
25 ds := dsS.Get(c) | |
26 So(ds, ShouldNotBeNil) | |
27 | |
28 Convey("implements DSNewKeyer", func() { | |
29 Convey("NewKey", func() { | |
30 key := ds.NewKey("nerd", "stringID", 0, nil) | |
31 So(key, ShouldNotBeNil) | |
32 So(key.Kind(), ShouldEqual, "nerd") | |
33 So(key.StringID(), ShouldEqual, "stringID") | |
34 So(key.IntID(), ShouldEqual, 0) | |
35 So(key.Parent(), ShouldBeNil) | |
36 So(key.AppID(), ShouldEqual, "dev~app") | |
37 So(key.Namespace(), ShouldEqual, "") | |
38 So(key.String(), ShouldEqual, "/nerd,stringID") | |
39 So(key.Incomplete(), ShouldBeFalse) | |
40 So(key.Valid(false, "dev~app", ""), ShouldBeTrue ) | |
41 }) | |
42 }) | |
43 | |
44 }) | |
45 } | |
46 | |
47 type MetaGroup struct { | 19 type MetaGroup struct { |
48 » _id int64 `gae:"$id,1"` | 20 » _id int64 `gae:"$id,1"` |
49 » _kind string `gae:"$kind,__entity_group__"` | 21 » _kind string `gae:"$kind,__entity_group__"` |
50 » Parent dsS.Key `gae:"$parent"` | 22 » Parent *dsS.Key `gae:"$parent"` |
51 | 23 |
52 Version int64 `gae:"__version__"` | 24 Version int64 `gae:"__version__"` |
53 } | 25 } |
54 | 26 |
55 func testGetMeta(c context.Context, k dsS.Key) int64 { | 27 func testGetMeta(c context.Context, k *dsS.Key) int64 { |
56 ds := dsS.Get(c) | 28 ds := dsS.Get(c) |
57 » mg := &MetaGroup{Parent: dskey.Root(k)} | 29 » mg := &MetaGroup{Parent: k.Root()} |
58 if err := ds.Get(mg); err != nil { | 30 if err := ds.Get(mg); err != nil { |
59 panic(err) | 31 panic(err) |
60 } | 32 } |
61 return mg.Version | 33 return mg.Version |
62 } | 34 } |
63 | 35 |
64 var pls = dsS.GetPLS | 36 var pls = dsS.GetPLS |
65 | 37 |
66 type Foo struct { | 38 type Foo struct { |
67 » Id int64 `gae:"$id"` | 39 » Id int64 `gae:"$id"` |
68 » Parent dsS.Key `gae:"$parent"` | 40 » Parent *dsS.Key `gae:"$parent"` |
69 | 41 |
70 Val int | 42 Val int |
71 } | 43 } |
72 | 44 |
73 func TestDatastoreSingleReadWriter(t *testing.T) { | 45 func TestDatastoreSingleReadWriter(t *testing.T) { |
74 t.Parallel() | 46 t.Parallel() |
75 | 47 |
76 Convey("Datastore single reads and writes", t, func() { | 48 Convey("Datastore single reads and writes", t, func() { |
77 c := Use(context.Background()) | 49 c := Use(context.Background()) |
78 ds := dsS.Get(c) | 50 ds := dsS.Get(c) |
79 So(ds, ShouldNotBeNil) | 51 So(ds, ShouldNotBeNil) |
80 | 52 |
81 Convey("getting objects that DNE is an error", func() { | 53 Convey("getting objects that DNE is an error", func() { |
82 So(ds.Get(&Foo{Id: 1}), ShouldEqual, dsS.ErrNoSuchEntity ) | 54 So(ds.Get(&Foo{Id: 1}), ShouldEqual, dsS.ErrNoSuchEntity ) |
83 }) | 55 }) |
84 | 56 |
85 Convey("bad namespaces fail", func() { | 57 Convey("bad namespaces fail", func() { |
86 _, err := infoS.Get(c).Namespace("$$blzyall") | 58 _, err := infoS.Get(c).Namespace("$$blzyall") |
87 So(err.Error(), ShouldContainSubstring, "namespace \"$$b lzyall\" does not match") | 59 So(err.Error(), ShouldContainSubstring, "namespace \"$$b lzyall\" does not match") |
88 }) | 60 }) |
89 | 61 |
90 Convey("Can Put stuff", func() { | 62 Convey("Can Put stuff", func() { |
91 // with an incomplete key! | 63 // with an incomplete key! |
92 f := &Foo{Val: 10} | 64 f := &Foo{Val: 10} |
93 So(ds.Put(f), ShouldBeNil) | 65 So(ds.Put(f), ShouldBeNil) |
94 k := ds.KeyForObj(f) | 66 k := ds.KeyForObj(f) |
95 » » » So(k.String(), ShouldEqual, "/Foo,1") | 67 » » » So(k.String(), ShouldEqual, "dev~app::/Foo,1") |
iannucci
2015/09/18 04:31:52
I got pissed off at the Key string format not actu
| |
96 | 68 |
97 Convey("and Get it back", func() { | 69 Convey("and Get it back", func() { |
98 newFoo := &Foo{Id: 1} | 70 newFoo := &Foo{Id: 1} |
99 So(ds.Get(newFoo), ShouldBeNil) | 71 So(ds.Get(newFoo), ShouldBeNil) |
100 So(newFoo, ShouldResemble, f) | 72 So(newFoo, ShouldResemble, f) |
101 | 73 |
102 Convey("but it's hidden from a different namespa ce", func() { | 74 Convey("but it's hidden from a different namespa ce", func() { |
103 c, err := infoS.Get(c).Namespace("whomba t") | 75 c, err := infoS.Get(c).Namespace("whomba t") |
104 So(err, ShouldBeNil) | 76 So(err, ShouldBeNil) |
105 ds = dsS.Get(c) | 77 ds = dsS.Get(c) |
(...skipping 17 matching lines...) Expand all Loading... | |
123 So(testGetMeta(c, k), ShouldEqual, 1) | 95 So(testGetMeta(c, k), ShouldEqual, 1) |
124 | 96 |
125 foos := make([]Foo, 10) | 97 foos := make([]Foo, 10) |
126 for i := range foos { | 98 for i := range foos { |
127 foos[i].Val = 10 | 99 foos[i].Val = 10 |
128 foos[i].Parent = k | 100 foos[i].Parent = k |
129 } | 101 } |
130 So(ds.PutMulti(foos), ShouldBeNil) | 102 So(ds.PutMulti(foos), ShouldBeNil) |
131 So(testGetMeta(c, k), ShouldEqual, 11) | 103 So(testGetMeta(c, k), ShouldEqual, 11) |
132 | 104 |
133 » » » » keys := make([]dsS.Key, len(foos)) | 105 » » » » keys := make([]*dsS.Key, len(foos)) |
134 for i, f := range foos { | 106 for i, f := range foos { |
135 keys[i] = ds.KeyForObj(&f) | 107 keys[i] = ds.KeyForObj(&f) |
136 } | 108 } |
137 | 109 |
138 Convey("ensure that group versions persist acros s deletes", func() { | 110 Convey("ensure that group versions persist acros s deletes", func() { |
139 So(ds.DeleteMulti(append(keys, k)), Shou ldBeNil) | 111 So(ds.DeleteMulti(append(keys, k)), Shou ldBeNil) |
140 | 112 |
141 // TODO(riannucci): replace with a Count query instead of this cast | 113 // TODO(riannucci): replace with a Count query instead of this cast |
142 /* | 114 /* |
143 ents := ds.(*dsImpl).data.head.G etCollection("ents:") | 115 ents := ds.(*dsImpl).data.head.G etCollection("ents:") |
(...skipping 27 matching lines...) Expand all Loading... | |
171 }) | 143 }) |
172 | 144 |
173 }) | 145 }) |
174 }) | 146 }) |
175 | 147 |
176 Convey("implements DSTransactioner", func() { | 148 Convey("implements DSTransactioner", func() { |
177 Convey("Put", func() { | 149 Convey("Put", func() { |
178 f := &Foo{Val: 10} | 150 f := &Foo{Val: 10} |
179 So(ds.Put(f), ShouldBeNil) | 151 So(ds.Put(f), ShouldBeNil) |
180 k := ds.KeyForObj(f) | 152 k := ds.KeyForObj(f) |
181 » » » » So(k.String(), ShouldEqual, "/Foo,1") | 153 » » » » So(k.String(), ShouldEqual, "dev~app::/Foo,1") |
182 | 154 |
183 Convey("can Put new entity groups", func() { | 155 Convey("can Put new entity groups", func() { |
184 err := ds.RunInTransaction(func(c contex t.Context) error { | 156 err := ds.RunInTransaction(func(c contex t.Context) error { |
185 ds := dsS.Get(c) | 157 ds := dsS.Get(c) |
186 | 158 |
187 f := &Foo{Val: 100} | 159 f := &Foo{Val: 100} |
188 So(ds.Put(f), ShouldBeNil) | 160 So(ds.Put(f), ShouldBeNil) |
189 So(f.Id, ShouldEqual, 2) | 161 So(f.Id, ShouldEqual, 2) |
190 | 162 |
191 f.Id = 0 | 163 f.Id = 0 |
(...skipping 13 matching lines...) Expand all Loading... | |
205 So(ds.Get(f), ShouldBeNil) | 177 So(ds.Get(f), ShouldBeNil) |
206 So(f.Val, ShouldEqual, 200) | 178 So(f.Val, ShouldEqual, 200) |
207 }) | 179 }) |
208 | 180 |
209 Convey("can Put new entities in a current group" , func() { | 181 Convey("can Put new entities in a current group" , func() { |
210 err := ds.RunInTransaction(func(c contex t.Context) error { | 182 err := ds.RunInTransaction(func(c contex t.Context) error { |
211 ds := dsS.Get(c) | 183 ds := dsS.Get(c) |
212 | 184 |
213 f := &Foo{Val: 100, Parent: k} | 185 f := &Foo{Val: 100, Parent: k} |
214 So(ds.Put(f), ShouldBeNil) | 186 So(ds.Put(f), ShouldBeNil) |
215 » » » » » » So(ds.KeyForObj(f).String(), Sho uldEqual, "/Foo,1/Foo,1") | 187 » » » » » » So(ds.KeyForObj(f).String(), Sho uldEqual, "dev~app::/Foo,1/Foo,1") |
216 | 188 |
217 f.Id = 0 | 189 f.Id = 0 |
218 f.Val = 200 | 190 f.Val = 200 |
219 So(ds.Put(f), ShouldBeNil) | 191 So(ds.Put(f), ShouldBeNil) |
220 » » » » » » So(ds.KeyForObj(f).String(), Sho uldEqual, "/Foo,1/Foo,2") | 192 » » » » » » So(ds.KeyForObj(f).String(), Sho uldEqual, "dev~app::/Foo,1/Foo,2") |
221 | 193 |
222 return nil | 194 return nil |
223 }, nil) | 195 }, nil) |
224 So(err, ShouldBeNil) | 196 So(err, ShouldBeNil) |
225 | 197 |
226 f := &Foo{Id: 1, Parent: k} | 198 f := &Foo{Id: 1, Parent: k} |
227 So(ds.Get(f), ShouldBeNil) | 199 So(ds.Get(f), ShouldBeNil) |
228 So(f.Val, ShouldEqual, 100) | 200 So(f.Val, ShouldEqual, 100) |
229 | 201 |
230 f.Id = 2 | 202 f.Id = 2 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
545 ds := dsS.Get(Use(context.Background())) | 517 ds := dsS.Get(Use(context.Background())) |
546 m := Model{ID: 1} | 518 m := Model{ID: 1} |
547 So(ds.Put(&m), ShouldBeNil) | 519 So(ds.Put(&m), ShouldBeNil) |
548 | 520 |
549 // Reset to something non zero to ensure zero is fetched. | 521 // Reset to something non zero to ensure zero is fetched. |
550 m.Time = time.Now().UTC() | 522 m.Time = time.Now().UTC() |
551 So(ds.Get(&m), ShouldBeNil) | 523 So(ds.Get(&m), ShouldBeNil) |
552 So(m.Time.IsZero(), ShouldBeTrue) | 524 So(m.Time.IsZero(), ShouldBeTrue) |
553 }) | 525 }) |
554 } | 526 } |
OLD | NEW |