OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // adapted from github.com/golang/appengine/datastore |
| 6 |
| 7 package datastore |
| 8 |
| 9 import ( |
| 10 "bytes" |
| 11 "testing" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 func TestIndexDefinition(t *testing.T) { |
| 17 t.Parallel() |
| 18 |
| 19 Convey("Test IndexDefinition", t, func() { |
| 20 Convey("basic", func() { |
| 21 id := IndexDefinition{Kind: "kind"} |
| 22 |
| 23 So(id.Builtin(), ShouldBeTrue) |
| 24 So(id.Compound(), ShouldBeFalse) |
| 25 So(id.String(), ShouldEqual, "B:kind") |
| 26 |
| 27 id.SortBy = append(id.SortBy, IndexColumn{Property: "pro
p"}) |
| 28 So(id.SortBy[0].Direction, ShouldEqual, ASCENDING) |
| 29 So(id.Builtin(), ShouldBeTrue) |
| 30 So(id.Compound(), ShouldBeFalse) |
| 31 So(id.String(), ShouldEqual, "B:kind/prop") |
| 32 |
| 33 id.SortBy = append(id.SortBy, IndexColumn{"other", DESCE
NDING}) |
| 34 id.Ancestor = true |
| 35 So(id.Builtin(), ShouldBeFalse) |
| 36 So(id.Compound(), ShouldBeTrue) |
| 37 So(id.String(), ShouldEqual, "C:kind|A/prop/-other") |
| 38 |
| 39 // invalid |
| 40 id.SortBy = append(id.SortBy, IndexColumn{"", DESCENDING
}) |
| 41 So(id.Builtin(), ShouldBeFalse) |
| 42 So(id.Compound(), ShouldBeFalse) |
| 43 }) |
| 44 |
| 45 Convey("binary", func() { |
| 46 id := IndexDefinition{Kind: "kind"} |
| 47 buf := &bytes.Buffer{} |
| 48 So(id.Write(buf), ShouldBeNil) |
| 49 So(bytes.HasPrefix(buf.Bytes(), IndexBuiltinQueryPrefix(
)), ShouldBeTrue) |
| 50 newId := IndexDefinition{} |
| 51 So(newId.Read(buf), ShouldBeNil) |
| 52 So(newId, ShouldResemble, id) |
| 53 |
| 54 id.SortBy = append(id.SortBy, IndexColumn{Property: "pro
p"}) |
| 55 buf = &bytes.Buffer{} |
| 56 So(id.Write(buf), ShouldBeNil) |
| 57 So(bytes.HasPrefix(buf.Bytes(), IndexBuiltinQueryPrefix(
)), ShouldBeTrue) |
| 58 newId = IndexDefinition{} |
| 59 So(newId.Read(buf), ShouldBeNil) |
| 60 So(newId, ShouldResemble, id) |
| 61 |
| 62 id.SortBy = append(id.SortBy, IndexColumn{"other", DESCE
NDING}) |
| 63 id.Ancestor = true |
| 64 buf = &bytes.Buffer{} |
| 65 So(id.Write(buf), ShouldBeNil) |
| 66 So(bytes.HasPrefix(buf.Bytes(), IndexComplexQueryPrefix(
)), ShouldBeTrue) |
| 67 newId = IndexDefinition{} |
| 68 So(newId.Read(buf), ShouldBeNil) |
| 69 So(newId, ShouldResemble, id) |
| 70 |
| 71 // invalid |
| 72 id.SortBy = append(id.SortBy, IndexColumn{"", DESCENDING
}) |
| 73 buf = &bytes.Buffer{} |
| 74 So(id.Write(buf), ShouldBeNil) |
| 75 So(bytes.HasPrefix(buf.Bytes(), IndexComplexQueryPrefix(
)), ShouldBeTrue) |
| 76 newId = IndexDefinition{} |
| 77 So(newId.Read(buf), ShouldBeNil) |
| 78 So(newId, ShouldResemble, id) |
| 79 }) |
| 80 |
| 81 Convey("too many", func() { |
| 82 id := IndexDefinition{Kind: "wat"} |
| 83 for i := 0; i < MaxIndexColumns+1; i++ { |
| 84 id.SortBy = append(id.SortBy, IndexColumn{"Hi",
ASCENDING}) |
| 85 } |
| 86 buf := &bytes.Buffer{} |
| 87 So(id.Write(buf), ShouldBeNil) |
| 88 newId := IndexDefinition{} |
| 89 So(newId.Read(buf).Error(), ShouldContainSubstring, "ove
r 64 sort orders") |
| 90 }) |
| 91 }) |
| 92 } |
OLD | NEW |