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 "strings" | 9 "strings" |
10 "testing" | 10 "testing" |
11 "time" | 11 "time" |
12 | 12 |
| 13 "github.com/luci/gae/service/blobstore" |
13 ds "github.com/luci/gae/service/datastore" | 14 ds "github.com/luci/gae/service/datastore" |
14 "github.com/luci/gae/service/info" | 15 "github.com/luci/gae/service/info" |
| 16 "golang.org/x/net/context" |
| 17 |
15 . "github.com/luci/luci-go/common/testing/assertions" | 18 . "github.com/luci/luci-go/common/testing/assertions" |
16 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
17 "golang.org/x/net/context" | |
18 ) | 20 ) |
19 | 21 |
20 type qExpect struct { | 22 type qExpect struct { |
21 q *ds.Query | 23 q *ds.Query |
22 inTxn bool | 24 inTxn bool |
23 | 25 |
24 get []ds.PropertyMap | 26 get []ds.PropertyMap |
25 keys []*ds.Key | 27 keys []*ds.Key |
26 count int | 28 count int |
27 } | 29 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 pmap("$key", key("Kind", 3, "Kind", 2), Next, | 80 pmap("$key", key("Kind", 3, "Kind", 2), Next, |
79 "Val", 3, 4, Next, | 81 "Val", 3, 4, Next, |
80 "Extra", "hello", "waffle", | 82 "Extra", "hello", "waffle", |
81 ), | 83 ), |
82 pmap("$key", key("Kind", 3, "Kind", 3), Next, | 84 pmap("$key", key("Kind", 3, "Kind", 3), Next, |
83 "Val", 3, 4, 2, 1, Next, | 85 "Val", 3, 4, 2, 1, Next, |
84 "Extra", "nuts", | 86 "Extra", "nuts", |
85 ), | 87 ), |
86 } | 88 } |
87 | 89 |
| 90 var collapsedData = []ds.PropertyMap{ |
| 91 // PTTime |
| 92 pmap("$key", key("Kind", 1), Next, |
| 93 "Date", time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC),
Next, |
| 94 ), |
| 95 pmap("$key", key("Kind", 2), Next, |
| 96 "Date", time.Date(2000, time.March, 1, 1, 1, 1, 1, time.UTC), Ne
xt, |
| 97 ), |
| 98 |
| 99 // PTBlobKey |
| 100 pmap("$key", key("Kind", 3), Next, |
| 101 "Key", blobstore.Key("foo"), Next, |
| 102 ), |
| 103 pmap("$key", key("Kind", 4), Next, |
| 104 "Key", blobstore.Key("qux"), Next, |
| 105 ), |
| 106 |
| 107 // PTBytes |
| 108 pmap("$key", key("Kind", 5), Next, |
| 109 "Val", []byte("ohai"), Next, |
| 110 ), |
| 111 pmap("$key", key("Kind", 6), Next, |
| 112 "Val", []byte("uwutm8"), Next, |
| 113 ), |
| 114 } |
| 115 |
88 var queryExecutionTests = []qExTest{ | 116 var queryExecutionTests = []qExTest{ |
89 {"basic", []qExStage{ | 117 {"basic", []qExStage{ |
90 { | 118 { |
91 addIdxs: []*ds.IndexDefinition{ | 119 addIdxs: []*ds.IndexDefinition{ |
92 indx("Unrelated", "-thing", "bob", "-__key__"), | 120 indx("Unrelated", "-thing", "bob", "-__key__"), |
93 indx("Wat", "deep", "opt", "other"), | 121 indx("Wat", "deep", "opt", "other"), |
94 indx("Wat", "meep", "opt", "other"), | 122 indx("Wat", "meep", "opt", "other"), |
95 }, | 123 }, |
96 }, | 124 }, |
97 | 125 |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 {q: nq("Unique").Gt("__key__", key("AKind", 5)).
Lte("__key__", key("Zeta", "prime")), | 406 {q: nq("Unique").Gt("__key__", key("AKind", 5)).
Lte("__key__", key("Zeta", "prime")), |
379 keys: []*ds.Key{key("Unique", 1)}, | 407 keys: []*ds.Key{key("Unique", 1)}, |
380 get: []ds.PropertyMap{}}, | 408 get: []ds.PropertyMap{}}, |
381 | 409 |
382 {q: nq("Kind").Eq("Val", 1, 3), get: []ds.Proper
tyMap{ | 410 {q: nq("Kind").Eq("Val", 1, 3), get: []ds.Proper
tyMap{ |
383 stage1Data[0], stage2Data[2], | 411 stage1Data[0], stage2Data[2], |
384 }}, | 412 }}, |
385 }, | 413 }, |
386 }, | 414 }, |
387 }}, | 415 }}, |
| 416 {"collapsed types", []qExStage{ |
| 417 { |
| 418 putEnts: collapsedData, |
| 419 }, |
| 420 { |
| 421 expect: []qExpect{ |
| 422 // PTTime |
| 423 { |
| 424 q: nq("Kind").Lte("Date", time.Date(2000
, time.February, 1, 1, 1, 1, 1, time.UTC)), |
| 425 get: []ds.PropertyMap{ |
| 426 collapsedData[0], |
| 427 }, |
| 428 }, |
| 429 { |
| 430 q: nq("Kind").Eq("Date", time.Date(2000,
time.March, 1, 1, 1, 1, 1, time.UTC)), |
| 431 get: []ds.PropertyMap{ |
| 432 collapsedData[1], |
| 433 }, |
| 434 }, |
| 435 |
| 436 // PTBlobKey |
| 437 { |
| 438 q: nq("Kind").Lte("Key", blobstore.Key("
foo")), |
| 439 get: []ds.PropertyMap{ |
| 440 collapsedData[2], |
| 441 }, |
| 442 }, |
| 443 { |
| 444 q: nq("Kind").Eq("Key", blobstore.Key("q
ux")), |
| 445 get: []ds.PropertyMap{ |
| 446 collapsedData[3], |
| 447 }, |
| 448 }, |
| 449 |
| 450 // PTBytes |
| 451 { |
| 452 q: nq("Kind").Lte("Val", []byte("ohai"))
, |
| 453 get: []ds.PropertyMap{ |
| 454 collapsedData[4], |
| 455 }, |
| 456 }, |
| 457 { |
| 458 q: nq("Kind").Eq("Val", []byte("uwutm8")
), |
| 459 get: []ds.PropertyMap{ |
| 460 collapsedData[5], |
| 461 }, |
| 462 }, |
| 463 }, |
| 464 }, |
| 465 }}, |
388 } | 466 } |
389 | 467 |
390 func TestQueryExecution(t *testing.T) { | 468 func TestQueryExecution(t *testing.T) { |
391 t.Parallel() | 469 t.Parallel() |
392 | 470 |
393 Convey("Test query execution", t, func() { | 471 Convey("Test query execution", t, func() { |
394 c, err := info.Get(Use(context.Background())).Namespace("ns") | 472 c, err := info.Get(Use(context.Background())).Namespace("ns") |
395 if err != nil { | 473 if err != nil { |
396 panic(err) | 474 panic(err) |
397 } | 475 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 err := r
unner(func(c context.Context) error { | 539 err := r
unner(func(c context.Context) error { |
462
data := ds.Get(c) | 540
data := ds.Get(c) |
463
count, err := data.Count(expect.q) | 541
count, err := data.Count(expect.q) |
464
So(err, ShouldBeNil) | 542
So(err, ShouldBeNil) |
465
So(count, ShouldEqual, expect.count) | 543
So(count, ShouldEqual, expect.count) |
466 | 544 |
467
rslt := []ds.PropertyMap(nil) | 545
rslt := []ds.PropertyMap(nil) |
468
So(data.GetAll(expect.q, &rslt), ShouldBeNil) | 546
So(data.GetAll(expect.q, &rslt), ShouldBeNil) |
469
So(len(rslt), ShouldEqual, len(expect.get)) | 547
So(len(rslt), ShouldEqual, len(expect.get)) |
470
for i, r := range rslt { | 548
for i, r := range rslt { |
471 » » » » » » » » » »
» So(r, ShouldResemble, expect.get[i]) | 549 » » » » » » » » » »
» So(r, ShouldResembleV, expect.get[i]) |
472
} | 550
} |
473
return nil | 551
return nil |
474 }, &ds.T
ransactionOptions{XG: true}) | 552 }, &ds.T
ransactionOptions{XG: true}) |
475 So(err,
ShouldBeNil) | 553 So(err,
ShouldBeNil) |
476 }) | 554 }) |
477 } | 555 } |
478 } | 556 } |
479 | 557 |
480 for j, fn := range stage.extraFn
s { | 558 for j, fn := range stage.extraFn
s { |
481 Convey(fmt.Sprintf("extr
aFn %d", j), func() { | 559 Convey(fmt.Sprintf("extr
aFn %d", j), func() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 count, err := data.Count(q) | 591 count, err := data.Count(q) |
514 So(err, ShouldErrLike, "Insufficient indexes") | 592 So(err, ShouldErrLike, "Insufficient indexes") |
515 | 593 |
516 testing.AutoIndex(true) | 594 testing.AutoIndex(true) |
517 | 595 |
518 count, err = data.Count(q) | 596 count, err = data.Count(q) |
519 So(err, ShouldBeNil) | 597 So(err, ShouldBeNil) |
520 So(count, ShouldEqual, 2) | 598 So(count, ShouldEqual, 2) |
521 }) | 599 }) |
522 } | 600 } |
OLD | NEW |