Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2368)

Unified Diff: service/datastore/index_test.go

Issue 1285703002: Add testable interface for datastore. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: rebase Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: service/datastore/index_test.go
diff --git a/service/datastore/index_test.go b/service/datastore/index_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..2667534c791f2401fe334f5ba0427824d042c36e
--- /dev/null
+++ b/service/datastore/index_test.go
@@ -0,0 +1,92 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// adapted from github.com/golang/appengine/datastore
+
+package datastore
+
+import (
+ "bytes"
+ "testing"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestIndexDefinition(t *testing.T) {
+ t.Parallel()
+
+ Convey("Test IndexDefinition", t, func() {
+ Convey("basic", func() {
+ id := IndexDefinition{Kind: "kind"}
+
+ So(id.Builtin(), ShouldBeTrue)
+ So(id.Compound(), ShouldBeFalse)
+ So(id.String(), ShouldEqual, "B:kind")
+
+ id.SortBy = append(id.SortBy, IndexColumn{Property: "prop"})
+ So(id.SortBy[0].Direction, ShouldEqual, ASCENDING)
+ So(id.Builtin(), ShouldBeTrue)
+ So(id.Compound(), ShouldBeFalse)
+ So(id.String(), ShouldEqual, "B:kind/prop")
+
+ id.SortBy = append(id.SortBy, IndexColumn{"other", DESCENDING})
+ id.Ancestor = true
+ So(id.Builtin(), ShouldBeFalse)
+ So(id.Compound(), ShouldBeTrue)
+ So(id.String(), ShouldEqual, "C:kind|A/prop/-other")
+
+ // invalid
+ id.SortBy = append(id.SortBy, IndexColumn{"", DESCENDING})
+ So(id.Builtin(), ShouldBeFalse)
+ So(id.Compound(), ShouldBeFalse)
+ })
+
+ Convey("binary", func() {
+ id := IndexDefinition{Kind: "kind"}
+ buf := &bytes.Buffer{}
+ So(id.Write(buf), ShouldBeNil)
+ So(bytes.HasPrefix(buf.Bytes(), IndexBuiltinQueryPrefix()), ShouldBeTrue)
+ newId := IndexDefinition{}
+ So(newId.Read(buf), ShouldBeNil)
+ So(newId, ShouldResemble, id)
+
+ id.SortBy = append(id.SortBy, IndexColumn{Property: "prop"})
+ buf = &bytes.Buffer{}
+ So(id.Write(buf), ShouldBeNil)
+ So(bytes.HasPrefix(buf.Bytes(), IndexBuiltinQueryPrefix()), ShouldBeTrue)
+ newId = IndexDefinition{}
+ So(newId.Read(buf), ShouldBeNil)
+ So(newId, ShouldResemble, id)
+
+ id.SortBy = append(id.SortBy, IndexColumn{"other", DESCENDING})
+ id.Ancestor = true
+ buf = &bytes.Buffer{}
+ So(id.Write(buf), ShouldBeNil)
+ So(bytes.HasPrefix(buf.Bytes(), IndexComplexQueryPrefix()), ShouldBeTrue)
+ newId = IndexDefinition{}
+ So(newId.Read(buf), ShouldBeNil)
+ So(newId, ShouldResemble, id)
+
+ // invalid
+ id.SortBy = append(id.SortBy, IndexColumn{"", DESCENDING})
+ buf = &bytes.Buffer{}
+ So(id.Write(buf), ShouldBeNil)
+ So(bytes.HasPrefix(buf.Bytes(), IndexComplexQueryPrefix()), ShouldBeTrue)
+ newId = IndexDefinition{}
+ So(newId.Read(buf), ShouldBeNil)
+ So(newId, ShouldResemble, id)
+ })
+
+ Convey("too many", func() {
+ id := IndexDefinition{Kind: "wat"}
+ for i := 0; i < MaxIndexColumns+1; i++ {
+ id.SortBy = append(id.SortBy, IndexColumn{"Hi", ASCENDING})
+ }
+ buf := &bytes.Buffer{}
+ So(id.Write(buf), ShouldBeNil)
+ newId := IndexDefinition{}
+ So(newId.Read(buf).Error(), ShouldContainSubstring, "over 64 sort orders")
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698