@@ -527,24 +527,37 @@ func prepareCompareDatums() ([]Datum, []Datum) {
527
527
528
528
func TestStringToMysqlBit (t * testing.T ) {
529
529
tests := []struct {
530
- a Datum
531
- out []byte
530
+ a Datum
531
+ out []byte
532
+ flen int
533
+ truncated bool
532
534
}{
533
- {NewStringDatum ("true" ), []byte {1 }},
534
- {NewStringDatum ("false" ), []byte {0 }},
535
- {NewStringDatum ("1" ), []byte {1 }},
536
- {NewStringDatum ("0" ), []byte {0 }},
537
- {NewStringDatum ("b'1'" ), []byte {1 }},
538
- {NewStringDatum ("b'0'" ), []byte {0 }},
535
+ {NewStringDatum ("true" ), []byte {1 }, 1 , true },
536
+ {NewStringDatum ("true" ), []byte {0x74 , 0x72 , 0x75 , 0x65 }, 32 , false },
537
+ {NewStringDatum ("false" ), []byte {0x1 }, 1 , true },
538
+ {NewStringDatum ("false" ), []byte {0x66 , 0x61 , 0x6c , 0x73 , 0x65 }, 40 , false },
539
+ {NewStringDatum ("1" ), []byte {1 }, 1 , true },
540
+ {NewStringDatum ("1" ), []byte {0x31 }, 8 , false },
541
+ {NewStringDatum ("0" ), []byte {1 }, 1 , true },
542
+ {NewStringDatum ("0" ), []byte {0x30 }, 8 , false },
543
+ {NewStringDatum ("b'1'" ), []byte {0x62 , 0x27 , 0x31 , 0x27 }, 32 , false },
544
+ {NewStringDatum ("b'0'" ), []byte {0x62 , 0x27 , 0x30 , 0x27 }, 32 , false },
539
545
}
540
546
sc := stmtctx .NewStmtCtx ()
541
547
sc .IgnoreTruncate .Store (true )
542
- tp := NewFieldType (mysql .TypeBit )
543
- tp .SetFlen (1 )
544
548
for _ , tt := range tests {
545
- bin , err := tt .a .convertToMysqlBit (nil , tp )
546
- require .NoError (t , err )
547
- require .Equal (t , tt .out , bin .b )
549
+ t .Run (fmt .Sprintf ("%s %d %t" , tt .a .GetString (), tt .flen , tt .truncated ), func (t * testing.T ) {
550
+ tp := NewFieldType (mysql .TypeBit )
551
+ tp .SetFlen (tt .flen )
552
+
553
+ bin , err := tt .a .convertToMysqlBit (sc , tp )
554
+ if tt .truncated {
555
+ require .Contains (t , err .Error (), "Data Too Long" )
556
+ } else {
557
+ require .NoError (t , err )
558
+ }
559
+ require .Equal (t , tt .out , bin .b )
560
+ })
548
561
}
549
562
}
550
563
0 commit comments