-
Notifications
You must be signed in to change notification settings - Fork 19
/
error_test.go
56 lines (44 loc) · 1.41 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package wire
import (
"context"
"database/sql"
"errors"
"fmt"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jeroenrinzema/psql-wire/codes"
psqlerr "github.com/jeroenrinzema/psql-wire/errors"
"github.com/neilotoole/slogt"
"github.com/stretchr/testify/assert"
)
func TestErrorCode(t *testing.T) {
handler := func(ctx context.Context, query string) (PreparedStatements, error) {
stmt := NewStatement(func(ctx context.Context, writer DataWriter, parameters []Parameter) error {
return psqlerr.WithSeverity(psqlerr.WithCode(errors.New("unimplemented feature"), codes.FeatureNotSupported), psqlerr.LevelFatal)
})
return Prepared(stmt), nil
}
server, err := NewServer(handler, Logger(slogt.New(t)))
assert.NoError(t, err)
address := TListenAndServe(t, server)
t.Run("lib/pq", func(t *testing.T) {
connstr := fmt.Sprintf("host=%s port=%d sslmode=disable", address.IP, address.Port)
conn, err := sql.Open("postgres", connstr)
assert.NoError(t, err)
_, err = conn.Query("SELECT *;")
assert.Error(t, err)
err = conn.Close()
assert.NoError(t, err)
})
t.Run("jackc/pgx", func(t *testing.T) {
ctx := context.Background()
connstr := fmt.Sprintf("postgres://%s:%d", address.IP, address.Port)
conn, err := pgx.Connect(ctx, connstr)
assert.NoError(t, err)
rows, _ := conn.Query(ctx, "SELECT *;")
rows.Close()
assert.Error(t, rows.Err())
err = conn.Close(ctx)
assert.NoError(t, err)
})
}