@@ -370,6 +370,39 @@ func TestUnmarshalRowStructWithTags(t *testing.T) {
370370 })
371371}
372372
373+ func TestUnmarshalRowStructWithTagsIgnoreFields (t * testing.T ) {
374+ dbtest .RunTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
375+ value := new (struct {
376+ Age int `db:"age"`
377+ Name string
378+ Ignore bool
379+ })
380+
381+ rs := sqlmock .NewRows ([]string {"name" , "age" }).FromCSVString ("liao,5" )
382+ mock .ExpectQuery ("select (.+) from users where user=?" ).WithArgs ("anyone" ).WillReturnRows (rs )
383+
384+ assert .ErrorIs (t , query (context .Background (), db , func (rows * sql.Rows ) error {
385+ return unmarshalRow (value , rows , true )
386+ }, "select name, age from users where user=?" , "anyone" ), ErrNotMatchDestination )
387+ })
388+
389+ dbtest .RunTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
390+ value := new (struct {
391+ Age int `db:"age"`
392+ Name string
393+ Ignore bool `db:"-"`
394+ })
395+
396+ rs := sqlmock .NewRows ([]string {"name" , "age" }).FromCSVString ("liao,5" )
397+ mock .ExpectQuery ("select (.+) from users where user=?" ).WithArgs ("anyone" ).WillReturnRows (rs )
398+
399+ assert .Nil (t , query (context .Background (), db , func (rows * sql.Rows ) error {
400+ return unmarshalRow (value , rows , true )
401+ }, "select name, age from users where user=?" , "anyone" ))
402+ assert .Equal (t , 5 , value .Age )
403+ })
404+ }
405+
373406func TestUnmarshalRowStructWithTagsWrongColumns (t * testing.T ) {
374407 value := new (struct {
375408 Age * int `db:"age"`
@@ -999,6 +1032,58 @@ func TestUnmarshalRowsStructWithTags(t *testing.T) {
9991032 })
10001033}
10011034
1035+ func TestUnmarshalRowsStructWithTagsIgnoreFields (t * testing.T ) {
1036+ expect := []struct {
1037+ Name string
1038+ Age int64
1039+ Ignore bool
1040+ }{
1041+ {
1042+ Name : "first" ,
1043+ Age : 2 ,
1044+ Ignore : false ,
1045+ },
1046+ {
1047+ Name : "second" ,
1048+ Age : 3 ,
1049+ Ignore : false ,
1050+ },
1051+ }
1052+
1053+ dbtest .RunTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
1054+ var value []struct {
1055+ Age int64 `db:"age"`
1056+ Name string `db:"name"`
1057+ Ignore bool
1058+ }
1059+
1060+ rs := sqlmock .NewRows ([]string {"name" , "age" }).FromCSVString ("first,2\n second,3" )
1061+ mock .ExpectQuery ("select (.+) from users where user=?" ).WithArgs ("anyone" ).WillReturnRows (rs )
1062+ assert .ErrorIs (t , query (context .Background (), db , func (rows * sql.Rows ) error {
1063+ return unmarshalRows (& value , rows , true )
1064+ }, "select name, age from users where user=?" , "anyone" ), ErrNotMatchDestination )
1065+ })
1066+
1067+ dbtest .RunTest (t , func (db * sql.DB , mock sqlmock.Sqlmock ) {
1068+ var value []struct {
1069+ Age int64 `db:"age"`
1070+ Name string `db:"name"`
1071+ Ignore bool `db:"-"`
1072+ }
1073+
1074+ rs := sqlmock .NewRows ([]string {"name" , "age" }).FromCSVString ("first,2\n second,3" )
1075+ mock .ExpectQuery ("select (.+) from users where user=?" ).WithArgs ("anyone" ).WillReturnRows (rs )
1076+ assert .Nil (t , query (context .Background (), db , func (rows * sql.Rows ) error {
1077+ return unmarshalRows (& value , rows , true )
1078+ }, "select name, age from users where user=?" , "anyone" ))
1079+
1080+ for i , each := range expect {
1081+ assert .Equal (t , each .Name , value [i ].Name )
1082+ assert .Equal (t , each .Age , value [i ].Age )
1083+ }
1084+ })
1085+ }
1086+
10021087func TestUnmarshalRowsStructAndEmbeddedAnonymousStructWithTags (t * testing.T ) {
10031088 type Embed struct {
10041089 Value int64 `db:"value"`
0 commit comments