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

Function TestConvertToStringWithCheck

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

Source from the content-addressed store, hash-verified

392}
393
394func TestConvertToStringWithCheck(t *testing.T) {
395 nhUTF8 := "你好"
396 nhUTF8MB4 := "你好👋"
397 nhUTF8Invalid := "你好" + string([]byte{0x81})
398 tests := []struct {
399 input string
400 outputChs string
401 newFlags func(flags Flags) Flags
402 output string
403 }{
404 {nhUTF8, "utf8mb4", func(f Flags) Flags { return f.WithSkipUTF8Check(false) }, nhUTF8},
405 {nhUTF8MB4, "utf8mb4", func(f Flags) Flags { return f.WithSkipUTF8Check(false) }, nhUTF8MB4},
406 {nhUTF8, "utf8mb4", func(f Flags) Flags { return f.WithSkipUTF8Check(true) }, nhUTF8},
407 {nhUTF8MB4, "utf8mb4", func(f Flags) Flags { return f.WithSkipUTF8Check(true) }, nhUTF8MB4},
408 {nhUTF8Invalid, "utf8mb4", func(f Flags) Flags { return f.WithSkipUTF8Check(true) }, nhUTF8Invalid},
409 {nhUTF8Invalid, "utf8mb4", func(f Flags) Flags { return f.WithSkipUTF8Check(false) }, ""},
410 {nhUTF8Invalid, "ascii", func(f Flags) Flags { return f.WithSkipSACIICheck(false) }, ""},
411 {nhUTF8Invalid, "ascii", func(f Flags) Flags { return f.WithSkipSACIICheck(true) }, nhUTF8Invalid},
412 {nhUTF8MB4, "utf8", func(f Flags) Flags { return f.WithSkipUTF8MB4Check(false) }, ""},
413 {nhUTF8MB4, "utf8", func(f Flags) Flags { return f.WithSkipUTF8MB4Check(true) }, nhUTF8MB4},
414 }
415 for _, tt := range tests {
416 ft := NewFieldType(mysql.TypeVarchar)
417 ft.SetFlen(255)
418 ft.SetCharset(tt.outputChs)
419 inputDatum := NewStringDatum(tt.input)
420 ctx := DefaultStmtNoWarningContext
421 ctx = ctx.WithFlags(tt.newFlags(DefaultStmtFlags))
422 outputDatum, err := inputDatum.ConvertTo(ctx, ft)
423 if len(tt.output) == 0 {
424 require.True(t, charset.ErrInvalidCharacterString.Equal(err), tt)
425 } else {
426 require.NoError(t, err, tt)
427 require.Equal(t, tt.output, outputDatum.GetString(), tt)
428 }
429 }
430}
431
432func TestConvertToBinaryString(t *testing.T) {
433 nhUTF8 := "你好"

Callers

nothing calls this directly

Calls 11

SetFlenMethod · 0.95
SetCharsetMethod · 0.95
WithSkipUTF8CheckMethod · 0.80
WithSkipSACIICheckMethod · 0.80
WithSkipUTF8MB4CheckMethod · 0.80
WithFlagsMethod · 0.80
NewFieldTypeFunction · 0.70
NewStringDatumFunction · 0.70
EqualMethod · 0.65
GetStringMethod · 0.65
ConvertToMethod · 0.45

Tested by

no test coverage detected