Chromium Code Reviews| 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 package datastore | 5 package datastore |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "math" | 8 "math" |
| 9 "testing" | 9 "testing" |
| 10 | 10 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 {"projecting an empty field", | 118 {"projecting an empty field", |
| 119 nq().Project("hello", ""), | 119 nq().Project("hello", ""), |
| 120 "", | 120 "", |
| 121 "cannot filter/project on: \"\"", nil}, | 121 "cannot filter/project on: \"\"", nil}, |
| 122 | 122 |
| 123 {"projecting __key__", | 123 {"projecting __key__", |
| 124 nq().Project("hello", "__key__"), | 124 nq().Project("hello", "__key__"), |
| 125 "", | 125 "", |
| 126 "cannot project on \"__key__\"", nil}, | 126 "cannot project on \"__key__\"", nil}, |
| 127 | 127 |
| 128 {"getting all the keys", | |
| 129 nq("").KeysOnly(true), | |
| 130 "SELECT __key__ ORDER BY `__key__`", | |
| 131 nil, nil}, | |
| 132 | |
| 128 {"projecting a duplicate", | 133 {"projecting a duplicate", |
| 129 nq().Project("hello", "hello"), | 134 nq().Project("hello", "hello"), |
| 130 "SELECT `hello` FROM `Foo` ORDER BY `hello`, `__key__`", | 135 "SELECT `hello` FROM `Foo` ORDER BY `hello`, `__key__`", |
| 131 nil, nq().Project("hello")}, | 136 nil, nq().Project("hello")}, |
| 132 | 137 |
| 133 {"projecting a duplicate (style 2)", | 138 {"projecting a duplicate (style 2)", |
| 134 nq().Project("hello").Project("hello"), | 139 nq().Project("hello").Project("hello"), |
| 135 "SELECT `hello` FROM `Foo` ORDER BY `hello`, `__key__`", | 140 "SELECT `hello` FROM `Foo` ORDER BY `hello`, `__key__`", |
| 136 nil, nq().Project("hello")}, | 141 nil, nq().Project("hello")}, |
| 137 | 142 |
| 143 {"project distinct", | |
| 144 nq().Project("hello").Distinct(true), | |
| 145 "SELECT DISTINCT `hello` FROM `Foo` ORDER BY `hello`, `__key__`" , | |
| 146 nil, nil}, | |
| 147 | |
| 138 {"bad ancestors", | 148 {"bad ancestors", |
| 139 nq().Ancestor(mkKey("goop", 0)), | 149 nq().Ancestor(mkKey("goop", 0)), |
| 140 "", | 150 "", |
| 141 ErrInvalidKey, nil}, | 151 ErrInvalidKey, nil}, |
| 142 | 152 |
| 143 {"nil ancestors", | 153 {"nil ancestors", |
| 144 nq().Ancestor(nil), | 154 nq().Ancestor(nil), |
| 145 "SELECT * FROM `Foo` ORDER BY `__key__`", | 155 "SELECT * FROM `Foo` ORDER BY `__key__`", |
| 146 nil, nq()}, | 156 nil, nq()}, |
| 147 | 157 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 {"kindless with non-__key__ orders", | 198 {"kindless with non-__key__ orders", |
| 189 nq("").Order("face"), | 199 nq("").Order("face"), |
| 190 "", | 200 "", |
| 191 "invalid order for kindless query", nil}, | 201 "invalid order for kindless query", nil}, |
| 192 | 202 |
| 193 {"kindless with descending-__key__ order", | 203 {"kindless with descending-__key__ order", |
| 194 nq("").Order("-__key__"), | 204 nq("").Order("-__key__"), |
| 195 "", | 205 "", |
| 196 "invalid order for kindless query", nil}, | 206 "invalid order for kindless query", nil}, |
| 197 | 207 |
| 208 {"kindless with equality filters", | |
| 209 nq("").Eq("hello", 1), | |
| 210 "", | |
| 211 "may not have any equality", nil}, | |
| 212 | |
| 213 {"kindless with ancestor filter", | |
| 214 nq("").Ancestor(mkKey("Parent", 1)), | |
| 215 "SELECT * WHERE __key__ HAS ANCESTOR KEY(DATASET(\"s~aid\"), NAM ESPACE(\"ns\"), \"Parent\", 1) ORDER BY `__key__`", | |
| 216 nil, nil}, | |
| 217 | |
| 218 {"kindless with ancestor filter and __key__ ineq", | |
| 219 nq("").Ancestor(mkKey("Parent", 1)).Lt("__key__", mkKey("Parent" , 1, "Sub", "hat")), | |
| 220 "SELECT * WHERE `__key__` < KEY(DATASET(\"s~aid\"), NAMESPACE(\" ns\"), \"Parent\", 1, \"Sub\", \"hat\") AND __key__ HAS ANCESTOR KEY(DATASET(\"s ~aid\"), NAMESPACE(\"ns\"), \"Parent\", 1) ORDER BY `__key__`", | |
| 221 nil, nil}, | |
| 222 | |
| 198 {"distinct non-projection", | 223 {"distinct non-projection", |
| 199 nq().Distinct(true).Gt("marla", 1), | 224 nq().Distinct(true).Gt("marla", 1), |
| 200 "SELECT * FROM `Foo` WHERE `marla` > 1 ORDER BY `marla`, `__key_ _`", | 225 "SELECT * FROM `Foo` WHERE `marla` > 1 ORDER BY `marla`, `__key_ _`", |
| 201 nil, nq().Gt("marla", 1)}, | 226 nil, nq().Gt("marla", 1)}, |
| 202 | 227 |
| 203 {"chained errors return the first", | 228 {"chained errors return the first", |
| 204 nq().Eq("__reserved__", 100).Eq("hello", "wurld").Order(""), | 229 nq().Eq("__reserved__", 100).Eq("hello", "wurld").Order(""), |
| 205 "", | 230 "", |
| 206 "__reserved__", nil}, | 231 "__reserved__", nil}, |
| 207 | 232 |
| 208 {"multiple ancestors", | 233 {"multiple ancestors", |
| 209 nq().Ancestor(mkKey("something", "correct")).Ancestor(mkKey("som ething", "else")), | 234 nq().Ancestor(mkKey("something", "correct")).Ancestor(mkKey("som ething", "else")), |
| 210 ("SELECT * FROM `Foo` " + | 235 ("SELECT * FROM `Foo` " + |
| 211 "WHERE __key__ HAS ANCESTOR KEY(DATASET(\"s~aid\"), NAME SPACE(\"ns\"), \"something\", \"else\") " + | 236 "WHERE __key__ HAS ANCESTOR KEY(DATASET(\"s~aid\"), NAME SPACE(\"ns\"), \"something\", \"else\") " + |
| 212 "ORDER BY `__key__`"), | 237 "ORDER BY `__key__`"), |
| 213 nil, nq().Ancestor(mkKey("something", "else"))}, | 238 nil, nq().Ancestor(mkKey("something", "else"))}, |
| 214 | 239 |
| 215 {"filter with illegal type", | 240 {"filter with illegal type", |
| 216 nq().Eq("something", complex(1, 2)), | 241 nq().Eq("something", complex(1, 2)), |
| 217 "", | 242 "", |
| 218 "bad type complex", nil}, | 243 "bad type complex", nil}, |
| 219 | 244 |
| 220 {"sort orders used for equality are ignored", | 245 {"sort orders used for equality are ignored", |
| 221 » » nq().Order("a", "b", "c").Eq("b", 2), | 246 » » nq().Order("a", "b", "c").Eq("b", 2, 2), |
| 222 "SELECT * FROM `Foo` WHERE `b` = 2 ORDER BY `a`, `c`, `__key__`" , | 247 "SELECT * FROM `Foo` WHERE `b` = 2 ORDER BY `a`, `c`, `__key__`" , |
| 223 nil, nq().Order("a", "c").Eq("b", 2)}, | 248 nil, nq().Order("a", "c").Eq("b", 2)}, |
| 224 | 249 |
| 225 {"sort orders used for equality are ignored (reversed)", | 250 {"sort orders used for equality are ignored (reversed)", |
| 226 nq().Eq("b", 2).Order("a", "b", "c"), | 251 nq().Eq("b", 2).Order("a", "b", "c"), |
| 227 "SELECT * FROM `Foo` WHERE `b` = 2 ORDER BY `a`, `c`, `__key__`" , | 252 "SELECT * FROM `Foo` WHERE `b` = 2 ORDER BY `a`, `c`, `__key__`" , |
| 228 nil, | 253 nil, |
| 229 nq().Order("a", "c").Eq("b", 2)}, | 254 nq().Order("a", "c").Eq("b", 2)}, |
| 230 | 255 |
| 231 {"duplicate orders are ignored", | 256 {"duplicate orders are ignored", |
| 232 nq().Order("a").Order("a").Order("a"), | 257 nq().Order("a").Order("a").Order("a"), |
| 233 "SELECT * FROM `Foo` ORDER BY `a`, `__key__`", | 258 "SELECT * FROM `Foo` ORDER BY `a`, `__key__`", |
| 234 nil, | 259 nil, |
| 235 nq().Order("a")}, | 260 nq().Order("a")}, |
| 236 | 261 |
| 237 {"Filtering on a reserved property is forbidden", | 262 {"Filtering on a reserved property is forbidden", |
| 238 nq().Gte("__special__", 10), | 263 nq().Gte("__special__", 10), |
| 239 "", | 264 "", |
| 240 "cannot filter/project on reserved property: \"__special__\"", | 265 "cannot filter/project on reserved property: \"__special__\"", |
| 241 nil}, | 266 nil}, |
| 242 | 267 |
| 243 {"in-bound key filters with ancestor OK", | 268 {"in-bound key filters with ancestor OK", |
| 244 » » nq().Ancestor(mkKey("Hello", 10)).Lt("__key__", mkKey("Hello", 1 0, "Something", "hi")), | 269 » » nq().Ancestor(mkKey("Hello", 10)).Lte("__key__", mkKey("Hello", 10, "Something", "hi")), |
| 245 ("SELECT * FROM `Foo` " + | 270 ("SELECT * FROM `Foo` " + |
| 246 » » » "WHERE `__key__` < KEY(DATASET(\"s~aid\"), NAMESPACE(\"n s\"), \"Hello\", 10, \"Something\", \"hi\") AND " + | 271 » » » "WHERE `__key__` <= KEY(DATASET(\"s~aid\"), NAMESPACE(\" ns\"), \"Hello\", 10, \"Something\", \"hi\") AND " + |
| 247 "__key__ HAS ANCESTOR KEY(DATASET(\"s~aid\"), NAMESPACE( \"ns\"), \"Hello\", 10) " + | 272 "__key__ HAS ANCESTOR KEY(DATASET(\"s~aid\"), NAMESPACE( \"ns\"), \"Hello\", 10) " + |
| 248 "ORDER BY `__key__`"), | 273 "ORDER BY `__key__`"), |
| 249 nil, | 274 nil, |
| 250 nil}, | 275 nil}, |
| 251 | 276 |
| 252 {"projection elements get filled in", | 277 {"projection elements get filled in", |
| 253 nq().Project("Foo", "Bar").Order("-Bar"), | 278 nq().Project("Foo", "Bar").Order("-Bar"), |
| 254 "SELECT `Bar`, `Foo` FROM `Foo` ORDER BY `Bar` DESC, `Foo`, `__k ey__`", | 279 "SELECT `Bar`, `Foo` FROM `Foo` ORDER BY `Bar` DESC, `Foo`, `__k ey__`", |
| 255 nil, nq().Project("Foo", "Bar").Order("-Bar").Order("Foo")}, | 280 nil, nq().Project("Foo", "Bar").Order("-Bar").Order("Foo")}, |
| 256 | 281 |
| 257 {"query without anything is fine", | 282 {"query without anything is fine", |
| 258 nq(), | 283 nq(), |
| 259 "SELECT * FROM `Foo` ORDER BY `__key__`", | 284 "SELECT * FROM `Foo` ORDER BY `__key__`", |
| 260 nil, | 285 nil, |
| 261 nil}, | 286 nil}, |
| 287 | |
| 288 {"ineq on __key__ with ancestor must be in range", | |
|
dnj
2015/09/29 19:06:23
nit: is "in range" accurate here? The condition yo
iannucci
2015/09/29 19:19:55
updated
| |
| 289 nq().Ancestor(mkKey("Hello", 10)).Lt("__key__", mkKey("Hello", 8 )), | |
| 290 "", | |
| 291 "inequality filters on __key__ must be descendants of the __ance stor__", nil}, | |
| 292 | |
| 293 {"ineq on __key__ with ancestor must be in range (2)", | |
| 294 nq().Ancestor(mkKey("Hello", 10)).Gt("__key__", mkKey("Hello", 8 )), | |
| 295 "", | |
| 296 "inequality filters on __key__ must be descendants of the __ance stor__", nil}, | |
| 297 | |
| 298 {"can build an empty query", | |
| 299 nq().Lt("hello", 10).Gt("hello", 50), | |
| 300 "", | |
| 301 ErrNullQuery, nil}, | |
| 262 } | 302 } |
| 263 | 303 |
| 264 func TestQueries(t *testing.T) { | 304 func TestQueries(t *testing.T) { |
| 265 t.Parallel() | 305 t.Parallel() |
| 266 | 306 |
| 267 Convey("queries have tons of condition checking", t, func() { | 307 Convey("queries have tons of condition checking", t, func() { |
| 268 for _, tc := range queryTests { | 308 for _, tc := range queryTests { |
| 269 Convey(tc.name, func() { | 309 Convey(tc.name, func() { |
| 270 fq, err := tc.q.Finalize() | 310 fq, err := tc.q.Finalize() |
| 271 if err == nil { | 311 if err == nil { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 282 So(err, ShouldBeNil) | 322 So(err, ShouldBeNil) |
| 283 | 323 |
| 284 fq.original = nil | 324 fq.original = nil |
| 285 fq2.original = nil | 325 fq2.original = nil |
| 286 So(fq, ShouldResemble, fq2) | 326 So(fq, ShouldResemble, fq2) |
| 287 } | 327 } |
| 288 }) | 328 }) |
| 289 } | 329 } |
| 290 }) | 330 }) |
| 291 } | 331 } |
| OLD | NEW |