@@ -17,11 +17,13 @@ limitations under the License.
1717package vitessdriver
1818
1919import (
20- "reflect"
2120 "testing"
2221 "time"
2322
23+ "github.com/stretchr/testify/require"
24+
2425 "vitess.io/vitess/go/sqltypes"
26+ querypb "vitess.io/vitess/go/vt/proto/query"
2527)
2628
2729func TestToNative (t * testing.T ) {
@@ -147,7 +149,7 @@ func TestToNative(t *testing.T) {
147149 {
148150 convert : & converter {},
149151 in : sqltypes .TestValue (sqltypes .Blob , "a" ),
150- out : "a" ,
152+ out : [] byte ( "a" ) ,
151153 },
152154 {
153155 convert : & converter {},
@@ -157,7 +159,7 @@ func TestToNative(t *testing.T) {
157159 {
158160 convert : & converter {},
159161 in : sqltypes .TestValue (sqltypes .VarBinary , "a" ),
160- out : "a" ,
162+ out : [] byte ( "a" ) ,
161163 },
162164 {
163165 convert : & converter {},
@@ -167,7 +169,7 @@ func TestToNative(t *testing.T) {
167169 {
168170 convert : & converter {},
169171 in : sqltypes .TestValue (sqltypes .Binary , "a" ),
170- out : "a" ,
172+ out : [] byte ( "a" ) ,
171173 },
172174 {
173175 convert : & converter {},
@@ -193,11 +195,53 @@ func TestToNative(t *testing.T) {
193195
194196 for _ , tcase := range testcases {
195197 v , err := tcase .convert .ToNative (tcase .in )
196- if err != nil {
197- t .Error (err )
198- }
199- if ! reflect .DeepEqual (v , tcase .out ) {
200- t .Errorf ("%v.ToNativeEx = %#v, want %#v" , tcase .in , v , tcase .out )
201- }
198+ require .NoError (t , err )
199+ require .Equal (t , tcase .out , v )
200+ }
201+ }
202+
203+ func TestBuildBindVariable (t * testing.T ) {
204+ testcases := []struct {
205+ name string
206+ in any
207+ out * querypb.BindVariable
208+ }{
209+ {
210+ name : "json bytes become varchar" ,
211+ in : []byte ("[]" ),
212+ out : sqltypes .StringBindVariable ("[]" ),
213+ },
214+ {
215+ name : "binary bytes become varchar" ,
216+ in : []byte {0x00 , 0xff },
217+ out : & querypb.BindVariable {
218+ Type : querypb .Type_VARCHAR ,
219+ Value : []byte {0x00 , 0xff },
220+ },
221+ },
222+ {
223+ name : "nil bytes become null" ,
224+ in : []byte (nil ),
225+ out : sqltypes .NullBindVariable ,
226+ },
227+ {
228+ name : "empty bytes become empty varchar" ,
229+ in : []byte {},
230+ out : sqltypes .StringBindVariable ("" ),
231+ },
232+ {
233+ name : "time becomes datetime" ,
234+ in : time .Date (2012 , 0o2 , 24 , 23 , 19 , 43 , 0 , time .UTC ),
235+ out : sqltypes .ValueBindVariable (sqltypes .TestValue (sqltypes .Datetime , "2012-02-24 23:19:43" )),
236+ },
237+ }
238+
239+ convert := & converter {location : time .UTC }
240+ for _ , tcase := range testcases {
241+ t .Run (tcase .name , func (t * testing.T ) {
242+ bv , err := convert .BuildBindVariable (tcase .in )
243+ require .NoError (t , err )
244+ require .Equal (t , tcase .out , bv )
245+ })
202246 }
203247}
0 commit comments