Debian Codegraph @

dependency/sqltypes/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 "bytes"
21 "reflect"
22 "strings"
23 "testing"
24
25 "github.com/xwb1989/sqlparser/dependency/querypb"
26 )
27
28 const (
29 InvalidNeg = "-9223372036854775809"
30 MinNeg = "-9223372036854775808"
31 MinPos = "18446744073709551615"
32 InvalidPos = "18446744073709551616"
33 )
34
35 func TestNewValue(t *testing.T) {
36 testcases := []struct {
37 inType querypb.Type
38 inVal string
39 outVal Value
40 outErr string
41 }{{
42 inType: Null,
43 inVal: "",
44 outVal: NULL,
45 }, {
46 inType: Int8,
47 inVal: "1",
48 outVal: TestValue(Int8, "1"),
49 }, {
50 inType: Int16,
51 inVal: "1",
52 outVal: TestValue(Int16, "1"),
53 }, {
54 inType: Int24,
55 inVal: "1",
56 outVal: TestValue(Int24, "1"),
57 }, {
58 inType: Int32,
59 inVal: "1",
60 outVal: TestValue(Int32, "1"),
61 }, {
62 inType: Int64,
63 inVal: "1",
64 outVal: TestValue(Int64, "1"),
65 }, {
66 inType: Uint8,
67 inVal: "1",
68 outVal: TestValue(Uint8, "1"),
69 }, {
70 inType: Uint16,
71 inVal: "1",
72 outVal: TestValue(Uint16, "1"),
73 }, {
74 inType: Uint24,
75 inVal: "1",
76 outVal: TestValue(Uint24, "1"),
77 }, {
78 inType: Uint32,
79 inVal: "1",
80 outVal: TestValue(Uint32, "1"),
81 }, {
82 inType: Uint64,
83 inVal: "1",
84 outVal: TestValue(Uint64, "1"),
85 }, {
86 inType: Float32,
87 inVal: "1.00",
88 outVal: TestValue(Float32, "1.00"),
89 }, {
90 inType: Float64,
91 inVal: "1.00",
92 outVal: TestValue(Float64, "1.00"),
93 }, {
94 inType: Decimal,
95 inVal: "1.00",
96 outVal: TestValue(Decimal, "1.00"),
97 }, {
98 inType: Timestamp,
99 inVal: "2012-02-24 23:19:43",
100 outVal: TestValue(Timestamp, "2012-02-24 23:19:43"),
101 }, {
102 inType: Date,
103 inVal: "2012-02-24",
104 outVal: TestValue(Date, "2012-02-24"),
105 }, {
106 inType: Time,
107 inVal: "23:19:43",
108 outVal: TestValue(Time, "23:19:43"),
109 }, {
110 inType: Datetime,
111 inVal: "2012-02-24 23:19:43",
112 outVal: TestValue(Datetime, "2012-02-24 23:19:43"),
113 }, {
114 inType: Year,
115 inVal: "1",
116 outVal: TestValue(Year, "1"),
117 }, {
118 inType: Text,
119 inVal: "a",
120 outVal: TestValue(Text, "a"),
121 }, {
122 inType: Blob,
123 inVal: "a",
124 outVal: TestValue(Blob, "a"),
125 }, {
126 inType: VarChar,
127 inVal: "a",
128 outVal: TestValue(VarChar, "a"),
129 }, {
130 inType: Binary,
131 inVal: "a",
132 outVal: TestValue(Binary, "a"),
133 }, {
134 inType: Char,
135 inVal: "a",
136 outVal: TestValue(Char, "a"),
137 }, {
138 inType: Bit,
139 inVal: "1",
140 outVal: TestValue(Bit, "1"),
141 }, {
142 inType: Enum,
143 inVal: "a",
144 outVal: TestValue(Enum, "a"),
145 }, {
146 inType: Set,
147 inVal: "a",
148 outVal: TestValue(Set, "a"),
149 }, {
150 inType: VarBinary,
151 inVal: "a",
152 outVal: TestValue(VarBinary, "a"),
153 }, {
154 inType: Int64,
155 inVal: InvalidNeg,
156 outErr: "out of range",
157 }, {
158 inType: Int64,
159 inVal: InvalidPos,
160 outErr: "out of range",
161 }, {
162 inType: Uint64,
163 inVal: "-1",
164 outErr: "invalid syntax",
165 }, {
166 inType: Uint64,
167 inVal: InvalidPos,
168 outErr: "out of range",
169 }, {
170 inType: Float64,
171 inVal: "a",
172 outErr: "invalid syntax",
173 }, {
174 inType: Expression,
175 inVal: "a",
176 outErr: "invalid type specified for MakeValue: EXPRESSION",
177 }}
178 for _, tcase := range testcases {
179 v, err := NewValue(tcase.inType, []byte(tcase.inVal))
180 if tcase.outErr != "" {
181 if err == nil || !strings.Contains(err.Error(), tcase.outErr) {
182 t.Errorf("ValueFromBytes(%v, %v) error: %v, must contain %v", tcase.inType, tcase.inVal, err, tcase.outErr)
183 }
184 continue
185 }
186 if err != nil {
187 t.Errorf("ValueFromBytes(%v, %v) error: %v", tcase.inType, tcase.inVal, err)
188 continue
189 }
190 if !reflect.DeepEqual(v, tcase.outVal) {
191 t.Errorf("ValueFromBytes(%v, %v) = %v, want %v", tcase.inType, tcase.inVal, v, tcase.outVal)
192 }
193 }
194 }
195
196 // TestNew tests 'New' functions that are not tested
197 // through other code paths.
198 func TestNew(t *testing.T) {
199 got := NewInt32(1)
200 want := MakeTrusted(Int32, []byte("1"))
201 if !reflect.DeepEqual(got, want) {
202 t.Errorf("NewInt32(aa): %v, want %v", got, want)
203 }
204
205 got = NewVarBinary("aa")
206 want = MakeTrusted(VarBinary, []byte("aa"))
207 if !reflect.DeepEqual(got, want) {
208 t.Errorf("NewVarBinary(aa): %v, want %v", got, want)
209 }
210 }
211
212 func TestMakeTrusted(t *testing.T) {
213 v := MakeTrusted(Null, []byte("abcd"))
214 if !reflect.DeepEqual(v, NULL) {
215 t.Errorf("MakeTrusted(Null...) = %v, want null", v)
216 }
217 v = MakeTrusted(Int64, []byte("1"))
218 want := TestValue(Int64, "1")
219 if !reflect.DeepEqual(v, want) {
220 t.Errorf("MakeTrusted(Int64, \"1\") = %v, want %v", v, want)
221 }
222 }
223
224 func TestIntegralValue(t *testing.T) {
225 testcases := []struct {
226 in string
227 outVal Value
228 outErr string
229 }{{
230 in: MinNeg,
231 outVal: TestValue(Int64, MinNeg),
232 }, {
233 in: "1",
234 outVal: TestValue(Int64, "1"),
235 }, {
236 in: MinPos,
237 outVal: TestValue(Uint64, MinPos),
238 }, {
239 in: InvalidPos,
240 outErr: "out of range",
241 }}
242 for _, tcase := range testcases {
243 v, err := NewIntegral(tcase.in)
244 if tcase.outErr != "" {
245 if err == nil || !strings.Contains(err.Error(), tcase.outErr) {
246 t.Errorf("BuildIntegral(%v) error: %v, must contain %v", tcase.in, err, tcase.outErr)
247 }
248 continue
249 }
250 if err != nil {
251 t.Errorf("BuildIntegral(%v) error: %v", tcase.in, err)
252 continue
253 }
254 if !reflect.DeepEqual(v, tcase.outVal) {
255 t.Errorf("BuildIntegral(%v) = %v, want %v", tcase.in, v, tcase.outVal)
256 }
257 }
258 }
259
260 func TestInerfaceValue(t *testing.T) {
261 testcases := []struct {
262 in interface{}
263 out Value
264 }{{
265 in: nil,
266 out: NULL,
267 }, {
268 in: []byte("a"),
269 out: TestValue(VarBinary, "a"),
270 }, {
271 in: int64(1),
272 out: TestValue(Int64, "1"),
273 }, {
274 in: uint64(1),
275 out: TestValue(Uint64, "1"),
276 }, {
277 in: float64(1.2),
278 out: TestValue(Float64, "1.2"),
279 }, {
280 in: "a",
281 out: TestValue(VarChar, "a"),
282 }}
283 for _, tcase := range testcases {
284 v, err := InterfaceToValue(tcase.in)
285 if err != nil {
286 t.Errorf("BuildValue(%#v) error: %v", tcase.in, err)
287 continue
288 }
289 if !reflect.DeepEqual(v, tcase.out) {
290 t.Errorf("BuildValue(%#v) = %v, want %v", tcase.in, v, tcase.out)
291 }
292 }
293
294 _, err := InterfaceToValue(make(chan bool))
295 want := "unexpected"
296 if err == nil || !strings.Contains(err.Error(), want) {
297 t.Errorf("BuildValue(chan): %v, want %v", err, want)
298 }
299 }
300
301 func TestAccessors(t *testing.T) {
302 v := TestValue(Int64, "1")
303 if v.Type() != Int64 {
304 t.Errorf("v.Type=%v, want Int64", v.Type())
305 }
306 if !bytes.Equal(v.Raw(), []byte("1")) {
307 t.Errorf("v.Raw=%s, want 1", v.Raw())
308 }
309 if v.Len() != 1 {
310 t.Errorf("v.Len=%d, want 1", v.Len())
311 }
312 if v.ToString() != "1" {
313 t.Errorf("v.String=%s, want 1", v.ToString())
314 }
315 if v.IsNull() {
316 t.Error("v.IsNull: true, want false")
317 }
318 if !v.IsIntegral() {
319 t.Error("v.IsIntegral: false, want true")
320 }
321 if !v.IsSigned() {
322 t.Error("v.IsSigned: false, want true")
323 }
324 if v.IsUnsigned() {
325 t.Error("v.IsUnsigned: true, want false")
326 }
327 if v.IsFloat() {
328 t.Error("v.IsFloat: true, want false")
329 }
330 if v.IsQuoted() {
331 t.Error("v.IsQuoted: true, want false")
332 }
333 if v.IsText() {
334 t.Error("v.IsText: true, want false")
335 }
336 if v.IsBinary() {
337 t.Error("v.IsBinary: true, want false")
338 }
339 }
340
341 func TestToBytesAndString(t *testing.T) {
342 for _, v := range []Value{
343 NULL,
344 TestValue(Int64, "1"),
345 TestValue(Int64, "12"),
346 } {
347 if b := v.ToBytes(); bytes.Compare(b, v.Raw()) != 0 {
348 t.Errorf("%v.ToBytes: %s, want %s", v, b, v.Raw())
349 }
350 if s := v.ToString(); s != string(v.Raw()) {
351 t.Errorf("%v.ToString: %s, want %s", v, s, v.Raw())
352 }
353 }
354
355 tv := TestValue(Expression, "aa")
356 if b := tv.ToBytes(); b != nil {
357 t.Errorf("%v.ToBytes: %s, want nil", tv, b)
358 }
359 if s := tv.ToString(); s != "" {
360 t.Errorf("%v.ToString: %s, want \"\"", tv, s)
361 }
362 }
363
364 func TestEncode(t *testing.T) {
365 testcases := []struct {
366 in Value
367 outSQL string
368 outASCII string
369 }{{
370 in: NULL,
371 outSQL: "null",
372 outASCII: "null",
373 }, {
374 in: TestValue(Int64, "1"),
375 outSQL: "1",
376 outASCII: "1",
377 }, {
378 in: TestValue(VarChar, "foo"),
379 outSQL: "'foo'",
380 outASCII: "'Zm9v'",
381 }, {
382 in: TestValue(VarChar, "\x00'\"\b\n\r\t\x1A\\"),
383 outSQL: "'\\0\\'\\\"\\b\\n\\r\\t\\Z\\\\'",
384 outASCII: "'ACciCAoNCRpc'",
385 }}
386 for _, tcase := range testcases {
387 buf := &bytes.Buffer{}
388 tcase.in.EncodeSQL(buf)
389 if tcase.outSQL != buf.String() {
390 t.Errorf("%v.EncodeSQL = %q, want %q", tcase.in, buf.String(), tcase.outSQL)
391 }
392 buf = &bytes.Buffer{}
393 tcase.in.EncodeASCII(buf)
394 if tcase.outASCII != buf.String() {
395 t.Errorf("%v.EncodeASCII = %q, want %q", tcase.in, buf.String(), tcase.outASCII)
396 }
397 }
398 }
399
400 // TestEncodeMap ensures DontEscape is not escaped
401 func TestEncodeMap(t *testing.T) {
402 if SQLEncodeMap[DontEscape] != DontEscape {
403 t.Errorf("SQLEncodeMap[DontEscape] = %v, want %v", SQLEncodeMap[DontEscape], DontEscape)
404 }
405 if SQLDecodeMap[DontEscape] != DontEscape {
406 t.Errorf("SQLDecodeMap[DontEscape] = %v, want %v", SQLEncodeMap[DontEscape], DontEscape)
407 }
408 }