Skip to content

Commit

Permalink
[-] return ignored error for QueryRow().Scan(), fixes #209 (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub authored Jul 24, 2024
1 parent f0c7674 commit 7792656
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestBatch(t *testing.T) {
// run the test
batch := &pgx.Batch{}
batch.Queue("select 1 + 1").QueryRow(func(row pgx.Row) error {
var n int32
var n int
return row.Scan(&n)
})
batch.Queue("update users set active = $1 where id = $2", true, 1).Exec(func(ct pgconn.CommandTag) (err error) {
Expand Down
7 changes: 3 additions & 4 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgxmock
import (
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -46,10 +47,8 @@ func (r *connRow) Scan(dest ...any) (err error) {
}
return rows.Err()
}

_ = rows.Scan(dest...)
rows.Close()
return rows.Err()
defer rows.Close()
return errors.Join(rows.Scan(dest...), rows.Err())
}

type rowSets struct {
Expand Down
36 changes: 36 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,39 @@ func TestConnRow(t *testing.T) {

a.NoError(mock.ExpectationsWereMet())
}

func TestInvalidsQueryRow(t *testing.T) {
mock, _ := NewPool()
a := assert.New(t)

// check invalid argument type
mock.ExpectQuery("SELECT").WillReturnRows(mock.NewRows([]string{"seq"}).AddRow("not-an-int"))
var expectedInt int
err := mock.QueryRow(ctx, "SELECT").Scan(&expectedInt)
a.Error(err)

// check BOF error
rs := mock.NewRows([]string{"seq"})
rs.AddRow("not-an-int").RowError(-1, errors.New("error")) // emulate pre-Next() error
mock.ExpectQuery("SELECT").WillReturnRows(rs)
err = mock.QueryRow(ctx, "SELECT").Scan(&expectedInt)
a.Error(err)

// check no row error
rs = mock.NewRows([]string{"seq"})
mock.ExpectQuery("SELECT").WillReturnRows(rs)
err = mock.QueryRow(ctx, "SELECT").Scan(&expectedInt)
a.Error(err)

//check first row error
rs = mock.NewRows([]string{"seq"}).RowError(0, errors.New("error"))
mock.ExpectQuery("SELECT").WillReturnRows(rs)
err = mock.QueryRow(ctx, "SELECT").Scan(&expectedInt)
a.Error(err)

// check pgtype.DriverBytes error
mock.ExpectQuery("SELECT").WillReturnRows(mock.NewRows([]string{"seq"}).AddRow("not-an-int"))
var d pgtype.DriverBytes
err = mock.QueryRow(ctx, "SELECT").Scan(&d)
a.Error(err)
}

0 comments on commit 7792656

Please sign in to comment.