| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 TestProtoConversions(t *testing.T) { |
| 28 |
v := TestValue(Int64, "1") |
| 29 |
got := ValueToProto(v) |
| 30 |
want := &querypb.Value{Type: Int64, Value: []byte("1")} |
| 31 |
if !reflect.DeepEqual(got, want) { |
| 32 |
t.Errorf("ValueToProto: %v, want %v", got, want) |
| 33 |
} |
| 34 |
gotback := ProtoToValue(got) |
| 35 |
if !reflect.DeepEqual(gotback, v) { |
| 36 |
t.Errorf("ProtoToValue: %v, want %v", gotback, v) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestBuildBindVariables(t *testing.T) { |
| 41 |
tcases := []struct { |
| 42 |
in map[string]interface{} |
| 43 |
out map[string]*querypb.BindVariable |
| 44 |
err string |
| 45 |
}{{ |
| 46 |
in: nil, |
| 47 |
out: nil, |
| 48 |
}, { |
| 49 |
in: map[string]interface{}{ |
| 50 |
"k": int64(1), |
| 51 |
}, |
| 52 |
out: map[string]*querypb.BindVariable{ |
| 53 |
"k": Int64BindVariable(1), |
| 54 |
}, |
| 55 |
}, { |
| 56 |
in: map[string]interface{}{ |
| 57 |
"k": byte(1), |
| 58 |
}, |
| 59 |
err: "k: type uint8 not supported as bind var: 1", |
| 60 |
}} |
| 61 |
for _, tcase := range tcases { |
| 62 |
bindVars, err := BuildBindVariables(tcase.in) |
| 63 |
if err != nil { |
| 64 |
if err.Error() != tcase.err { |
| 65 |
t.Errorf("MapToBindVars(%v) error: %v, want %s", tcase.in, err, tcase.err) |
| 66 |
} |
| 67 |
continue |
| 68 |
} |
| 69 |
if tcase.err != "" { |
| 70 |
t.Errorf("MapToBindVars(%v) error: nil, want %s", tcase.in, tcase.err) |
| 71 |
continue |
| 72 |
} |
| 73 |
if !BindVariablesEqual(bindVars, tcase.out) { |
| 74 |
t.Errorf("MapToBindVars(%v): %v, want %s", tcase.in, bindVars, tcase.out) |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func TestBuildBindVariable(t *testing.T) { |
| 80 |
tcases := []struct { |
| 81 |
in interface{} |
| 82 |
out *querypb.BindVariable |
| 83 |
err string |
| 84 |
}{{ |
| 85 |
in: "aa", |
| 86 |
out: &querypb.BindVariable{ |
| 87 |
Type: querypb.Type_VARCHAR, |
| 88 |
Value: []byte("aa"), |
| 89 |
}, |
| 90 |
}, { |
| 91 |
in: []byte("aa"), |
| 92 |
out: &querypb.BindVariable{ |
| 93 |
Type: querypb.Type_VARBINARY, |
| 94 |
Value: []byte("aa"), |
| 95 |
}, |
| 96 |
}, { |
| 97 |
in: int(1), |
| 98 |
out: &querypb.BindVariable{ |
| 99 |
Type: querypb.Type_INT64, |
| 100 |
Value: []byte("1"), |
| 101 |
}, |
| 102 |
}, { |
| 103 |
in: int64(1), |
| 104 |
out: &querypb.BindVariable{ |
| 105 |
Type: querypb.Type_INT64, |
| 106 |
Value: []byte("1"), |
| 107 |
}, |
| 108 |
}, { |
| 109 |
in: uint64(1), |
| 110 |
out: &querypb.BindVariable{ |
| 111 |
Type: querypb.Type_UINT64, |
| 112 |
Value: []byte("1"), |
| 113 |
}, |
| 114 |
}, { |
| 115 |
in: float64(1), |
| 116 |
out: &querypb.BindVariable{ |
| 117 |
Type: querypb.Type_FLOAT64, |
| 118 |
Value: []byte("1"), |
| 119 |
}, |
| 120 |
}, { |
| 121 |
in: nil, |
| 122 |
out: NullBindVariable, |
| 123 |
}, { |
| 124 |
in: MakeTrusted(Int64, []byte("1")), |
| 125 |
out: &querypb.BindVariable{ |
| 126 |
Type: querypb.Type_INT64, |
| 127 |
Value: []byte("1"), |
| 128 |
}, |
| 129 |
}, { |
| 130 |
in: &querypb.BindVariable{ |
| 131 |
Type: querypb.Type_INT64, |
| 132 |
Value: []byte("1"), |
| 133 |
}, |
| 134 |
out: &querypb.BindVariable{ |
| 135 |
Type: querypb.Type_INT64, |
| 136 |
Value: []byte("1"), |
| 137 |
}, |
| 138 |
}, { |
| 139 |
in: []interface{}{"aa", int64(1)}, |
| 140 |
out: &querypb.BindVariable{ |
| 141 |
Type: querypb.Type_TUPLE, |
| 142 |
Values: []*querypb.Value{{ |
| 143 |
Type: querypb.Type_VARCHAR, |
| 144 |
Value: []byte("aa"), |
| 145 |
}, { |
| 146 |
Type: querypb.Type_INT64, |
| 147 |
Value: []byte("1"), |
| 148 |
}}, |
| 149 |
}, |
| 150 |
}, { |
| 151 |
in: []string{"aa", "bb"}, |
| 152 |
out: &querypb.BindVariable{ |
| 153 |
Type: querypb.Type_TUPLE, |
| 154 |
Values: []*querypb.Value{{ |
| 155 |
Type: querypb.Type_VARCHAR, |
| 156 |
Value: []byte("aa"), |
| 157 |
}, { |
| 158 |
Type: querypb.Type_VARCHAR, |
| 159 |
Value: []byte("bb"), |
| 160 |
}}, |
| 161 |
}, |
| 162 |
}, { |
| 163 |
in: [][]byte{[]byte("aa"), []byte("bb")}, |
| 164 |
out: &querypb.BindVariable{ |
| 165 |
Type: querypb.Type_TUPLE, |
| 166 |
Values: []*querypb.Value{{ |
| 167 |
Type: querypb.Type_VARBINARY, |
| 168 |
Value: []byte("aa"), |
| 169 |
}, { |
| 170 |
Type: querypb.Type_VARBINARY, |
| 171 |
Value: []byte("bb"), |
| 172 |
}}, |
| 173 |
}, |
| 174 |
}, { |
| 175 |
in: []int{1, 2}, |
| 176 |
out: &querypb.BindVariable{ |
| 177 |
Type: querypb.Type_TUPLE, |
| 178 |
Values: []*querypb.Value{{ |
| 179 |
Type: querypb.Type_INT64, |
| 180 |
Value: []byte("1"), |
| 181 |
}, { |
| 182 |
Type: querypb.Type_INT64, |
| 183 |
Value: []byte("2"), |
| 184 |
}}, |
| 185 |
}, |
| 186 |
}, { |
| 187 |
in: []int64{1, 2}, |
| 188 |
out: &querypb.BindVariable{ |
| 189 |
Type: querypb.Type_TUPLE, |
| 190 |
Values: []*querypb.Value{{ |
| 191 |
Type: querypb.Type_INT64, |
| 192 |
Value: []byte("1"), |
| 193 |
}, { |
| 194 |
Type: querypb.Type_INT64, |
| 195 |
Value: []byte("2"), |
| 196 |
}}, |
| 197 |
}, |
| 198 |
}, { |
| 199 |
in: []uint64{1, 2}, |
| 200 |
out: &querypb.BindVariable{ |
| 201 |
Type: querypb.Type_TUPLE, |
| 202 |
Values: []*querypb.Value{{ |
| 203 |
Type: querypb.Type_UINT64, |
| 204 |
Value: []byte("1"), |
| 205 |
}, { |
| 206 |
Type: querypb.Type_UINT64, |
| 207 |
Value: []byte("2"), |
| 208 |
}}, |
| 209 |
}, |
| 210 |
}, { |
| 211 |
in: []float64{1, 2}, |
| 212 |
out: &querypb.BindVariable{ |
| 213 |
Type: querypb.Type_TUPLE, |
| 214 |
Values: []*querypb.Value{{ |
| 215 |
Type: querypb.Type_FLOAT64, |
| 216 |
Value: []byte("1"), |
| 217 |
}, { |
| 218 |
Type: querypb.Type_FLOAT64, |
| 219 |
Value: []byte("2"), |
| 220 |
}}, |
| 221 |
}, |
| 222 |
}, { |
| 223 |
in: byte(1), |
| 224 |
err: "type uint8 not supported as bind var: 1", |
| 225 |
}, { |
| 226 |
in: []interface{}{1, byte(1)}, |
| 227 |
err: "type uint8 not supported as bind var: 1", |
| 228 |
}} |
| 229 |
for _, tcase := range tcases { |
| 230 |
bv, err := BuildBindVariable(tcase.in) |
| 231 |
if err != nil { |
| 232 |
if err.Error() != tcase.err { |
| 233 |
t.Errorf("ToBindVar(%T(%v)) error: %v, want %s", tcase.in, tcase.in, err, tcase.err) |
| 234 |
} |
| 235 |
continue |
| 236 |
} |
| 237 |
if tcase.err != "" { |
| 238 |
t.Errorf("ToBindVar(%T(%v)) error: nil, want %s", tcase.in, tcase.in, tcase.err) |
| 239 |
continue |
| 240 |
} |
| 241 |
if !reflect.DeepEqual(bv, tcase.out) { |
| 242 |
t.Errorf("ToBindVar(%T(%v)): %v, want %s", tcase.in, tcase.in, bv, tcase.out) |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestValidateBindVarables(t *testing.T) { |
| 248 |
tcases := []struct { |
| 249 |
in map[string]*querypb.BindVariable |
| 250 |
err string |
| 251 |
}{{ |
| 252 |
in: map[string]*querypb.BindVariable{ |
| 253 |
"v": { |
| 254 |
Type: querypb.Type_INT64, |
| 255 |
Value: []byte("1"), |
| 256 |
}, |
| 257 |
}, |
| 258 |
err: "", |
| 259 |
}, { |
| 260 |
in: map[string]*querypb.BindVariable{ |
| 261 |
"v": { |
| 262 |
Type: querypb.Type_INT64, |
| 263 |
Value: []byte("a"), |
| 264 |
}, |
| 265 |
}, |
| 266 |
err: `v: strconv.ParseInt: parsing "a": invalid syntax`, |
| 267 |
}, { |
| 268 |
in: map[string]*querypb.BindVariable{ |
| 269 |
"v": { |
| 270 |
Type: querypb.Type_TUPLE, |
| 271 |
Values: []*querypb.Value{{ |
| 272 |
Type: Int64, |
| 273 |
Value: []byte("a"), |
| 274 |
}}, |
| 275 |
}, |
| 276 |
}, |
| 277 |
err: `v: strconv.ParseInt: parsing "a": invalid syntax`, |
| 278 |
}} |
| 279 |
for _, tcase := range tcases { |
| 280 |
err := ValidateBindVariables(tcase.in) |
| 281 |
if tcase.err != "" { |
| 282 |
if err == nil || err.Error() != tcase.err { |
| 283 |
t.Errorf("ValidateBindVars(%v): %v, want %s", tcase.in, err, tcase.err) |
| 284 |
} |
| 285 |
continue |
| 286 |
} |
| 287 |
if err != nil { |
| 288 |
t.Errorf("ValidateBindVars(%v): %v, want nil", tcase.in, err) |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
func TestValidateBindVariable(t *testing.T) { |
| 294 |
testcases := []struct { |
| 295 |
in *querypb.BindVariable |
| 296 |
err string |
| 297 |
}{{ |
| 298 |
in: &querypb.BindVariable{ |
| 299 |
Type: querypb.Type_INT8, |
| 300 |
Value: []byte("1"), |
| 301 |
}, |
| 302 |
}, { |
| 303 |
in: &querypb.BindVariable{ |
| 304 |
Type: querypb.Type_INT16, |
| 305 |
Value: []byte("1"), |
| 306 |
}, |
| 307 |
}, { |
| 308 |
in: &querypb.BindVariable{ |
| 309 |
Type: querypb.Type_INT24, |
| 310 |
Value: []byte("1"), |
| 311 |
}, |
| 312 |
}, { |
| 313 |
in: &querypb.BindVariable{ |
| 314 |
Type: querypb.Type_INT32, |
| 315 |
Value: []byte("1"), |
| 316 |
}, |
| 317 |
}, { |
| 318 |
in: &querypb.BindVariable{ |
| 319 |
Type: querypb.Type_INT64, |
| 320 |
Value: []byte("1"), |
| 321 |
}, |
| 322 |
}, { |
| 323 |
in: &querypb.BindVariable{ |
| 324 |
Type: querypb.Type_UINT8, |
| 325 |
Value: []byte("1"), |
| 326 |
}, |
| 327 |
}, { |
| 328 |
in: &querypb.BindVariable{ |
| 329 |
Type: querypb.Type_UINT16, |
| 330 |
Value: []byte("1"), |
| 331 |
}, |
| 332 |
}, { |
| 333 |
in: &querypb.BindVariable{ |
| 334 |
Type: querypb.Type_UINT24, |
| 335 |
Value: []byte("1"), |
| 336 |
}, |
| 337 |
}, { |
| 338 |
in: &querypb.BindVariable{ |
| 339 |
Type: querypb.Type_UINT32, |
| 340 |
Value: []byte("1"), |
| 341 |
}, |
| 342 |
}, { |
| 343 |
in: &querypb.BindVariable{ |
| 344 |
Type: querypb.Type_UINT64, |
| 345 |
Value: []byte("1"), |
| 346 |
}, |
| 347 |
}, { |
| 348 |
in: &querypb.BindVariable{ |
| 349 |
Type: querypb.Type_FLOAT32, |
| 350 |
Value: []byte("1.00"), |
| 351 |
}, |
| 352 |
}, { |
| 353 |
in: &querypb.BindVariable{ |
| 354 |
Type: querypb.Type_FLOAT64, |
| 355 |
Value: []byte("1.00"), |
| 356 |
}, |
| 357 |
}, { |
| 358 |
in: &querypb.BindVariable{ |
| 359 |
Type: querypb.Type_DECIMAL, |
| 360 |
Value: []byte("1.00"), |
| 361 |
}, |
| 362 |
}, { |
| 363 |
in: &querypb.BindVariable{ |
| 364 |
Type: querypb.Type_TIMESTAMP, |
| 365 |
Value: []byte("2012-02-24 23:19:43"), |
| 366 |
}, |
| 367 |
}, { |
| 368 |
in: &querypb.BindVariable{ |
| 369 |
Type: querypb.Type_DATE, |
| 370 |
Value: []byte("2012-02-24"), |
| 371 |
}, |
| 372 |
}, { |
| 373 |
in: &querypb.BindVariable{ |
| 374 |
Type: querypb.Type_TIME, |
| 375 |
Value: []byte("23:19:43"), |
| 376 |
}, |
| 377 |
}, { |
| 378 |
in: &querypb.BindVariable{ |
| 379 |
Type: querypb.Type_DATETIME, |
| 380 |
Value: []byte("2012-02-24 23:19:43"), |
| 381 |
}, |
| 382 |
}, { |
| 383 |
in: &querypb.BindVariable{ |
| 384 |
Type: querypb.Type_YEAR, |
| 385 |
Value: []byte("1"), |
| 386 |
}, |
| 387 |
}, { |
| 388 |
in: &querypb.BindVariable{ |
| 389 |
Type: querypb.Type_TEXT, |
| 390 |
Value: []byte("a"), |
| 391 |
}, |
| 392 |
}, { |
| 393 |
in: &querypb.BindVariable{ |
| 394 |
Type: querypb.Type_BLOB, |
| 395 |
Value: []byte("a"), |
| 396 |
}, |
| 397 |
}, { |
| 398 |
in: &querypb.BindVariable{ |
| 399 |
Type: querypb.Type_VARCHAR, |
| 400 |
Value: []byte("a"), |
| 401 |
}, |
| 402 |
}, { |
| 403 |
in: &querypb.BindVariable{ |
| 404 |
Type: querypb.Type_BINARY, |
| 405 |
Value: []byte("a"), |
| 406 |
}, |
| 407 |
}, { |
| 408 |
in: &querypb.BindVariable{ |
| 409 |
Type: querypb.Type_CHAR, |
| 410 |
Value: []byte("a"), |
| 411 |
}, |
| 412 |
}, { |
| 413 |
in: &querypb.BindVariable{ |
| 414 |
Type: querypb.Type_BIT, |
| 415 |
Value: []byte("1"), |
| 416 |
}, |
| 417 |
}, { |
| 418 |
in: &querypb.BindVariable{ |
| 419 |
Type: querypb.Type_ENUM, |
| 420 |
Value: []byte("a"), |
| 421 |
}, |
| 422 |
}, { |
| 423 |
in: &querypb.BindVariable{ |
| 424 |
Type: querypb.Type_SET, |
| 425 |
Value: []byte("a"), |
| 426 |
}, |
| 427 |
}, { |
| 428 |
in: &querypb.BindVariable{ |
| 429 |
Type: querypb.Type_VARBINARY, |
| 430 |
Value: []byte("a"), |
| 431 |
}, |
| 432 |
}, { |
| 433 |
in: &querypb.BindVariable{ |
| 434 |
Type: querypb.Type_INT64, |
| 435 |
Value: []byte(InvalidNeg), |
| 436 |
}, |
| 437 |
err: "out of range", |
| 438 |
}, { |
| 439 |
in: &querypb.BindVariable{ |
| 440 |
Type: querypb.Type_INT64, |
| 441 |
Value: []byte(InvalidPos), |
| 442 |
}, |
| 443 |
err: "out of range", |
| 444 |
}, { |
| 445 |
in: &querypb.BindVariable{ |
| 446 |
Type: querypb.Type_UINT64, |
| 447 |
Value: []byte("-1"), |
| 448 |
}, |
| 449 |
err: "invalid syntax", |
| 450 |
}, { |
| 451 |
in: &querypb.BindVariable{ |
| 452 |
Type: querypb.Type_UINT64, |
| 453 |
Value: []byte(InvalidPos), |
| 454 |
}, |
| 455 |
err: "out of range", |
| 456 |
}, { |
| 457 |
in: &querypb.BindVariable{ |
| 458 |
Type: querypb.Type_FLOAT64, |
| 459 |
Value: []byte("a"), |
| 460 |
}, |
| 461 |
err: "invalid syntax", |
| 462 |
}, { |
| 463 |
in: &querypb.BindVariable{ |
| 464 |
Type: querypb.Type_EXPRESSION, |
| 465 |
Value: []byte("a"), |
| 466 |
}, |
| 467 |
err: "invalid type specified for MakeValue: EXPRESSION", |
| 468 |
}, { |
| 469 |
in: &querypb.BindVariable{ |
| 470 |
Type: querypb.Type_TUPLE, |
| 471 |
Values: []*querypb.Value{{ |
| 472 |
Type: querypb.Type_INT64, |
| 473 |
Value: []byte("1"), |
| 474 |
}}, |
| 475 |
}, |
| 476 |
}, { |
| 477 |
in: &querypb.BindVariable{ |
| 478 |
Type: querypb.Type_TUPLE, |
| 479 |
}, |
| 480 |
err: "empty tuple is not allowed", |
| 481 |
}, { |
| 482 |
in: &querypb.BindVariable{ |
| 483 |
Type: querypb.Type_TUPLE, |
| 484 |
Values: []*querypb.Value{{ |
| 485 |
Type: querypb.Type_TUPLE, |
| 486 |
}}, |
| 487 |
}, |
| 488 |
err: "tuple not allowed inside another tuple", |
| 489 |
}} |
| 490 |
for _, tcase := range testcases { |
| 491 |
err := ValidateBindVariable(tcase.in) |
| 492 |
if tcase.err != "" { |
| 493 |
if err == nil || !strings.Contains(err.Error(), tcase.err) { |
| 494 |
t.Errorf("ValidateBindVar(%v) error: %v, must contain %v", tcase.in, err, tcase.err) |
| 495 |
} |
| 496 |
continue |
| 497 |
} |
| 498 |
if err != nil { |
| 499 |
t.Errorf("ValidateBindVar(%v) error: %v", tcase.in, err) |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
err := ValidateBindVariable(nil) |
| 505 |
want := "bind variable is nil" |
| 506 |
if err == nil || err.Error() != want { |
| 507 |
t.Errorf("ValidateBindVar(nil) error: %v, want %s", err, want) |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
func TestBindVariableToValue(t *testing.T) { |
| 512 |
v, err := BindVariableToValue(Int64BindVariable(1)) |
| 513 |
if err != nil { |
| 514 |
t.Error(err) |
| 515 |
} |
| 516 |
want := MakeTrusted(querypb.Type_INT64, []byte("1")) |
| 517 |
if !reflect.DeepEqual(v, want) { |
| 518 |
t.Errorf("BindVarToValue(1): %v, want %v", v, want) |
| 519 |
} |
| 520 |
|
| 521 |
v, err = BindVariableToValue(&querypb.BindVariable{Type: querypb.Type_TUPLE}) |
| 522 |
wantErr := "cannot convert a TUPLE bind var into a value" |
| 523 |
if err == nil || err.Error() != wantErr { |
| 524 |
t.Errorf(" BindVarToValue(TUPLE): %v, want %s", err, wantErr) |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
func TestBindVariablesEqual(t *testing.T) { |
| 529 |
bv1 := map[string]*querypb.BindVariable{ |
| 530 |
"k": { |
| 531 |
Type: querypb.Type_INT64, |
| 532 |
Value: []byte("1"), |
| 533 |
}, |
| 534 |
} |
| 535 |
bv2 := map[string]*querypb.BindVariable{ |
| 536 |
"k": { |
| 537 |
Type: querypb.Type_INT64, |
| 538 |
Value: []byte("1"), |
| 539 |
}, |
| 540 |
} |
| 541 |
bv3 := map[string]*querypb.BindVariable{ |
| 542 |
"k": { |
| 543 |
Type: querypb.Type_INT64, |
| 544 |
Value: []byte("1"), |
| 545 |
}, |
| 546 |
} |
| 547 |
if !BindVariablesEqual(bv1, bv2) { |
| 548 |
t.Errorf("%v != %v, want equal", bv1, bv2) |
| 549 |
} |
| 550 |
if !BindVariablesEqual(bv1, bv3) { |
| 551 |
t.Errorf("%v = %v, want not equal", bv1, bv3) |
| 552 |
} |
| 553 |
} |