2
0

Add CollectOneRow

This commit is contained in:
Jack Christensen
2022-07-09 16:59:29 -05:00
parent 90c2dc6f68
commit 62f0347586
3 changed files with 63 additions and 0 deletions
+41
View File
@@ -147,6 +147,47 @@ func TestCollectRows(t *testing.T) {
})
}
func TestCollectOneRow(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
rows, _ := conn.Query(ctx, `select 42`)
n, err := pgx.CollectOneRow(rows, func(row pgx.CollectableRow) (int32, error) {
var n int32
err := row.Scan(&n)
return n, err
})
assert.NoError(t, err)
assert.Equal(t, int32(42), n)
})
}
func TestCollectOneRowNotFound(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
rows, _ := conn.Query(ctx, `select 42 where false`)
n, err := pgx.CollectOneRow(rows, func(row pgx.CollectableRow) (int32, error) {
var n int32
err := row.Scan(&n)
return n, err
})
assert.ErrorIs(t, err, pgx.ErrNoRows)
assert.Equal(t, int32(0), n)
})
}
func TestCollectOneRowIgnoresExtraRows(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(42, 99) n`)
n, err := pgx.CollectOneRow(rows, func(row pgx.CollectableRow) (int32, error) {
var n int32
err := row.Scan(&n)
return n, err
})
require.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, int32(42), n)
})
}
func TestRowTo(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(0, 99) n`)