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 ds "github.com/luci/gae/service/datastore" | 12 ds "github.com/luci/gae/service/datastore" |
13 "github.com/luci/gae/service/info" | 13 "github.com/luci/gae/service/info" |
14 . "github.com/luci/luci-go/common/testing/assertions" | 14 . "github.com/luci/luci-go/common/testing/assertions" |
15 . "github.com/smartystreets/goconvey/convey" | 15 . "github.com/smartystreets/goconvey/convey" |
16 "golang.org/x/net/context" | 16 "golang.org/x/net/context" |
17 ) | 17 ) |
18 | 18 |
19 type qExpect struct { | 19 type qExpect struct { |
20 q *ds.Query | 20 q *ds.Query |
21 inTxn bool | 21 inTxn bool |
22 | 22 |
23 » get []ds.PropertyMap | 23 » get []ds.PropertyMap |
24 » keys []*ds.Key | 24 » keys []*ds.Key |
| 25 » count int |
25 } | 26 } |
26 | 27 |
27 type qExStage struct { | 28 type qExStage struct { |
28 addIdxs []*ds.IndexDefinition | 29 addIdxs []*ds.IndexDefinition |
29 putEnts []ds.PropertyMap | 30 putEnts []ds.PropertyMap |
30 delEnts []*ds.Key | 31 delEnts []*ds.Key |
31 | 32 |
32 expect []qExpect | 33 expect []qExpect |
33 | 34 |
34 extraFns []func(context.Context) | 35 extraFns []func(context.Context) |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 }, | 153 }, |
153 | 154 |
154 {q: nq("Kind").Gt("Val", 2).Lte("Val", 5), get:
[]ds.PropertyMap{ | 155 {q: nq("Kind").Gt("Val", 2).Lte("Val", 5), get:
[]ds.PropertyMap{ |
155 stage1Data[0], stage1Data[3], | 156 stage1Data[0], stage1Data[3], |
156 }}, | 157 }}, |
157 | 158 |
158 {q: nq("Kind").Gt("Val", 2).Lte("Val", 5).Order(
"-Val"), get: []ds.PropertyMap{ | 159 {q: nq("Kind").Gt("Val", 2).Lte("Val", 5).Order(
"-Val"), get: []ds.PropertyMap{ |
159 stage1Data[3], stage1Data[0], | 160 stage1Data[3], stage1Data[0], |
160 }}, | 161 }}, |
161 | 162 |
162 » » » » {q: nq("").Gt("__key__", key("Kind", 2)), get: [
]ds.PropertyMap{ | 163 » » » » {q: nq("").Gt("__key__", key("Kind", 2)), |
163 » » » » » // TODO(riannucci): determine if the rea
l datastore shows metadata | 164 » » » » » // count counts from the index with Keys
Only and so counts the deleted |
164 » » » » » // during kindless queries. The document
ation seems to imply so, but | 165 » » » » » // entity Unique/1. |
165 » » » » » // I'd like to be sure. | 166 » » » » » count: 8, |
166 » » » » » pmap("$key", key("Kind", 2, "__entity_gr
oup__", 1), Next, | 167 » » » » » get: []ds.PropertyMap{ |
167 » » » » » » "__version__", 1), | 168 » » » » » » // TODO(riannucci): determine if
the real datastore shows metadata |
168 » » » » » stage1Data[2], | 169 » » » » » » // during kindless queries. The
documentation seems to imply so, but |
169 » » » » » stage1Data[4], | 170 » » » » » » // I'd like to be sure. |
170 » » » » » // this is 5 because the value is retrie
ved from HEAD and not from | 171 » » » » » » pmap("$key", key("Kind", 2, "__e
ntity_group__", 1), Next, |
171 » » » » » // the index snapshot! | 172 » » » » » » » "__version__", 1), |
172 » » » » » pmap("$key", key("Kind", 3, "__entity_gr
oup__", 1), Next, | 173 » » » » » » stage1Data[2], |
173 » » » » » » "__version__", 5), | 174 » » » » » » stage1Data[4], |
174 » » » » » stage1Data[3], | 175 » » » » » » // this is 5 because the value i
s retrieved from HEAD and not from |
175 » » » » » pmap("$key", key("Kind", 6, "__entity_gr
oup__", 1), Next, | 176 » » » » » » // the index snapshot! |
176 » » » » » » "__version__", 1), | 177 » » » » » » pmap("$key", key("Kind", 3, "__e
ntity_group__", 1), Next, |
177 » » » » » pmap("$key", key("Unique", 1, "__entity_
group__", 1), Next, | 178 » » » » » » » "__version__", 5), |
178 » » » » » » "__version__", 2), | 179 » » » » » » stage1Data[3], |
179 » » » » }}, | 180 » » » » » » pmap("$key", key("Kind", 6, "__e
ntity_group__", 1), Next, |
| 181 » » » » » » » "__version__", 1), |
| 182 » » » » » » pmap("$key", key("Unique", 1, "_
_entity_group__", 1), Next, |
| 183 » » » » » » » "__version__", 2), |
| 184 » » » » » }}, |
180 | 185 |
181 {q: (nq("Kind"). | 186 {q: (nq("Kind"). |
182 Gt("Val", 2).Eq("Extra", "waffle"). | 187 Gt("Val", 2).Eq("Extra", "waffle"). |
183 Order("-Val"). | 188 Order("-Val"). |
184 Ancestor(key("Kind", 3))), | 189 Ancestor(key("Kind", 3))), |
185 get: []ds.PropertyMap{ | 190 get: []ds.PropertyMap{ |
186 stage1Data[2], | 191 stage1Data[2], |
187 stage2Data[0], | 192 stage2Data[0], |
188 stage2Data[1], | 193 stage2Data[1], |
189 }}, | 194 }}, |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 | 341 |
337 Convey(fmt.Sprintf("stage %d", i), func(
) { | 342 Convey(fmt.Sprintf("stage %d", i), func(
) { |
338 for j, expect := range stage.exp
ect { | 343 for j, expect := range stage.exp
ect { |
339 runner := func(f func(ic
context.Context) error, _ *ds.TransactionOptions) error { | 344 runner := func(f func(ic
context.Context) error, _ *ds.TransactionOptions) error { |
340 return f(c) | 345 return f(c) |
341 } | 346 } |
342 if expect.inTxn { | 347 if expect.inTxn { |
343 runner = data.Ru
nInTransaction | 348 runner = data.Ru
nInTransaction |
344 } | 349 } |
345 | 350 |
| 351 if expect.count == 0 { |
| 352 if len(expect.ke
ys) > 0 { |
| 353 expect.c
ount = len(expect.keys) |
| 354 } else { |
| 355 expect.c
ount = len(expect.get) |
| 356 } |
| 357 } |
| 358 |
346 if expect.keys != nil { | 359 if expect.keys != nil { |
347 » » » » » » » » err := runner(fu
nc(c context.Context) error { | 360 » » » » » » » » Convey(fmt.Sprin
tf("expect %d (keys)", j), func() { |
348 » » » » » » » » » data :=
ds.Get(c) | 361 » » » » » » » » » err := r
unner(func(c context.Context) error { |
349 » » » » » » » » » Convey(f
mt.Sprintf("expect %d (keys)", j), func() { | 362 » » » » » » » » » »
data := ds.Get(c) |
| 363 » » » » » » » » » »
count, err := data.Count(expect.q) |
| 364 » » » » » » » » » »
So(err, ShouldBeNil) |
| 365 » » » » » » » » » »
So(count, ShouldEqual, expect.count) |
| 366 |
350
rslt := []*ds.Key(nil) | 367
rslt := []*ds.Key(nil) |
351
So(data.GetAll(expect.q, &rslt), ShouldBeNil) | 368
So(data.GetAll(expect.q, &rslt), ShouldBeNil) |
352
So(len(rslt), ShouldEqual, len(expect.keys)) | 369
So(len(rslt), ShouldEqual, len(expect.keys)) |
353
for i, r := range rslt { | 370
for i, r := range rslt { |
354
So(r, ShouldResemble, expect.keys[i]) | 371
So(r, ShouldResemble, expect.keys[i]) |
355
} | 372
} |
356 » » » » » » » » » }) | 373 » » » » » » » » » »
return nil |
357 » » » » » » » » » return n
il | 374 » » » » » » » » » }, &ds.T
ransactionOptions{XG: true}) |
358 » » » » » » » » }, &ds.Transacti
onOptions{XG: true}) | 375 » » » » » » » » » So(err,
ShouldBeNil) |
359 » » » » » » » » So(err, ShouldBe
Nil) | 376 » » » » » » » » }) |
360 } | 377 } |
361 | 378 |
362 if expect.get != nil { | 379 if expect.get != nil { |
363 Convey(fmt.Sprin
tf("expect %d (data)", j), func() { | 380 Convey(fmt.Sprin
tf("expect %d (data)", j), func() { |
364 err := r
unner(func(c context.Context) error { | 381 err := r
unner(func(c context.Context) error { |
| 382
data := ds.Get(c) |
| 383
count, err := data.Count(expect.q) |
| 384
So(err, ShouldBeNil) |
| 385
So(count, ShouldEqual, expect.count) |
| 386 |
365
rslt := []ds.PropertyMap(nil) | 387
rslt := []ds.PropertyMap(nil) |
366
So(data.GetAll(expect.q, &rslt), ShouldBeNil) | 388
So(data.GetAll(expect.q, &rslt), ShouldBeNil) |
367
So(len(rslt), ShouldEqual, len(expect.get)) | 389
So(len(rslt), ShouldEqual, len(expect.get)) |
368
for i, r := range rslt { | 390
for i, r := range rslt { |
369
So(r, ShouldResemble, expect.get[i]) | 391
So(r, ShouldResemble, expect.get[i]) |
370
} | 392
} |
371
return nil | 393
return nil |
372 }, &ds.T
ransactionOptions{XG: true}) | 394 }, &ds.T
ransactionOptions{XG: true}) |
373 So(err,
ShouldBeNil) | 395 So(err,
ShouldBeNil) |
374 }) | 396 }) |
375 } | 397 } |
376 } | 398 } |
377 | 399 |
378 for j, fn := range stage.extraFn
s { | 400 for j, fn := range stage.extraFn
s { |
379 Convey(fmt.Sprintf("extr
aFn %d", j), func() { | 401 Convey(fmt.Sprintf("extr
aFn %d", j), func() { |
380 fn(c) | 402 fn(c) |
381 }) | 403 }) |
382 } | 404 } |
383 }) | 405 }) |
384 } | 406 } |
385 }) | 407 }) |
386 } | 408 } |
387 }) | 409 }) |
388 } | 410 } |
OLD | NEW |