Skip to content

Commit

Permalink
Add test to validate CollectRows for empty Rows
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix authored and jackc committed Mar 7, 2024
1 parent da6f2c9 commit 0cc4c14
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rows_test.go
Expand Up @@ -175,6 +175,21 @@ func TestCollectRows(t *testing.T) {
})
}

func TestCollectRowsEmpty(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
rows, _ := conn.Query(ctx, `select n from generate_series(1, 0) n`)
numbers, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (int32, error) {
var n int32
err := row.Scan(&n)
return n, err
})
require.NoError(t, err)
require.NotNil(t, numbers)

assert.Empty(t, numbers)
})
}

// This example uses CollectRows with a manually written collector function. In most cases RowTo, RowToAddrOf,
// RowToStructByPos, RowToAddrOfStructByPos, or another generic function would be used.
func ExampleCollectRows() {
Expand Down

0 comments on commit 0cc4c14

Please sign in to comment.