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

Side by Side Diff: impl/memory/datastore_query_test.go

Issue 1286943002: impl/memory: Make queries self-validate (Closed) Base URL: https://github.com/luci/gae.git@add_datastore_testable
Patch Set: add doc Created 5 years, 4 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
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 memory
6
7 import (
8 "math"
9 "testing"
10
11 dsS "github.com/luci/gae/service/datastore"
12 . "github.com/smartystreets/goconvey/convey"
13 "golang.org/x/net/context"
14 )
15
16 const (
17 MaxUint = ^uint(0)
18 MaxInt = int(MaxUint >> 1)
19 IntIs32Bits = int64(MaxInt) < math.MaxInt64
20 )
21
22 func TestDatastoreQueries(t *testing.T) {
23 Convey("Datastore Query suport", t, func() {
24 c := Use(context.Background())
25 ds := dsS.Get(c)
26 So(ds, ShouldNotBeNil)
27
28 Convey("can create good queries", func() {
29 q := ds.NewQuery("Foo").KeysOnly().Limit(10).Offset(39)
30 q = q.Start(queryCursor("kosmik")).End(queryCursor("krab s"))
31 So(q, ShouldNotBeNil)
32 So(q.(*queryImpl).err, ShouldBeNil)
33 done, err := q.(*queryImpl).valid("", false)
34 So(done, ShouldBeFalse)
35 So(err, ShouldBeNil)
36 })
37
38 Convey("ensures orders make sense", func() {
39 q := ds.NewQuery("Cool")
40 q = q.Filter("cat =", 19).Filter("bob =", 10).Order("bob ").Order("bob")
41
42 Convey("removes dups and equality orders", func() {
43 q = q.Order("wat")
44 qi := q.(*queryImpl)
45 done, err := qi.valid("", false)
46 So(done, ShouldBeFalse)
47 So(err, ShouldBeNil)
48 So(qi.order, ShouldResemble, []dsS.IndexColumn{{ Property: "wat"}})
49 })
50
51 Convey("if we equality-filter on __key__, that's just si lly", func() {
52 q = q.Order("wat")
53 q := q.Filter("__key__ =", ds.NewKey("Foo", "wat ", 0, nil))
54 _, err := q.(*queryImpl).valid("", false)
55 So(err.Error(), ShouldContainSubstring,
56 "query equality filter on __key__ is sil ly")
57 })
58
59 })
60
61 Convey("can create bad queries", func() {
62 q := ds.NewQuery("Foo")
63
64 Convey("only one inequality", func() {
65 q = q.Order("bob").Order("wat")
66 q = q.Filter("bob >", 10).Filter("wat <", 29)
67 qi := q.(*queryImpl)
68 _, err := qi.valid("", false)
69 So(err.Error(), ShouldContainSubstring,
70 "inequality filters on multiple properti es")
71 })
72
73 Convey("bad filter ops", func() {
74 q := q.Filter("Bob !", "value")
75 So(q.(*queryImpl).err.Error(), ShouldContainSubs tring, "invalid operator \"!\"")
76 })
77 Convey("bad filter", func() {
78 q := q.Filter("Bob", "value")
79 So(q.(*queryImpl).err.Error(), ShouldContainSubs tring, "invalid filter")
80 })
81 Convey("bad order", func() {
82 q := q.Order("+Bob")
83 So(q.(*queryImpl).err.Error(), ShouldContainSubs tring, "invalid order")
84 })
85 Convey("empty", func() {
86 q := q.Order("")
87 So(q.(*queryImpl).err.Error(), ShouldContainSubs tring, "empty order")
88 })
89 Convey("OOB limit", func() {
90 // this is supremely stupid. The SDK uses 'int' which measn we have to
91 // use it too, but then THEY BOUNDS CHECK IT FOR 32 BITS... *sigh*
92 if !IntIs32Bits {
93 q := q.Limit(MaxInt)
94 So(q.(*queryImpl).err.Error(), ShouldCon tainSubstring, "query limit overflow")
95 }
96 })
97 Convey("underflow offset", func() {
98 q := q.Offset(-29)
99 So(q.(*queryImpl).err.Error(), ShouldContainSubs tring, "negative query offset")
100 })
101 Convey("OOB offset", func() {
102 if !IntIs32Bits {
103 q := q.Offset(MaxInt)
104 So(q.(*queryImpl).err.Error(), ShouldCon tainSubstring, "query offset overflow")
105 }
106 })
107 Convey("Bad cursors", func() {
108 q := q.Start(queryCursor("")).End(queryCursor("" ))
109 So(q.(*queryImpl).err.Error(), ShouldContainSubs tring, "invalid cursor")
110 })
111 Convey("Bad ancestors", func() {
112 q := q.Ancestor(ds.NewKey("Goop", "wat", 10, nil ))
113 So(q, ShouldNotBeNil)
114 _, err := q.(*queryImpl).valid("", false)
115 So(err, ShouldEqual, dsS.ErrInvalidKey)
116 })
117 Convey("nil ancestors", func() {
118 _, err := q.Ancestor(nil).(*queryImpl).valid("", false)
119 So(err.Error(), ShouldContainSubstring, "nil que ry ancestor")
120 })
121 Convey("Bad key filters", func() {
122 q := q.Filter("__key__ >", ds.NewKey("Goop", "wa t", 10, nil))
123 _, err := q.(*queryImpl).valid("", false)
124 So(err, ShouldEqual, dsS.ErrInvalidKey)
125 })
126 Convey("non-ancestor queries in a transaction", func() {
127 _, err := q.(*queryImpl).valid("", true)
128 So(err.Error(), ShouldContainSubstring, "Only an cestor queries")
129 })
130 Convey("absurd numbers of filters are prohibited", func( ) {
131 q := q.Ancestor(ds.NewKey("thing", "wat", 0, nil ))
132 for i := 0; i < 100; i++ {
133 q = q.Filter("something =", i)
134 }
135 _, err := q.(*queryImpl).valid("", false)
136 So(err.Error(), ShouldContainSubstring, "query i s too large")
137 })
138 Convey("filters for __key__ that aren't keys", func() {
139 q := q.Filter("__key__ > ", 10)
140 _, err := q.(*queryImpl).valid("", false)
141 So(err.Error(), ShouldContainSubstring, "is not a key")
142 })
143 Convey("multiple inequalities", func() {
144 q := q.Filter("bob > ", 19).Filter("charlie < ", 20)
145 _, err := q.(*queryImpl).valid("", false)
146 So(err.Error(), ShouldContainSubstring,
147 "inequality filters on multiple properti es")
148 })
149 Convey("bad sort orders", func() {
150 q := q.Filter("bob > ", 19).Order("-charlie")
151 _, err := q.(*queryImpl).valid("", false)
152 So(err.Error(), ShouldContainSubstring, "first s ort order")
153 })
154 Convey("kindless with non-__key__ filters", func() {
155 q := ds.NewQuery("").Filter("face <", 25.3)
156 _, err := q.(*queryImpl).valid("", false)
157 So(err.Error(), ShouldContainSubstring,
158 "kindless queries can only filter on __k ey__")
159 })
160 Convey("kindless with non-__key__ orders", func() {
161 q := ds.NewQuery("").Order("face")
162 _, err := q.(*queryImpl).valid("", false)
163 So(err.Error(), ShouldContainSubstring,
164 "invalid order for kindless query")
165 })
166 Convey("kindless with decending-__key__ orders", func() {
167 q := ds.NewQuery("").Order("-__key__")
168 _, err := q.(*queryImpl).valid("", false)
169 So(err.Error(), ShouldContainSubstring,
170 "invalid order for kindless query")
171 })
172 })
173
174 })
175 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698