Add CollectOneRow
This commit is contained in:
@@ -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`)
|
||||
|
||||
Reference in New Issue
Block a user