go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/datastore/index_test.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // adapted from github.com/golang/appengine/datastore 16 17 package datastore 18 19 import ( 20 "fmt" 21 "strings" 22 "testing" 23 24 "gopkg.in/yaml.v2" 25 26 . "github.com/smartystreets/goconvey/convey" 27 ) 28 29 var indexDefinitionTests = []struct { 30 id *IndexDefinition 31 builtin bool 32 compound bool 33 str string 34 yaml []string 35 }{ 36 { 37 id: &IndexDefinition{Kind: "kind"}, 38 builtin: true, 39 str: "B:kind", 40 }, 41 42 { 43 id: &IndexDefinition{ 44 Kind: "kind", 45 SortBy: []IndexColumn{{Property: ""}}, 46 }, 47 builtin: true, 48 str: "B:kind/", 49 }, 50 51 { 52 id: &IndexDefinition{ 53 Kind: "kind", 54 SortBy: []IndexColumn{{Property: "prop"}}, 55 }, 56 builtin: true, 57 str: "B:kind/prop", 58 }, 59 60 { 61 id: &IndexDefinition{ 62 Kind: "Kind", 63 Ancestor: true, 64 SortBy: []IndexColumn{ 65 {Property: "prop"}, 66 {Property: "other", Descending: true}, 67 }, 68 }, 69 compound: true, 70 str: "C:Kind|A/prop/-other", 71 yaml: []string{ 72 "- kind: Kind", 73 " ancestor: yes", 74 " properties:", 75 " - name: prop", 76 " - name: other", 77 " direction: desc", 78 }, 79 }, 80 } 81 82 func TestIndexDefinition(t *testing.T) { 83 t.Parallel() 84 85 Convey("Test IndexDefinition", t, func() { 86 for _, tc := range indexDefinitionTests { 87 Convey(tc.str, func() { 88 So(tc.id.String(), ShouldEqual, tc.str) 89 So(tc.id.Builtin(), ShouldEqual, tc.builtin) 90 So(tc.id.Compound(), ShouldEqual, tc.compound) 91 yaml, _ := tc.id.YAMLString() 92 So(yaml, ShouldEqual, strings.Join(tc.yaml, "\n")) 93 }) 94 } 95 }) 96 97 Convey("Test MarshalYAML/UnmarshalYAML", t, func() { 98 for _, tc := range indexDefinitionTests { 99 Convey(fmt.Sprintf("marshallable index definition `%s` is marshalled and unmarshalled correctly", tc.str), func() { 100 if tc.yaml != nil { 101 // marshal 102 _, err := yaml.Marshal(tc.id) 103 So(err, ShouldBeNil) 104 105 // unmarshal 106 var ids []*IndexDefinition 107 yaml.Unmarshal([]byte(strings.Join(tc.yaml, "\n")), &ids) 108 So(ids[0], ShouldResemble, tc.id) 109 } 110 }) 111 } 112 }) 113 }