-
Notifications
You must be signed in to change notification settings - Fork 19
/
command_test.go
133 lines (103 loc) · 2.72 KB
/
command_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package wire
import (
"context"
"fmt"
"net"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jeroenrinzema/psql-wire/pkg/buffer"
"github.com/jeroenrinzema/psql-wire/pkg/mock"
"github.com/jeroenrinzema/psql-wire/pkg/types"
"github.com/lib/pq/oid"
"github.com/neilotoole/slogt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMessageSizeExceeded(t *testing.T) {
server, err := NewServer(nil, Logger(slogt.New(t)))
if err != nil {
t.Fatal(err)
}
address := TListenAndServe(t, server)
conn, err := net.Dial("tcp", address.String())
if err != nil {
t.Fatal(err)
}
client := mock.NewClient(t, conn)
client.Handshake(t)
client.Authenticate(t)
client.ReadyForQuery(t)
// NOTE: attempt to send a message twice the max buffer size
size := uint32(buffer.DefaultBufferSize * 2)
t.Logf("writing message of size: %d", size)
client.Start(types.ClientSimpleQuery)
client.AddBytes(make([]byte, size))
err = client.End()
if err != nil {
t.Fatal(err)
}
client.Error(t)
client.Close(t)
}
func TestBindMessageParameters(t *testing.T) {
t.Parallel()
columns := Columns{
{
Table: 0,
Name: "full_name",
Oid: oid.T_text,
Width: 256,
},
{
Table: 0,
Name: "answer_to_life_the_universe_and_everything",
Oid: oid.T_text,
Width: 256,
},
}
handler := func(ctx context.Context, query string) (PreparedStatements, error) {
handle := func(ctx context.Context, writer DataWriter, parameters []Parameter) error {
t.Log("serving query")
if len(parameters) != 2 {
return fmt.Errorf("unexpected amount of parameters %d, expected 2", len(parameters))
}
first := string(parameters[0].value)
second := string(parameters[1].value)
writer.Row([]any{first, second}) //nolint:errcheck
return writer.Complete("SELECT 1")
}
return Prepared(NewStatement(handle, WithColumns(columns), WithParameters(ParseParameters(query)))), nil
}
server, err := NewServer(handler, Logger(slogt.New(t)))
if err != nil {
t.Fatal(err)
}
address := TListenAndServe(t, server)
ctx := context.Background()
connstr := fmt.Sprintf("postgres://%s:%d", address.IP, address.Port)
t.Run("pgx", func(t *testing.T) {
conn, err := pgx.Connect(ctx, connstr)
if err != nil {
t.Fatal(err)
}
defer conn.Close(ctx)
rows, err := conn.Query(ctx, "SELECT $1 $2;", "John Doe", 42)
if err != nil {
t.Fatal(err)
}
assert.True(t, rows.Next())
var name string
var answer string
err = rows.Scan(&name, &answer)
require.NoError(t, err)
t.Logf("scan result: %s, %s", name, answer)
assert.Equal(t, name, "John Doe")
assert.Equal(t, answer, "42")
assert.False(t, rows.Next())
rows.Close()
err = conn.Close(ctx)
if err != nil {
t.Fatal(err)
}
})
}