diff --git a/batch_test.go b/batch_test.go index 6dea88b..697b77c 100644 --- a/batch_test.go +++ b/batch_test.go @@ -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) { diff --git a/rows.go b/rows.go index 3c94a9b..ac81935 100644 --- a/rows.go +++ b/rows.go @@ -3,6 +3,7 @@ package pgxmock import ( "encoding/csv" "encoding/json" + "errors" "fmt" "reflect" "strings" @@ -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 { diff --git a/rows_test.go b/rows_test.go index bb312f6..b1a183d 100644 --- a/rows_test.go +++ b/rows_test.go @@ -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) +}