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

Function TestConvertToString

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

Source from the content-addressed store, hash-verified

329}
330
331func TestConvertToString(t *testing.T) {
332 testToString(t, "0", "0")
333 testToString(t, true, "1")
334 testToString(t, "false", "false")
335 testToString(t, 0, "0")
336 testToString(t, int64(0), "0")
337 testToString(t, uint64(0), "0")
338 testToString(t, float32(1.6), "1.6")
339 testToString(t, float64(-0.6), "-0.6")
340 testToString(t, []byte{1}, "\x01")
341 testToString(t, NewBinaryLiteralFromUint(0x4D7953514C, -1), "MySQL")
342 testToString(t, NewBinaryLiteralFromUint(0x41, -1), "A")
343 testToString(t, Enum{Name: "a", Value: 1}, "a")
344 testToString(t, Set{Name: "a", Value: 1}, "a")
345
346 t1, err := ParseTime(DefaultStmtNoWarningContext, "2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
347 require.NoError(t, err)
348 testToString(t, t1, "2011-11-10 11:11:11.999999")
349
350 td, _, err := ParseDuration(DefaultStmtNoWarningContext, "11:11:11.999999", 6)
351 require.NoError(t, err)
352 testToString(t, td, "11:11:11.999999")
353
354 ft := NewFieldType(mysql.TypeNewDecimal)
355 ft.SetFlen(10)
356 ft.SetDecimal(5)
357 v, err := Convert(3.1415926, ft)
358 require.NoError(t, err)
359 testToString(t, v, "3.14159")
360
361 _, err = ToString(&invalidMockType{})
362 require.Error(t, err)
363
364 // test truncate
365 tests := []struct {
366 flen int
367 charset string
368 input string
369 output string
370 }{
371 {5, "utf8", "你好,世界", "你好,世界"},
372 {5, "utf8mb4", "你好,世界", "你好,世界"},
373 {4, "utf8", "你好,世界", "你好,世"},
374 {4, "utf8mb4", "你好,世界", "你好,世"},
375 {15, "binary", "你好,世界", "你好,世界"},
376 {12, "binary", "你好,世界", "你好,世"},
377 {0, "binary", "你好,世界", ""},
378 }
379 for _, tt := range tests {
380 ft = NewFieldType(mysql.TypeVarchar)
381 ft.SetFlen(tt.flen)
382 ft.SetCharset(tt.charset)
383 inputDatum := NewStringDatum(tt.input)
384 outputDatum, err := inputDatum.ConvertTo(DefaultStmtNoWarningContext, ft)
385 if tt.input != tt.output {
386 require.True(t, ErrDataTooLong.Equal(err), "flen: %d, charset: %s, input: %s, output: %s", tt.flen, tt.charset, tt.input, tt.output)
387 } else {
388 require.NoError(t, err)

Callers

nothing calls this directly

Calls 15

SetFlenMethod · 0.95
SetDecimalMethod · 0.95
SetCharsetMethod · 0.95
testToStringFunction · 0.85
NewBinaryLiteralFromUintFunction · 0.85
ConvertFunction · 0.85
ParseTimeFunction · 0.70
ParseDurationFunction · 0.70
NewFieldTypeFunction · 0.70
ToStringFunction · 0.70
NewStringDatumFunction · 0.70
ErrorMethod · 0.65

Tested by

no test coverage detected