From 0cc4c14e620d484d555f2955fb39cfecea89aaa3 Mon Sep 17 00:00:00 2001 From: Felix <23635466+its-felix@users.noreply.github.com> Date: Tue, 5 Mar 2024 04:07:44 +0100 Subject: [PATCH] Add test to validate CollectRows for empty Rows https://github.com/jackc/pgx/issues/1924 https://github.com/jackc/pgx/issues/1925 --- rows_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rows_test.go b/rows_test.go index 31bd8c83e..bb9d50152 100644 --- a/rows_test.go +++ b/rows_test.go @@ -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() {