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 "bytes" |
8 "fmt" | 9 "fmt" |
9 "math" | 10 "math" |
10 "testing" | 11 "testing" |
11 | 12 |
12 dsS "github.com/luci/gae/service/datastore" | 13 dsS "github.com/luci/gae/service/datastore" |
13 infoS "github.com/luci/gae/service/info" | 14 infoS "github.com/luci/gae/service/info" |
14 . "github.com/smartystreets/goconvey/convey" | 15 . "github.com/smartystreets/goconvey/convey" |
15 "golang.org/x/net/context" | 16 "golang.org/x/net/context" |
16 ) | 17 ) |
17 | 18 |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 }) | 443 }) |
443 | 444 |
444 Convey("normalize ensures orders make sense", func() { | 445 Convey("normalize ensures orders make sense", func() { |
445 q := ds.NewQuery("Cool") | 446 q := ds.NewQuery("Cool") |
446 q = q.Filter("cat =", 19).Filter("bob =", 10).Order("bob
").Order("bob") | 447 q = q.Filter("cat =", 19).Filter("bob =", 10).Order("bob
").Order("bob") |
447 | 448 |
448 Convey("removes dups and equality orders", func() { | 449 Convey("removes dups and equality orders", func() { |
449 q = q.Order("wat") | 450 q = q.Order("wat") |
450 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) | 451 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) |
451 So(qi.err, ShouldBeNil) | 452 So(qi.err, ShouldBeNil) |
452 » » » » So(qi.order, ShouldResemble, []queryOrder{{"wat"
, qASC}}) | 453 » » » » So(qi.order, ShouldResemble, []dsS.IndexColumn{{
Property: "wat"}}) |
453 }) | 454 }) |
454 | 455 |
455 Convey("keeps inequality orders", func() { | 456 Convey("keeps inequality orders", func() { |
456 q = q.Order("wat") | 457 q = q.Order("wat") |
457 q := q.Filter("bob >", 10).Filter("wat <", 29) | 458 q := q.Filter("bob >", 10).Filter("wat <", 29) |
458 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) | 459 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) |
459 » » » » So(qi.order, ShouldResemble, []queryOrder{{"bob"
, qASC}, {"wat", qASC}}) | 460 » » » » So(qi.order, ShouldResemble, []dsS.IndexColumn{{
Property: "bob"}, {Property: "wat"}}) |
460 So(qi.err.Error(), ShouldContainSubstring, "Only
one inequality") | 461 So(qi.err.Error(), ShouldContainSubstring, "Only
one inequality") |
461 }) | 462 }) |
462 | 463 |
463 Convey("if we equality-filter on __key__, order is ditch
ed", func() { | 464 Convey("if we equality-filter on __key__, order is ditch
ed", func() { |
464 q = q.Order("wat") | 465 q = q.Order("wat") |
465 q := q.Filter("__key__ =", ds.NewKey("Foo", "wat
", 0, nil)) | 466 q := q.Filter("__key__ =", ds.NewKey("Foo", "wat
", 0, nil)) |
466 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) | 467 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) |
467 » » » » So(qi.order, ShouldResemble, []queryOrder(nil)) | 468 » » » » So(qi.order, ShouldResemble, []dsS.IndexColumn(n
il)) |
468 So(qi.err, ShouldBeNil) | 469 So(qi.err, ShouldBeNil) |
469 }) | 470 }) |
470 | 471 |
471 Convey("if we order by key and something else, key domin
ates", func() { | 472 Convey("if we order by key and something else, key domin
ates", func() { |
472 q := q.Order("__key__").Order("wat") | 473 q := q.Order("__key__").Order("wat") |
473 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) | 474 qi := q.(*queryImpl).normalize().checkCorrectnes
s("", false) |
474 » » » » So(qi.order, ShouldResemble, []queryOrder{{"__ke
y__", qASC}}) | 475 » » » » So(qi.order, ShouldResemble, []dsS.IndexColumn{{
Property: "__key__"}}) |
475 So(qi.err, ShouldBeNil) | 476 So(qi.err, ShouldBeNil) |
476 }) | 477 }) |
477 }) | 478 }) |
478 | 479 |
479 Convey("can create bad queries", func() { | 480 Convey("can create bad queries", func() { |
480 q := ds.NewQuery("Foo") | 481 q := ds.NewQuery("Foo") |
481 | 482 |
482 Convey("bad filter ops", func() { | 483 Convey("bad filter ops", func() { |
483 q := q.Filter("Bob !", "value") | 484 q := q.Filter("Bob !", "value") |
484 So(q.(*queryImpl).err.Error(), ShouldContainSubs
tring, "invalid operator \"!\"") | 485 So(q.(*queryImpl).err.Error(), ShouldContainSubs
tring, "invalid operator \"!\"") |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 }) | 572 }) |
572 Convey("kindless with decending-__key__ orders", func()
{ | 573 Convey("kindless with decending-__key__ orders", func()
{ |
573 q := ds.NewQuery("").Order("-__key__") | 574 q := ds.NewQuery("").Order("-__key__") |
574 qi := q.(*queryImpl).checkCorrectness("", false) | 575 qi := q.(*queryImpl).checkCorrectness("", false) |
575 So(qi.err.Error(), ShouldContainSubstring, "kind
is required for all orders") | 576 So(qi.err.Error(), ShouldContainSubstring, "kind
is required for all orders") |
576 }) | 577 }) |
577 }) | 578 }) |
578 | 579 |
579 }) | 580 }) |
580 } | 581 } |
| 582 |
| 583 func TestCompoundIndexes(t *testing.T) { |
| 584 t.Parallel() |
| 585 |
| 586 idxKey := func(def *dsS.IndexDefinition) string { |
| 587 buf := &bytes.Buffer{} |
| 588 buf.WriteString("idx::") |
| 589 So(def.Write(buf), ShouldBeNil) |
| 590 return buf.String() |
| 591 } |
| 592 |
| 593 numItms := func(c *memCollection) uint64 { |
| 594 ret, _ := c.GetTotals() |
| 595 return ret |
| 596 } |
| 597 |
| 598 Convey("Test Compound indexes", t, func() { |
| 599 type Model struct { |
| 600 ID int64 `gae:"$id"` |
| 601 |
| 602 Field1 []string |
| 603 Field2 []int64 |
| 604 } |
| 605 |
| 606 c := Use(context.Background()) |
| 607 ds := dsS.Get(c) |
| 608 t := ds.Raw().Testable().(*dsImpl) |
| 609 store := t.data.store |
| 610 |
| 611 So(ds.Put(&Model{1, []string{"hello", "world"}, []int64{10, 11}}
), ShouldBeNil) |
| 612 |
| 613 idx := &dsS.IndexDefinition{ |
| 614 Kind: "Model", |
| 615 SortBy: []dsS.IndexColumn{ |
| 616 {Property: "Field2"}, |
| 617 }, |
| 618 } |
| 619 |
| 620 coll := store.GetCollection(idxKey(idx)) |
| 621 So(coll, ShouldNotBeNil) |
| 622 So(numItms(coll), ShouldEqual, 2) |
| 623 |
| 624 idx.SortBy[0].Property = "Field1" |
| 625 coll = store.GetCollection(idxKey(idx)) |
| 626 So(coll, ShouldNotBeNil) |
| 627 So(numItms(coll), ShouldEqual, 2) |
| 628 |
| 629 idx.SortBy = append(idx.SortBy, dsS.IndexColumn{Property: "Field
1"}) |
| 630 So(store.GetCollection(idxKey(idx)), ShouldBeNil) |
| 631 |
| 632 t.AddIndexes(idx) |
| 633 coll = store.GetCollection(idxKey(idx)) |
| 634 So(coll, ShouldNotBeNil) |
| 635 So(numItms(coll), ShouldEqual, 4) |
| 636 }) |
| 637 } |
OLD | NEW |