Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hector.oliveros@wabtec.com authored and hector.oliveros@wabtec.com committed May 3, 2023
1 parent 857ea54 commit e15a7ab
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
7 changes: 6 additions & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ func (rs *rowSets) RawValues() [][]byte {
dest[i] = b
continue
}
dest[i] = col.([]byte)
d, ok := col.([]byte)
if ok {
dest[i] = d
} else {
dest[i] = []byte(fmt.Sprintf("%v", col))
}
}

return dest
Expand Down
53 changes: 53 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,56 @@ func TestEmptyRowSets(t *testing.T) {
// t.Fatal(err)
// }
// }

func TestMockQueryWithCollect(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())
type rowStructType struct {
ID int
Title string
}
rs := NewRows([]string{"id", "title"}).AddRow(5, "hello world")

mock.ExpectQuery("SELECT (.+) FROM articles WHERE id = ?").
WithArgs(5).
WillReturnRows(rs)

rows, err := mock.Query(context.Background(), "SELECT (.+) FROM articles WHERE id = ?", 5)
if err != nil {
t.Errorf("error '%s' was not expected while retrieving mock rows", err)
}

defer rows.Close()

//if !rows.Next() {
// t.Error("it must have had one row as result, but got empty result set instead")
//}

rawMap, err := pgx.CollectRows(rows, pgx.RowToStructByPos[rowStructType])
if err != nil {
t.Errorf("error '%s' was not expected while trying to collect rows", err)
}

var id = rawMap[0].ID
var title = rawMap[0].Title

if err != nil {
t.Errorf("error '%s' was not expected while trying to scan row", err)
}

if id != 5 {
t.Errorf("expected mocked id to be 5, but got %d instead", id)
}

if title != "hello world" {
t.Errorf("expected mocked title to be 'hello world', but got '%s' instead", title)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

0 comments on commit e15a7ab

Please sign in to comment.