| 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" |
| (...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 So(ds.Put(&Foo{}), ShouldErrLike, "allocateIDs is disabl
ed") | 523 So(ds.Put(&Foo{}), ShouldErrLike, "allocateIDs is disabl
ed") |
| 524 | 524 |
| 525 So(ds.Put(&Foo{ID: 1}), ShouldBeNil) | 525 So(ds.Put(&Foo{ID: 1}), ShouldBeNil) |
| 526 | 526 |
| 527 ds.Testable().CatchupIndexes() | 527 ds.Testable().CatchupIndexes() |
| 528 | 528 |
| 529 count, err := ds.Count(dsS.NewQuery("")) | 529 count, err := ds.Count(dsS.NewQuery("")) |
| 530 So(err, ShouldBeNil) | 530 So(err, ShouldBeNil) |
| 531 So(count, ShouldEqual, 1) // normally this would include
__entity_group__ | 531 So(count, ShouldEqual, 1) // normally this would include
__entity_group__ |
| 532 }) | 532 }) |
| 533 |
| 534 Convey("Datastore namespace interaction", func() { |
| 535 run := func(rc context.Context, txn bool) (putErr, getEr
r, queryErr, countErr error) { |
| 536 var foo Foo |
| 537 |
| 538 putFunc := func(doC context.Context) error { |
| 539 return dsS.Get(doC).Put(&foo) |
| 540 } |
| 541 |
| 542 doFunc := func(doC context.Context) { |
| 543 ds := dsS.Get(doC) |
| 544 getErr = ds.Get(&foo) |
| 545 |
| 546 q := dsS.NewQuery("Foo").Ancestor(ds.Key
ForObj(&foo)) |
| 547 queryErr = ds.Run(q, func(f *Foo) error
{ return nil }) |
| 548 _, countErr = ds.Count(q) |
| 549 } |
| 550 |
| 551 if txn { |
| 552 putErr = dsS.Get(rc).RunInTransaction(fu
nc(ic context.Context) error { |
| 553 return putFunc(ic) |
| 554 }, nil) |
| 555 if putErr != nil { |
| 556 return |
| 557 } |
| 558 |
| 559 dsS.Get(rc).Testable().CatchupIndexes() |
| 560 dsS.Get(rc).RunInTransaction(func(ic con
text.Context) error { |
| 561 doFunc(ic) |
| 562 return nil |
| 563 }, nil) |
| 564 } else { |
| 565 putErr = putFunc(rc) |
| 566 if putErr != nil { |
| 567 return |
| 568 } |
| 569 dsS.Get(rc).Testable().CatchupIndexes() |
| 570 doFunc(rc) |
| 571 } |
| 572 return |
| 573 } |
| 574 |
| 575 for _, txn := range []bool{false, true} { |
| 576 Convey(fmt.Sprintf("In transaction? %v", txn), f
unc() { |
| 577 Convey("With no namespace installed, can
Put, Get, Query, and Count.", func() { |
| 578 _, has := infoS.Get(c).GetNamesp
ace() |
| 579 So(has, ShouldBeFalse) |
| 580 |
| 581 putErr, getErr, queryErr, countE
rr := run(c, txn) |
| 582 So(putErr, ShouldBeNil) |
| 583 So(getErr, ShouldBeNil) |
| 584 So(queryErr, ShouldBeNil) |
| 585 So(countErr, ShouldBeNil) |
| 586 }) |
| 587 |
| 588 Convey("With an non-empty namespace inst
alled, can Put, Get, Query, and Count.", func() { |
| 589 putErr, getErr, queryErr, countE
rr := run(infoS.Get(c).MustNamespace("foo"), txn) |
| 590 So(putErr, ShouldBeNil) |
| 591 So(getErr, ShouldBeNil) |
| 592 So(queryErr, ShouldBeNil) |
| 593 So(countErr, ShouldBeNil) |
| 594 }) |
| 595 |
| 596 Convey("With an empty namespace installe
d, can Put and Get, but not Query or Count.", func() { |
| 597 putErr, getErr, queryErr, countE
rr := run(infoS.Get(c).MustNamespace(""), txn) |
| 598 So(putErr, ShouldBeNil) |
| 599 So(getErr, ShouldBeNil) |
| 600 So(queryErr, ShouldErrLike, "nam
espace may not be present and empty") |
| 601 So(countErr, ShouldErrLike, "nam
espace may not be present and empty") |
| 602 }) |
| 603 }) |
| 604 } |
| 605 }) |
| 533 }) | 606 }) |
| 534 } | 607 } |
| 535 | 608 |
| 536 func TestCompoundIndexes(t *testing.T) { | 609 func TestCompoundIndexes(t *testing.T) { |
| 537 t.Parallel() | 610 t.Parallel() |
| 538 | 611 |
| 539 idxKey := func(def dsS.IndexDefinition) string { | 612 idxKey := func(def dsS.IndexDefinition) string { |
| 540 So(def, ShouldNotBeNil) | 613 So(def, ShouldNotBeNil) |
| 541 return "idx::" + string(serialize.ToBytes(*def.PrepForIdxTable()
)) | 614 return "idx::" + string(serialize.ToBytes(*def.PrepForIdxTable()
)) |
| 542 } | 615 } |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 dsS.Get(ctx).Testable().AddIndexes(&dsS.IndexDefinition{ | 735 dsS.Get(ctx).Testable().AddIndexes(&dsS.IndexDefinition{ |
| 663 Kind: "Foo", | 736 Kind: "Foo", |
| 664 SortBy: []dsS.IndexColumn{ | 737 SortBy: []dsS.IndexColumn{ |
| 665 {Property: "Val"}, | 738 {Property: "Val"}, |
| 666 {Property: "Name"}, | 739 {Property: "Name"}, |
| 667 }, | 740 }, |
| 668 }) | 741 }) |
| 669 dsS.Get(ctx).Testable().CatchupIndexes() | 742 dsS.Get(ctx).Testable().CatchupIndexes() |
| 670 | 743 |
| 671 for _, ns := range namespaces { | 744 for _, ns := range namespaces { |
| 745 if ns == "" { |
| 746 // Skip query test for empty namespace,
as this is invalid. |
| 747 continue |
| 748 } |
| 749 |
| 672 results = nil | 750 results = nil |
| 673 So(dsS.Get(infoS.Get(ctx).MustNamespace(ns)).Get
All(q, &results), ShouldBeNil) | 751 So(dsS.Get(infoS.Get(ctx).MustNamespace(ns)).Get
All(q, &results), ShouldBeNil) |
| 674 So(len(results), ShouldEqual, 2) | 752 So(len(results), ShouldEqual, 2) |
| 675 } | 753 } |
| 676 | 754 |
| 677 » » » // Add "foos" to a new namesapce, then confirm that it g
ets indexed. | 755 » » » // Add "foos" to a new namespace, then confirm that it g
ets indexed. |
| 678 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).PutMulti
(foos), ShouldBeNil) | 756 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).PutMulti
(foos), ShouldBeNil) |
| 679 dsS.Get(ctx).Testable().CatchupIndexes() | 757 dsS.Get(ctx).Testable().CatchupIndexes() |
| 680 | 758 |
| 681 results = nil | 759 results = nil |
| 682 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).GetAll(q
, &results), ShouldBeNil) | 760 So(dsS.Get(infoS.Get(ctx).MustNamespace("qux")).GetAll(q
, &results), ShouldBeNil) |
| 683 So(len(results), ShouldEqual, 2) | 761 So(len(results), ShouldEqual, 2) |
| 684 }) | 762 }) |
| 685 }) | 763 }) |
| 686 } | 764 } |
| OLD | NEW |