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