Skip to content

Commit 0cc4c14

Browse files
its-felixjackc
authored andcommitted
Add test to validate CollectRows for empty Rows
#1924 #1925
1 parent da6f2c9 commit 0cc4c14

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

rows_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ func TestCollectRows(t *testing.T) {
175175
})
176176
}
177177

178+
func TestCollectRowsEmpty(t *testing.T) {
179+
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
180+
rows, _ := conn.Query(ctx, `select n from generate_series(1, 0) n`)
181+
numbers, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (int32, error) {
182+
var n int32
183+
err := row.Scan(&n)
184+
return n, err
185+
})
186+
require.NoError(t, err)
187+
require.NotNil(t, numbers)
188+
189+
assert.Empty(t, numbers)
190+
})
191+
}
192+
178193
// This example uses CollectRows with a manually written collector function. In most cases RowTo, RowToAddrOf,
179194
// RowToStructByPos, RowToAddrOfStructByPos, or another generic function would be used.
180195
func ExampleCollectRows() {

0 commit comments

Comments
 (0)