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 // adapted from github.com/golang/appengine/datastore | 5 // adapted from github.com/golang/appengine/datastore |
6 | 6 |
7 package datastore | 7 package datastore |
8 | 8 |
9 import ( | 9 import ( |
| 10 "strings" |
10 "testing" | 11 "testing" |
11 | 12 |
12 . "github.com/smartystreets/goconvey/convey" | 13 . "github.com/smartystreets/goconvey/convey" |
13 ) | 14 ) |
14 | 15 |
| 16 var indexDefinitionTests = []struct { |
| 17 id *IndexDefinition |
| 18 builtin bool |
| 19 compound bool |
| 20 str string |
| 21 yaml []string |
| 22 }{ |
| 23 { |
| 24 id: &IndexDefinition{Kind: "kind"}, |
| 25 builtin: true, |
| 26 str: "B:kind", |
| 27 }, |
| 28 |
| 29 { |
| 30 id: &IndexDefinition{ |
| 31 Kind: "kind", |
| 32 SortBy: []IndexColumn{{Property: ""}}, |
| 33 }, |
| 34 builtin: true, |
| 35 str: "B:kind/", |
| 36 }, |
| 37 |
| 38 { |
| 39 id: &IndexDefinition{ |
| 40 Kind: "kind", |
| 41 SortBy: []IndexColumn{{Property: "prop"}}, |
| 42 }, |
| 43 builtin: true, |
| 44 str: "B:kind/prop", |
| 45 }, |
| 46 |
| 47 { |
| 48 id: &IndexDefinition{ |
| 49 Kind: "Kind", |
| 50 Ancestor: true, |
| 51 SortBy: []IndexColumn{ |
| 52 {Property: "prop"}, |
| 53 {Property: "other", Descending: true}, |
| 54 }, |
| 55 }, |
| 56 compound: true, |
| 57 str: "C:Kind|A/prop/-other", |
| 58 yaml: []string{ |
| 59 "- kind: Kind", |
| 60 " ancestor: yes", |
| 61 " properties:", |
| 62 " - name: prop", |
| 63 " - name: other", |
| 64 " direction: desc", |
| 65 }, |
| 66 }, |
| 67 } |
| 68 |
15 func TestIndexDefinition(t *testing.T) { | 69 func TestIndexDefinition(t *testing.T) { |
16 t.Parallel() | 70 t.Parallel() |
17 | 71 |
18 Convey("Test IndexDefinition", t, func() { | 72 Convey("Test IndexDefinition", t, func() { |
19 » » Convey("basic", func() { | 73 » » for _, tc := range indexDefinitionTests { |
20 » » » id := IndexDefinition{Kind: "kind"} | 74 » » » Convey(tc.str, func() { |
21 | 75 » » » » So(tc.id.String(), ShouldEqual, tc.str) |
22 » » » So(id.Builtin(), ShouldBeTrue) | 76 » » » » So(tc.id.Builtin(), ShouldEqual, tc.builtin) |
23 » » » So(id.Compound(), ShouldBeFalse) | 77 » » » » So(tc.id.Compound(), ShouldEqual, tc.compound) |
24 » » » So(id.String(), ShouldEqual, "B:kind") | 78 » » » » yaml, _ := tc.id.YAMLString() |
25 | 79 » » » » So(yaml, ShouldEqual, strings.Join(tc.yaml, "\n"
)) |
26 » » » id.SortBy = append(id.SortBy, IndexColumn{Property: "pro
p"}) | 80 » » » }) |
27 » » » So(id.SortBy[0].Descending, ShouldBeFalse) | 81 » » } |
28 » » » So(id.Builtin(), ShouldBeTrue) | |
29 » » » So(id.Compound(), ShouldBeFalse) | |
30 » » » So(id.String(), ShouldEqual, "B:kind/prop") | |
31 | |
32 » » » id.SortBy = append(id.SortBy, IndexColumn{Property: "oth
er", Descending: true}) | |
33 » » » id.Ancestor = true | |
34 » » » So(id.Builtin(), ShouldBeFalse) | |
35 » » » So(id.Compound(), ShouldBeTrue) | |
36 » » » So(id.String(), ShouldEqual, "C:kind|A/prop/-other") | |
37 | |
38 » » » // invalid | |
39 » » » id.SortBy = append(id.SortBy, IndexColumn{Property: "",
Descending: true}) | |
40 » » » So(id.Builtin(), ShouldBeFalse) | |
41 » » » So(id.Compound(), ShouldBeFalse) | |
42 » » }) | |
43 }) | 82 }) |
44 } | 83 } |
OLD | NEW |