@@ -517,22 +517,35 @@ func prepareCompareDatums() ([]Datum, []Datum) {
517
517
518
518
func TestStringToMysqlBit (t * testing.T ) {
519
519
tests := []struct {
520
- a Datum
521
- out []byte
520
+ a Datum
521
+ out []byte
522
+ flen int
523
+ truncated bool
522
524
}{
523
- {NewStringDatum ("true" ), []byte {1 }},
524
- {NewStringDatum ("false" ), []byte {0 }},
525
- {NewStringDatum ("1" ), []byte {1 }},
526
- {NewStringDatum ("0" ), []byte {0 }},
527
- {NewStringDatum ("b'1'" ), []byte {1 }},
528
- {NewStringDatum ("b'0'" ), []byte {0 }},
529
- }
530
- tp := NewFieldType (mysql .TypeBit )
531
- tp .SetFlen (1 )
525
+ {NewStringDatum ("true" ), []byte {1 }, 1 , true },
526
+ {NewStringDatum ("true" ), []byte {0x74 , 0x72 , 0x75 , 0x65 }, 32 , false },
527
+ {NewStringDatum ("false" ), []byte {0x1 }, 1 , true },
528
+ {NewStringDatum ("false" ), []byte {0x66 , 0x61 , 0x6c , 0x73 , 0x65 }, 40 , false },
529
+ {NewStringDatum ("1" ), []byte {1 }, 1 , true },
530
+ {NewStringDatum ("1" ), []byte {0x31 }, 8 , false },
531
+ {NewStringDatum ("0" ), []byte {1 }, 1 , true },
532
+ {NewStringDatum ("0" ), []byte {0x30 }, 8 , false },
533
+ {NewStringDatum ("b'1'" ), []byte {0x62 , 0x27 , 0x31 , 0x27 }, 32 , false },
534
+ {NewStringDatum ("b'0'" ), []byte {0x62 , 0x27 , 0x30 , 0x27 }, 32 , false },
535
+ }
532
536
for _ , tt := range tests {
533
- bin , err := tt .a .convertToMysqlBit (DefaultStmtNoWarningContext , tp )
534
- require .NoError (t , err )
535
- require .Equal (t , tt .out , bin .b )
537
+ t .Run (fmt .Sprintf ("%s %d %t" , tt .a .GetString (), tt .flen , tt .truncated ), func (t * testing.T ) {
538
+ tp := NewFieldType (mysql .TypeBit )
539
+ tp .SetFlen (tt .flen )
540
+
541
+ bin , err := tt .a .convertToMysqlBit (DefaultStmtNoWarningContext , tp )
542
+ if tt .truncated {
543
+ require .Contains (t , err .Error (), "Data Too Long" )
544
+ } else {
545
+ require .NoError (t , err )
546
+ }
547
+ require .Equal (t , tt .out , bin .b )
548
+ })
536
549
}
537
550
}
538
551
0 commit comments