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

Side by Side Diff: service/datastore/key_test.go

Issue 1355783002: Refactor keys and queries in datastore service and implementation. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: appease errcheck Created 5 years, 3 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 unified diff | Download patch
« no previous file with comments | « service/datastore/key.go ('k') | service/datastore/multiarg.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 package datastore
6
7 import (
8 "encoding/json"
9 "fmt"
10 "testing"
11
12 . "github.com/luci/luci-go/common/testing/assertions"
13 . "github.com/smartystreets/goconvey/convey"
14 )
15
16 func ShouldEqualKey(actual interface{}, expected ...interface{}) string {
17 if len(expected) != 1 {
18 return fmt.Sprintf("Assertion requires 1 expected value, got %d" , len(expected))
19 }
20 if actual.(*Key).Equal(expected[0].(*Key)) {
21 return ""
22 }
23 return fmt.Sprintf("Expected: %q\nActual: %q", actual, expected[0])
24 }
25
26 func TestKeyEncode(t *testing.T) {
27 t.Parallel()
28
29 keys := []*Key{
30 MakeKey("appid", "ns", "kind", 1),
31 MakeKey("appid", "ns", "nerd", "moo"),
32 MakeKey("appid", "ns", "parent", 10, "renerd", "moo"),
33 }
34
35 Convey("Key Round trip", t, func() {
36 for _, k := range keys {
37 k := k
38 Convey(k.String(), func() {
39 enc := k.Encode()
40 dec, err := NewKeyEncoded(enc)
41 So(err, ShouldBeNil)
42 So(dec, ShouldNotBeNil)
43 So(dec, ShouldEqualKey, k)
44
45 dec2, err := NewKeyEncoded(enc)
46 So(err, ShouldBeNil)
47 So(dec2, ShouldEqualKey, dec)
48 So(dec2, ShouldEqualKey, k)
49 })
50
51 Convey(k.String()+" (json)", func() {
52 data, err := k.MarshalJSON()
53 So(err, ShouldBeNil)
54
55 dec := &Key{}
56 So(dec.UnmarshalJSON(data), ShouldBeNil)
57 So(dec, ShouldEqualKey, k)
58 })
59 }
60 })
61
62 Convey("NewKey", t, func() {
63 Convey("single", func() {
64 k := NewKey("appid", "ns", "kind", "", 1, nil)
65 So(k, ShouldEqualKey, keys[0])
66 })
67
68 Convey("empty", func() {
69 So(NewKeyToks("appid", "ns", nil), ShouldBeNil)
70 })
71
72 Convey("nest", func() {
73 k := NewKey("appid", "ns", "renerd", "moo", 0,
74 NewKey("appid", "ns", "parent", "", 10, nil))
75 So(k, ShouldEqualKey, keys[2])
76 })
77 })
78
79 Convey("Key bad encoding", t, func() {
80 Convey("extra junk before", func() {
81 enc := keys[2].Encode()
82 _, err := NewKeyEncoded("/" + enc)
83 So(err, ShouldErrLike, "illegal base64")
84 })
85
86 Convey("extra junk after", func() {
87 enc := keys[2].Encode()
88 _, err := NewKeyEncoded(enc[:len(enc)-1])
89 So(err, ShouldErrLike, "EOF")
90 })
91
92 Convey("json encoding includes quotes", func() {
93 data, err := keys[0].MarshalJSON()
94 So(err, ShouldBeNil)
95
96 dec := &Key{}
97 err = dec.UnmarshalJSON(append(data, '!'))
98 So(err, ShouldErrLike, "bad JSON key")
99 })
100 })
101 }
102
103 func TestKeyValidity(t *testing.T) {
104 //t.Parallel()
105
106 Convey("keys validity", t, func() {
107 Convey("incomplete", func() {
108 So(MakeKey("aid", "ns", "kind", 1).Incomplete(), ShouldB eFalse)
109 So(MakeKey("aid", "ns", "kind", 0).Incomplete(), ShouldB eTrue)
110 })
111
112 Convey("invalid", func() {
113 So(MakeKey("aid", "ns", "hat", "face", "__kind__", 1).Va lid(true, "aid", "ns"), ShouldBeTrue)
114
115 bads := []*Key{
116 NewKeyToks("aid", "ns", []KeyTok{{"Kind", 1, "1" }}),
117 MakeKey("", "ns", "hat", "face"),
118 MakeKey("aid", "ns", "base", 1, "", "id"),
119 MakeKey("aid", "ns", "hat", "face", "__kind__", 1),
120 MakeKey("aid", "ns", "hat", 0, "kind", 1),
121 }
122 for _, k := range bads {
123 Convey(k.String(), func() {
124 So(k.Valid(false, "aid", "ns"), ShouldBe False)
125 })
126 }
127 })
128
129 Convey("partially valid", func() {
130 So(MakeKey("aid", "ns", "kind", "").PartialValid("aid", "ns"), ShouldBeTrue)
131 So(MakeKey("aid", "ns", "kind", "", "child", "").Partial Valid("aid", "ns"), ShouldBeFalse)
132 })
133 })
134 }
135
136 func TestMiscKey(t *testing.T) {
137 t.Parallel()
138
139 Convey("KeyRoot", t, func() {
140 k := MakeKey("appid", "ns", "parent", 10, "renerd", "moo")
141 r := MakeKey("appid", "ns", "parent", 10)
142 So(k.Root(), ShouldEqualKey, r)
143 })
144
145 Convey("KeysEqual", t, func() {
146 k1 := MakeKey("a", "n", "knd", 1)
147 k2 := MakeKey("a", "n", "knd", 1)
148 So(k1.Equal(k2), ShouldBeTrue)
149 k3 := MakeKey("a", "n", "knd", 2)
150 So(k1.Equal(k3), ShouldBeFalse)
151 })
152
153 Convey("KeyString", t, func() {
154 k1 := MakeKey("a", "n", "knd", 1, "other", "wat")
155 So(k1.String(), ShouldEqual, "a:n:/knd,1/other,\"wat\"")
156 })
157
158 Convey("*GenericKey supports json encoding", t, func() {
159 type TestStruct struct {
160 Key *Key
161 }
162 t := &TestStruct{
163 NewKey("aid", "ns", "kind", "id", 0,
164 NewKey("aid", "ns", "parent", "", 1, nil),
165 )}
166 d, err := json.Marshal(t)
167 So(err, ShouldBeNil)
168 t2 := &TestStruct{}
169 err = json.Unmarshal(d, t2)
170 So(err, ShouldBeNil)
171 So(t.Key, ShouldEqualKey, t2.Key)
172 })
173 }
174
175 func shouldBeLess(actual interface{}, expected ...interface{}) string {
176 a, b := actual.(*Key), expected[0].(*Key)
177 if a.Less(b) {
178 return ""
179 }
180 return fmt.Sprintf("Expected %q < %q", a.String(), b.String())
181 }
182
183 func shouldNotBeEqual(actual interface{}, expected ...interface{}) string {
184 a, b := actual.(*Key), expected[0].(*Key)
185 if !a.Equal(b) {
186 return ""
187 }
188 return fmt.Sprintf("Expected %q != %q", a.String(), b.String())
189 }
190
191 func shouldNotBeLess(actual interface{}, expected ...interface{}) string {
192 a, b := actual.(*Key), expected[0].(*Key)
193 if !a.Less(b) {
194 return ""
195 }
196 return fmt.Sprintf("Expected !(%q < %q)", a.String(), b.String())
197 }
198
199 func TestKeySort(t *testing.T) {
200 t.Parallel()
201
202 Convey("KeyTok.Less() works", t, func() {
203 So((KeyTok{"a", 0, "1"}).Less(KeyTok{"b", 0, "2"}), ShouldBeTrue )
204 So((KeyTok{"b", 0, "1"}).Less(KeyTok{"a", 0, "2"}), ShouldBeFals e)
205 So((KeyTok{"kind", 0, "1"}).Less(KeyTok{"kind", 0, "2"}), Should BeTrue)
206 So((KeyTok{"kind", 1, ""}).Less(KeyTok{"kind", 2, ""}), ShouldBe True)
207 So((KeyTok{"kind", 1, ""}).Less(KeyTok{"kind", 0, "1"}), ShouldB eTrue)
208 })
209
210 Convey("Key comparison works", t, func() {
211 s := []*Key{
212 MakeKey("A", "", "kind", 1),
213 MakeKey("A", "n", "kind", 1),
214 MakeKey("A", "n", "kind", 1, "something", "else"),
215 MakeKey("A", "n", "kind", "1"),
216 MakeKey("A", "n", "kind", "1", "something", "else"),
217 MakeKey("A", "n", "other", 1, "something", "else"),
218 MakeKey("a", "", "kind", 1),
219 MakeKey("a", "n", "kind", 1),
220 MakeKey("a", "n", "kind", 2),
221 MakeKey("a", "p", "aleph", 1),
222 MakeKey("b", "n", "kind", 2),
223 }
224
225 for i := 1; i < len(s); i++ {
226 So(s[i-1], shouldBeLess, s[i])
227 So(s[i-1], shouldNotBeEqual, s[i])
228 So(s[i], shouldNotBeEqual, s[i-1])
229 So(s[i], shouldNotBeLess, s[i-1])
230 }
231 })
232 }
OLDNEW
« no previous file with comments | « service/datastore/key.go ('k') | service/datastore/multiarg.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698