Debian Codegraph @

dependency/sqltypes/plan_value_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqltypes
18
19 import (
20 "reflect"
21 "strings"
22 "testing"
23
24 "github.com/xwb1989/sqlparser/dependency/querypb"
25 )
26
27 func TestPlanValueIsNull(t *testing.T) {
28 tcases := []struct {
29 in PlanValue
30 out bool
31 }{{
32 in: PlanValue{},
33 out: true,
34 }, {
35 in: PlanValue{Key: "aa"},
36 out: false,
37 }, {
38 in: PlanValue{Value: NewVarBinary("aa")},
39 out: false,
40 }, {
41 in: PlanValue{ListKey: "aa"},
42 out: false,
43 }, {
44 in: PlanValue{Values: []PlanValue{}},
45 out: false,
46 }}
47 for _, tc := range tcases {
48 got := tc.in.IsNull()
49 if got != tc.out {
50 t.Errorf("IsNull(%v): %v, want %v", tc.in, got, tc.out)
51 }
52 }
53 }
54
55 func TestPlanValueIsList(t *testing.T) {
56 tcases := []struct {
57 in PlanValue
58 out bool
59 }{{
60 in: PlanValue{},
61 out: false,
62 }, {
63 in: PlanValue{Key: "aa"},
64 out: false,
65 }, {
66 in: PlanValue{Value: NewVarBinary("aa")},
67 out: false,
68 }, {
69 in: PlanValue{ListKey: "aa"},
70 out: true,
71 }, {
72 in: PlanValue{Values: []PlanValue{}},
73 out: true,
74 }}
75 for _, tc := range tcases {
76 got := tc.in.IsList()
77 if got != tc.out {
78 t.Errorf("IsList(%v): %v, want %v", tc.in, got, tc.out)
79 }
80 }
81 }
82
83 func TestResolveRows(t *testing.T) {
84 testBindVars := map[string]*querypb.BindVariable{
85 "int": Int64BindVariable(10),
86 "intstr": TestBindVariable([]interface{}{10, "aa"}),
87 }
88 intValue := MakeTrusted(Int64, []byte("10"))
89 strValue := MakeTrusted(VarChar, []byte("aa"))
90 tcases := []struct {
91 in []PlanValue
92 out [][]Value
93 err string
94 }{{
95 // Simple cases.
96 in: []PlanValue{
97 {Key: "int"},
98 },
99 out: [][]Value{
100 {intValue},
101 },
102 }, {
103 in: []PlanValue{
104 {Value: intValue},
105 },
106 out: [][]Value{
107 {intValue},
108 },
109 }, {
110 in: []PlanValue{
111 {ListKey: "intstr"},
112 },
113 out: [][]Value{
114 {intValue},
115 {strValue},
116 },
117 }, {
118 in: []PlanValue{
119 {Values: []PlanValue{{Value: intValue}, {Value: strValue}}},
120 },
121 out: [][]Value{
122 {intValue},
123 {strValue},
124 },
125 }, {
126 in: []PlanValue{
127 {Values: []PlanValue{{Key: "int"}, {Value: strValue}}},
128 },
129 out: [][]Value{
130 {intValue},
131 {strValue},
132 },
133 }, {
134 in: []PlanValue{{}},
135 out: [][]Value{
136 {NULL},
137 },
138 }, {
139 // Cases with varying rowcounts.
140 // All types of input..
141 in: []PlanValue{
142 {Key: "int"},
143 {Value: strValue},
144 {ListKey: "intstr"},
145 {Values: []PlanValue{{Value: strValue}, {Value: intValue}}},
146 },
147 out: [][]Value{
148 {intValue, strValue, intValue, strValue},
149 {intValue, strValue, strValue, intValue},
150 },
151 }, {
152 // list, val, list.
153 in: []PlanValue{
154 {Value: strValue},
155 {Key: "int"},
156 {Values: []PlanValue{{Value: strValue}, {Value: intValue}}},
157 },
158 out: [][]Value{
159 {strValue, intValue, strValue},
160 {strValue, intValue, intValue},
161 },
162 }, {
163 // list, list
164 in: []PlanValue{
165 {ListKey: "intstr"},
166 {Values: []PlanValue{{Value: strValue}, {Value: intValue}}},
167 },
168 out: [][]Value{
169 {intValue, strValue},
170 {strValue, intValue},
171 },
172 }, {
173 // Error cases
174 in: []PlanValue{
175 {ListKey: "intstr"},
176 {Values: []PlanValue{{Value: strValue}}},
177 },
178 err: "mismatch in number of column values",
179 }, {
180 // This is a different code path for a similar validation.
181 in: []PlanValue{
182 {Values: []PlanValue{{Value: strValue}}},
183 {ListKey: "intstr"},
184 },
185 err: "mismatch in number of column values",
186 }, {
187 in: []PlanValue{
188 {Key: "absent"},
189 },
190 err: "missing bind var",
191 }, {
192 in: []PlanValue{
193 {ListKey: "absent"},
194 },
195 err: "missing bind var",
196 }, {
197 in: []PlanValue{
198 {Values: []PlanValue{{Key: "absent"}}},
199 },
200 err: "missing bind var",
201 }}
202
203 for _, tc := range tcases {
204 got, err := ResolveRows(tc.in, testBindVars)
205 if err != nil {
206 if !strings.Contains(err.Error(), tc.err) {
207 t.Errorf("ResolveRows(%v) error: %v, want '%s'", tc.in, err, tc.err)
208 }
209 continue
210 }
211 if tc.err != "" {
212 t.Errorf("ResolveRows(%v) error: nil, want '%s'", tc.in, tc.err)
213 continue
214 }
215 if !reflect.DeepEqual(got, tc.out) {
216 t.Errorf("ResolveRows(%v): %v, want %v", tc.in, got, tc.out)
217 }
218 }
219 }
220
221 func TestResolveList(t *testing.T) {
222 testBindVars := map[string]*querypb.BindVariable{
223 "int": Int64BindVariable(10),
224 "intstr": TestBindVariable([]interface{}{10, "aa"}),
225 }
226 intValue := MakeTrusted(Int64, []byte("10"))
227 strValue := MakeTrusted(VarChar, []byte("aa"))
228 tcases := []struct {
229 in PlanValue
230 out []Value
231 err string
232 }{{
233 in: PlanValue{ListKey: "intstr"},
234 out: []Value{intValue, strValue},
235 }, {
236 in: PlanValue{Values: []PlanValue{{Value: intValue}, {Value: strValue}}},
237 out: []Value{intValue, strValue},
238 }, {
239 in: PlanValue{Values: []PlanValue{{Key: "int"}, {Value: strValue}}},
240 out: []Value{intValue, strValue},
241 }, {
242 in: PlanValue{ListKey: "absent"},
243 err: "missing bind var",
244 }, {
245 in: PlanValue{Values: []PlanValue{{Key: "absent"}, {Value: strValue}}},
246 err: "missing bind var",
247 }, {
248 in: PlanValue{ListKey: "int"},
249 err: "single value was supplied for TUPLE bind var",
250 }, {
251 in: PlanValue{Key: "int"},
252 err: "a single value was supplied where a list was expected",
253 }}
254
255 for _, tc := range tcases {
256 got, err := tc.in.ResolveList(testBindVars)
257 if err != nil {
258 if !strings.Contains(err.Error(), tc.err) {
259 t.Errorf("ResolveList(%v) error: %v, want '%s'", tc.in, err, tc.err)
260 }
261 continue
262 }
263 if tc.err != "" {
264 t.Errorf("ResolveList(%v) error: nil, want '%s'", tc.in, tc.err)
265 continue
266 }
267 if !reflect.DeepEqual(got, tc.out) {
268 t.Errorf("ResolveList(%v): %v, want %v", tc.in, got, tc.out)
269 }
270 }
271 }
272
273 func TestResolveValue(t *testing.T) {
274 testBindVars := map[string]*querypb.BindVariable{
275 "int": Int64BindVariable(10),
276 "intstr": TestBindVariable([]interface{}{10, "aa"}),
277 }
278 intValue := MakeTrusted(Int64, []byte("10"))
279 tcases := []struct {
280 in PlanValue
281 out Value
282 err string
283 }{{
284 in: PlanValue{Key: "int"},
285 out: intValue,
286 }, {
287 in: PlanValue{Value: intValue},
288 out: intValue,
289 }, {
290 in: PlanValue{},
291 out: NULL,
292 }, {
293 in: PlanValue{Key: "absent"},
294 err: "missing bind var",
295 }, {
296 in: PlanValue{Key: "intstr"},
297 err: "TUPLE was supplied for single value bind var",
298 }, {
299 in: PlanValue{ListKey: "intstr"},
300 err: "a list was supplied where a single value was expected",
301 }}
302
303 for _, tc := range tcases {
304 got, err := tc.in.ResolveValue(testBindVars)
305 if err != nil {
306 if !strings.Contains(err.Error(), tc.err) {
307 t.Errorf("ResolveValue(%v) error: %v, want '%s'", tc.in, err, tc.err)
308 }
309 continue
310 }
311 if tc.err != "" {
312 t.Errorf("ResolveValue(%v) error: nil, want '%s'", tc.in, tc.err)
313 continue
314 }
315 if !reflect.DeepEqual(got, tc.out) {
316 t.Errorf("ResolveValue(%v): %v, want %v", tc.in, got, tc.out)
317 }
318 }
319 }