MCPcopy
hub / github.com/pingcap/tidb / TestConvertToBinaryString

Function TestConvertToBinaryString

pkg/types/convert_test.go:432–467  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

430}
431
432func 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
469func testStrToInt(t *testing.T, str string, expect int64, truncateAsErr bool, expectErr error) {
470 ctx := DefaultStmtNoWarningContext.WithFlags(DefaultStmtFlags.WithIgnoreTruncateErr(!truncateAsErr))

Callers

nothing calls this directly

Calls 7

SetFlenMethod · 0.95
SetCharsetMethod · 0.95
NewCollationStringDatumFunction · 0.85
NewFieldTypeFunction · 0.70
EqualMethod · 0.65
GetStringMethod · 0.65
ConvertToMethod · 0.45

Tested by

no test coverage detected