Skip to content

Commit

Permalink
fix pgxmock.QueryRow when query returns error
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Feb 17, 2021
1 parent 4837708 commit 7291baa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
11 changes: 9 additions & 2 deletions pgxmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ func (c *pgxmock) Query(ctx context.Context, sql string, args ...interface{}) (p
return nil, err
}

type errRow struct {
err error
}

func (er errRow) Scan(dest ...interface{}) error {
return er.err
}

func (c *pgxmock) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
ex, err := c.query(sql, args)
if ex != nil {
Expand All @@ -526,8 +534,7 @@ func (c *pgxmock) QueryRow(ctx context.Context, sql string, args ...interface{})
return nil
}
}

return nil
return errRow{err}
}

// Implement the "ExecerContext" interface
Expand Down
4 changes: 2 additions & 2 deletions pgxmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestMockQuery(t *testing.T) {
}
defer mock.Close(context.Background())

rs := NewRows([]string{"id", "title"}).FromCSVString("5,hello world")
rs := NewRows([]string{"id", "title"}).AddRow(5, "hello world")

mock.ExpectQuery("SELECT (.+) FROM articles WHERE id = ?").
WithArgs(5).
Expand Down Expand Up @@ -425,7 +425,7 @@ func TestWrongExpectations(t *testing.T) {

mock.ExpectBegin()

rs1 := NewRows([]string{"id", "title"}).FromCSVString("5,hello world")
rs1 := NewRows([]string{"id", "title"}).AddRow(5, "hello world")
mock.ExpectQuery("SELECT (.+) FROM articles WHERE id = ?").
WithArgs(5).
WillReturnRows(rs1)
Expand Down
26 changes: 13 additions & 13 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ func ExampleRows_rowError() {
// got rows error: row error
}

func ExampleRows_closeError() {
mock, err := NewConn()
if err != nil {
fmt.Println("failed to open sqlmock database:", err)
}
defer mock.Close(context.Background())
// func ExampleRows_closeError() {
// mock, err := NewConn()
// if err != nil {
// fmt.Println("failed to open sqlmock database:", err)
// }
// defer mock.Close(context.Background())

rows := NewRows([]string{"id", "title"}).CloseError(fmt.Errorf("close error"))
mock.ExpectQuery("SELECT").WillReturnRows(rows)
// rows := NewRows([]string{"id", "title"}).CloseError(fmt.Errorf("close error"))
// mock.ExpectQuery("SELECT").WillReturnRows(rows)

rs, _ := mock.Query(context.Background(), "SELECT")
rs.Close()
// Output: got error: close error
}
// rs, _ := mock.Query(context.Background(), "SELECT")
// rs.Close()
// // Output: got error: close error
// }

// func ExampleRows_rawBytes() {
// mock, err := New()
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestRowsScanError(t *testing.T) {
t.Fatal("unexpected error on second row read")
}

err = rs.Scan(&one, &two)
err = rs.Scan(&one, two)
if err == nil {
t.Fatal("expected an error for scan, but got none")
}
Expand Down

0 comments on commit 7291baa

Please sign in to comment.