(t *testing.T)
| 430 | } |
| 431 | |
| 432 | func TestConvertToBinaryString(t *testing.T) { |
| 433 | nhUTF8 := "你好" |
| 434 | nhGBK := string([]byte{0xC4, 0xE3, 0xBA, 0xC3}) // "你好" in GBK |
| 435 | nhUTF8Invalid := "你好" + string([]byte{0x81}) |
| 436 | nhGBKInvalid := nhGBK + string([]byte{0x81}) |
| 437 | tests := []struct { |
| 438 | input string |
| 439 | inputCollate string |
| 440 | outputCharset string |
| 441 | output string |
| 442 | }{ |
| 443 | {nhUTF8, "utf8_bin", "utf8", nhUTF8}, |
| 444 | {nhUTF8, "utf8mb4_bin", "utf8mb4", nhUTF8}, |
| 445 | {nhUTF8, "gbk_bin", "utf8", nhUTF8}, |
| 446 | {nhUTF8, "gbk_bin", "gbk", nhUTF8}, |
| 447 | {nhUTF8, "binary", "utf8mb4", nhUTF8}, |
| 448 | {nhGBK, "binary", "gbk", nhUTF8}, |
| 449 | {nhUTF8, "utf8_bin", "binary", nhUTF8}, |
| 450 | {nhUTF8, "gbk_bin", "binary", nhGBK}, |
| 451 | {nhUTF8Invalid, "utf8_bin", "utf8", ""}, |
| 452 | {nhGBKInvalid, "gbk_bin", "gbk", ""}, |
| 453 | } |
| 454 | for _, tt := range tests { |
| 455 | ft := NewFieldType(mysql.TypeVarchar) |
| 456 | ft.SetFlen(255) |
| 457 | ft.SetCharset(tt.outputCharset) |
| 458 | inputDatum := NewCollationStringDatum(tt.input, tt.inputCollate) |
| 459 | outputDatum, err := inputDatum.ConvertTo(DefaultStmtNoWarningContext, ft) |
| 460 | if len(tt.output) == 0 { |
| 461 | require.True(t, charset.ErrInvalidCharacterString.Equal(err), tt) |
| 462 | } else { |
| 463 | require.NoError(t, err, tt) |
| 464 | require.Equal(t, tt.output, outputDatum.GetString(), tt) |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | func testStrToInt(t *testing.T, str string, expect int64, truncateAsErr bool, expectErr error) { |
| 470 | ctx := DefaultStmtNoWarningContext.WithFlags(DefaultStmtFlags.WithIgnoreTruncateErr(!truncateAsErr)) |
nothing calls this directly
no test coverage detected