Debian Codegraph @

dependency/sqltypes/type_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 "testing"
21
22 "github.com/xwb1989/sqlparser/dependency/querypb"
23 )
24
25 func TestTypeValues(t *testing.T) {
26 testcases := []struct {
27 defined querypb.Type
28 expected int
29 }{{
30 defined: Null,
31 expected: 0,
32 }, {
33 defined: Int8,
34 expected: 1 | flagIsIntegral,
35 }, {
36 defined: Uint8,
37 expected: 2 | flagIsIntegral | flagIsUnsigned,
38 }, {
39 defined: Int16,
40 expected: 3 | flagIsIntegral,
41 }, {
42 defined: Uint16,
43 expected: 4 | flagIsIntegral | flagIsUnsigned,
44 }, {
45 defined: Int24,
46 expected: 5 | flagIsIntegral,
47 }, {
48 defined: Uint24,
49 expected: 6 | flagIsIntegral | flagIsUnsigned,
50 }, {
51 defined: Int32,
52 expected: 7 | flagIsIntegral,
53 }, {
54 defined: Uint32,
55 expected: 8 | flagIsIntegral | flagIsUnsigned,
56 }, {
57 defined: Int64,
58 expected: 9 | flagIsIntegral,
59 }, {
60 defined: Uint64,
61 expected: 10 | flagIsIntegral | flagIsUnsigned,
62 }, {
63 defined: Float32,
64 expected: 11 | flagIsFloat,
65 }, {
66 defined: Float64,
67 expected: 12 | flagIsFloat,
68 }, {
69 defined: Timestamp,
70 expected: 13 | flagIsQuoted,
71 }, {
72 defined: Date,
73 expected: 14 | flagIsQuoted,
74 }, {
75 defined: Time,
76 expected: 15 | flagIsQuoted,
77 }, {
78 defined: Datetime,
79 expected: 16 | flagIsQuoted,
80 }, {
81 defined: Year,
82 expected: 17 | flagIsIntegral | flagIsUnsigned,
83 }, {
84 defined: Decimal,
85 expected: 18,
86 }, {
87 defined: Text,
88 expected: 19 | flagIsQuoted | flagIsText,
89 }, {
90 defined: Blob,
91 expected: 20 | flagIsQuoted | flagIsBinary,
92 }, {
93 defined: VarChar,
94 expected: 21 | flagIsQuoted | flagIsText,
95 }, {
96 defined: VarBinary,
97 expected: 22 | flagIsQuoted | flagIsBinary,
98 }, {
99 defined: Char,
100 expected: 23 | flagIsQuoted | flagIsText,
101 }, {
102 defined: Binary,
103 expected: 24 | flagIsQuoted | flagIsBinary,
104 }, {
105 defined: Bit,
106 expected: 25 | flagIsQuoted,
107 }, {
108 defined: Enum,
109 expected: 26 | flagIsQuoted,
110 }, {
111 defined: Set,
112 expected: 27 | flagIsQuoted,
113 }, {
114 defined: Geometry,
115 expected: 29 | flagIsQuoted,
116 }, {
117 defined: TypeJSON,
118 expected: 30 | flagIsQuoted,
119 }, {
120 defined: Expression,
121 expected: 31,
122 }}
123 for _, tcase := range testcases {
124 if int(tcase.defined) != tcase.expected {
125 t.Errorf("Type %s: %d, want: %d", tcase.defined, int(tcase.defined), tcase.expected)
126 }
127 }
128 }
129
130 // TestCategory verifies that the type categorizations
131 // are non-overlapping and complete.
132 func TestCategory(t *testing.T) {
133 alltypes := []querypb.Type{
134 Null,
135 Int8,
136 Uint8,
137 Int16,
138 Uint16,
139 Int24,
140 Uint24,
141 Int32,
142 Uint32,
143 Int64,
144 Uint64,
145 Float32,
146 Float64,
147 Timestamp,
148 Date,
149 Time,
150 Datetime,
151 Year,
152 Decimal,
153 Text,
154 Blob,
155 VarChar,
156 VarBinary,
157 Char,
158 Binary,
159 Bit,
160 Enum,
161 Set,
162 Geometry,
163 TypeJSON,
164 Expression,
165 }
166 for _, typ := range alltypes {
167 matched := false
168 if IsSigned(typ) {
169 if !IsIntegral(typ) {
170 t.Errorf("Signed type %v is not an integral", typ)
171 }
172 matched = true
173 }
174 if IsUnsigned(typ) {
175 if !IsIntegral(typ) {
176 t.Errorf("Unsigned type %v is not an integral", typ)
177 }
178 if matched {
179 t.Errorf("%v matched more than one category", typ)
180 }
181 matched = true
182 }
183 if IsFloat(typ) {
184 if matched {
185 t.Errorf("%v matched more than one category", typ)
186 }
187 matched = true
188 }
189 if IsQuoted(typ) {
190 if matched {
191 t.Errorf("%v matched more than one category", typ)
192 }
193 matched = true
194 }
195 if typ == Null || typ == Decimal || typ == Expression {
196 if matched {
197 t.Errorf("%v matched more than one category", typ)
198 }
199 matched = true
200 }
201 if !matched {
202 t.Errorf("%v matched no category", typ)
203 }
204 }
205 }
206
207 func TestIsFunctions(t *testing.T) {
208 if IsIntegral(Null) {
209 t.Error("Null: IsIntegral, must be false")
210 }
211 if !IsIntegral(Int64) {
212 t.Error("Int64: !IsIntegral, must be true")
213 }
214 if IsSigned(Uint64) {
215 t.Error("Uint64: IsSigned, must be false")
216 }
217 if !IsSigned(Int64) {
218 t.Error("Int64: !IsSigned, must be true")
219 }
220 if IsUnsigned(Int64) {
221 t.Error("Int64: IsUnsigned, must be false")
222 }
223 if !IsUnsigned(Uint64) {
224 t.Error("Uint64: !IsUnsigned, must be true")
225 }
226 if IsFloat(Int64) {
227 t.Error("Int64: IsFloat, must be false")
228 }
229 if !IsFloat(Float64) {
230 t.Error("Uint64: !IsFloat, must be true")
231 }
232 if IsQuoted(Int64) {
233 t.Error("Int64: IsQuoted, must be false")
234 }
235 if !IsQuoted(Binary) {
236 t.Error("Binary: !IsQuoted, must be true")
237 }
238 if IsText(Int64) {
239 t.Error("Int64: IsText, must be false")
240 }
241 if !IsText(Char) {
242 t.Error("Char: !IsText, must be true")
243 }
244 if IsBinary(Int64) {
245 t.Error("Int64: IsBinary, must be false")
246 }
247 if !IsBinary(Binary) {
248 t.Error("Char: !IsBinary, must be true")
249 }
250 if !isNumber(Int64) {
251 t.Error("Int64: !isNumber, must be true")
252 }
253 }
254
255 func TestTypeToMySQL(t *testing.T) {
256 v, f := TypeToMySQL(Bit)
257 if v != 16 {
258 t.Errorf("Bit: %d, want 16", v)
259 }
260 if f != mysqlUnsigned {
261 t.Errorf("Bit flag: %x, want %x", f, mysqlUnsigned)
262 }
263 v, f = TypeToMySQL(Date)
264 if v != 10 {
265 t.Errorf("Bit: %d, want 10", v)
266 }
267 if f != mysqlBinary {
268 t.Errorf("Bit flag: %x, want %x", f, mysqlBinary)
269 }
270 }
271
272 func TestMySQLToType(t *testing.T) {
273 testcases := []struct {
274 intype int64
275 inflags int64
276 outtype querypb.Type
277 }{{
278 intype: 1,
279 outtype: Int8,
280 }, {
281 intype: 1,
282 inflags: mysqlUnsigned,
283 outtype: Uint8,
284 }, {
285 intype: 2,
286 outtype: Int16,
287 }, {
288 intype: 2,
289 inflags: mysqlUnsigned,
290 outtype: Uint16,
291 }, {
292 intype: 3,
293 outtype: Int32,
294 }, {
295 intype: 3,
296 inflags: mysqlUnsigned,
297 outtype: Uint32,
298 }, {
299 intype: 4,
300 outtype: Float32,
301 }, {
302 intype: 5,
303 outtype: Float64,
304 }, {
305 intype: 6,
306 outtype: Null,
307 }, {
308 intype: 7,
309 outtype: Timestamp,
310 }, {
311 intype: 8,
312 outtype: Int64,
313 }, {
314 intype: 8,
315 inflags: mysqlUnsigned,
316 outtype: Uint64,
317 }, {
318 intype: 9,
319 outtype: Int24,
320 }, {
321 intype: 9,
322 inflags: mysqlUnsigned,
323 outtype: Uint24,
324 }, {
325 intype: 10,
326 outtype: Date,
327 }, {
328 intype: 11,
329 outtype: Time,
330 }, {
331 intype: 12,
332 outtype: Datetime,
333 }, {
334 intype: 13,
335 outtype: Year,
336 }, {
337 intype: 16,
338 outtype: Bit,
339 }, {
340 intype: 245,
341 outtype: TypeJSON,
342 }, {
343 intype: 246,
344 outtype: Decimal,
345 }, {
346 intype: 249,
347 outtype: Text,
348 }, {
349 intype: 250,
350 outtype: Text,
351 }, {
352 intype: 251,
353 outtype: Text,
354 }, {
355 intype: 252,
356 outtype: Text,
357 }, {
358 intype: 252,
359 inflags: mysqlBinary,
360 outtype: Blob,
361 }, {
362 intype: 253,
363 outtype: VarChar,
364 }, {
365 intype: 253,
366 inflags: mysqlBinary,
367 outtype: VarBinary,
368 }, {
369 intype: 254,
370 outtype: Char,
371 }, {
372 intype: 254,
373 inflags: mysqlBinary,
374 outtype: Binary,
375 }, {
376 intype: 254,
377 inflags: mysqlEnum,
378 outtype: Enum,
379 }, {
380 intype: 254,
381 inflags: mysqlSet,
382 outtype: Set,
383 }, {
384 intype: 255,
385 outtype: Geometry,
386 }, {
387 // Binary flag must be ignored.
388 intype: 8,
389 inflags: mysqlUnsigned | mysqlBinary,
390 outtype: Uint64,
391 }, {
392 // Unsigned flag must be ignored
393 intype: 252,
394 inflags: mysqlUnsigned | mysqlBinary,
395 outtype: Blob,
396 }}
397 for _, tcase := range testcases {
398 got, err := MySQLToType(tcase.intype, tcase.inflags)
399 if err != nil {
400 t.Error(err)
401 }
402 if got != tcase.outtype {
403 t.Errorf("MySQLToType(%d, %x): %v, want %v", tcase.intype, tcase.inflags, got, tcase.outtype)
404 }
405 }
406 }
407
408 func TestTypeError(t *testing.T) {
409 _, err := MySQLToType(15, 0)
410 want := "unsupported type: 15"
411 if err == nil || err.Error() != want {
412 t.Errorf("MySQLToType: %v, want %s", err, want)
413 }
414 }