@@ -22,6 +22,18 @@ func TestUnmarshalRowBool(t *testing.T) {
22
22
})
23
23
}
24
24
25
+ func TestUnmarshalRowBoolNotSettable (t * testing.T ) {
26
+ runOrmTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
27
+ rs := sqlmock .NewRows ([]string {"value" }).FromCSVString ("1" )
28
+ mock .ExpectQuery ("select (.+) from users where user=?" ).WithArgs ("anyone" ).WillReturnRows (rs )
29
+
30
+ var value bool
31
+ assert .NotNil (t , query (db , func (rows * sql.Rows ) error {
32
+ return unmarshalRow (value , rows , true )
33
+ }, "select value from users where user=?" , "anyone" ))
34
+ })
35
+ }
36
+
25
37
func TestUnmarshalRowInt (t * testing.T ) {
26
38
runOrmTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
27
39
rs := sqlmock .NewRows ([]string {"value" }).FromCSVString ("2" )
@@ -228,6 +240,40 @@ func TestUnmarshalRowStructWithTags(t *testing.T) {
228
240
})
229
241
}
230
242
243
+ func TestUnmarshalRowStructWithTagsWrongColumns (t * testing.T ) {
244
+ var value = new (struct {
245
+ Age * int `db:"age"`
246
+ Name string `db:"name"`
247
+ })
248
+
249
+ runOrmTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
250
+ rs := sqlmock .NewRows ([]string {"name" }).FromCSVString ("liao" )
251
+ mock .ExpectQuery ("select (.+) from users where user=?" ).WithArgs ("anyone" ).WillReturnRows (rs )
252
+
253
+ assert .NotNil (t , query (db , func (rows * sql.Rows ) error {
254
+ return unmarshalRow (value , rows , true )
255
+ }, "select name, age from users where user=?" , "anyone" ))
256
+ })
257
+ }
258
+
259
+ func TestUnmarshalRowStructWithTagsPtr (t * testing.T ) {
260
+ var value = new (struct {
261
+ Age * int `db:"age"`
262
+ Name string `db:"name"`
263
+ })
264
+
265
+ runOrmTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
266
+ rs := sqlmock .NewRows ([]string {"name" , "age" }).FromCSVString ("liao,5" )
267
+ mock .ExpectQuery ("select (.+) from users where user=?" ).WithArgs ("anyone" ).WillReturnRows (rs )
268
+
269
+ assert .Nil (t , query (db , func (rows * sql.Rows ) error {
270
+ return unmarshalRow (value , rows , true )
271
+ }, "select name, age from users where user=?" , "anyone" ))
272
+ assert .Equal (t , "liao" , value .Name )
273
+ assert .Equal (t , 5 , * value .Age )
274
+ })
275
+ }
276
+
231
277
func TestUnmarshalRowsBool (t * testing.T ) {
232
278
runOrmTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
233
279
var expect = []bool {true , false }
0 commit comments