Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[-] return ignored error for QueryRow().Scan(), fixes #209 #211

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading