-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
step.go
130 lines (113 loc) · 3.43 KB
/
step.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
package pgtest
import (
"fmt"
"reflect"
"github.com/jackc/pgmock"
"github.com/jackc/pgproto3/v2"
"github.com/jackc/pgtype"
)
type extendedQueryStep struct {
sql string
params [][]byte
oids []uint32
reply pgmock.Script
}
func (e *extendedQueryStep) Step(backend *pgproto3.Backend) error {
msg, err := getFrontendMessage(backend)
if err != nil {
return err
}
// Handle prepared statements, name can be dynamic: lrupsc_5_0
if m, ok := msg.(*pgproto3.Parse); ok {
want := &pgproto3.Parse{Name: m.Name, Query: e.sql, ParameterOIDs: m.ParameterOIDs}
if !reflect.DeepEqual(m, want) {
return fmt.Errorf("msg => %#v, e.want => %#v", m, want)
}
// Anonymous ps falls through
if m.Name != "" {
script := pgmock.Script{Steps: []pgmock.Step{
pgmock.ExpectMessage(&pgproto3.Describe{ObjectType: 'S', Name: m.Name}),
pgmock.ExpectMessage(&pgproto3.Sync{}),
pgmock.SendMessage(&pgproto3.ParseComplete{}),
pgmock.SendMessage(&pgproto3.ParameterDescription{ParameterOIDs: e.oids}),
// Postgres responds pgproto3.RowDescription but it's optional for pgx
pgmock.SendMessage(&pgproto3.ReadyForQuery{TxStatus: 'I'}),
}}
if err := script.Run(backend); err != nil {
return err
}
}
// Expect bind command next
msg, err = backend.Receive()
if err != nil {
return err
}
}
if m, ok := msg.(*pgproto3.Bind); ok {
var codes []int16
for range e.oids {
codes = append(codes, pgtype.TextFormatCode)
}
want := &pgproto3.Bind{
ParameterFormatCodes: codes,
Parameters: e.params,
ResultFormatCodes: []int16{},
DestinationPortal: m.DestinationPortal,
PreparedStatement: m.PreparedStatement,
}
if !reflect.DeepEqual(m, want) {
return fmt.Errorf("msg => %#v, e.want => %#v", msg, want)
}
e.reply.Steps = append([]pgmock.Step{
pgmock.ExpectMessage(&pgproto3.Describe{ObjectType: 'P'}),
pgmock.ExpectMessage(&pgproto3.Execute{}),
pgmock.SendMessage(&pgproto3.ParseComplete{}),
pgmock.SendMessage(&pgproto3.BindComplete{}),
}, e.reply.Steps...)
return e.reply.Run(backend)
}
// Handle simple query
want := &pgproto3.Query{String: e.sql}
if m, ok := msg.(*pgproto3.Query); ok && reflect.DeepEqual(m, want) {
e.reply.Steps = append(e.reply.Steps, pgmock.SendMessage(&pgproto3.ReadyForQuery{TxStatus: 'I'}))
return e.reply.Run(backend)
}
return fmt.Errorf("msg => %#v, e.want => %#v", msg, want)
}
// Expects a SQL query in any form: simple, prepared, or anonymous.
func ExpectQuery(sql string, params [][]byte, oids []uint32) pgmock.Step {
return &extendedQueryStep{sql: sql, params: params, oids: oids}
}
type terminateStep struct{}
func (e *terminateStep) Step(backend *pgproto3.Backend) error {
msg, err := getFrontendMessage(backend)
if err != nil {
return err
}
// Handle simple query
if _, ok := msg.(*pgproto3.Terminate); ok {
return nil
}
return fmt.Errorf("msg => %#v, e.want => %#v", msg, &pgproto3.Terminate{})
}
func ExpectTerminate() pgmock.Step {
return &terminateStep{}
}
func getFrontendMessage(backend *pgproto3.Backend) (pgproto3.FrontendMessage, error) {
msg, err := backend.Receive()
if err != nil {
return nil, err
}
// Sync signals end of batch statements
if _, ok := msg.(*pgproto3.Sync); ok {
reply := pgmock.SendMessage(&pgproto3.ReadyForQuery{TxStatus: 'I'})
if err := reply.Step(backend); err != nil {
return nil, err
}
msg, err = backend.Receive()
if err != nil {
return nil, err
}
}
return msg, nil
}