-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder_test.go
220 lines (207 loc) · 6.23 KB
/
builder_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package pg_stories
import (
"encoding/binary"
"fmt"
"github.com/jackc/pgx/pgproto3"
"math"
"strings"
"testing"
)
func createBuilder(name string, steps ...string) *Builder {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("=== %s\n", name))
for _, step := range steps {
sb.WriteString(step + "\n")
}
sb.WriteString(`===` + "\n")
return NewBuilder(strings.NewReader(sb.String()))
}
func TestBuilder_ParseNext(t *testing.T) {
t.Run("test empty", func(t *testing.T) {
builder := createBuilder(t.Name())
_, _, err := builder.ParseNext()
if err == nil {
t.Fatalf("expected error")
}
switch err.(type) {
case *EmptyStoryError:
break
default:
t.Fatalf("expected: EmptyStoryError. got: %T", err)
}
})
t.Run("test query", func(t *testing.T) {
builder := createBuilder(t.Name(), `-> Q "baaa"`)
story, _, err := builder.ParseNext()
if err != nil {
t.Fatal(err)
}
if story == nil {
t.Fatal("story is nil")
}
switch story.Steps[0].(type) {
case *Command:
cmd := story.Steps[0].(*Command)
switch cmd.FrontendMessage.(type) {
case *pgproto3.Query:
msg := cmd.FrontendMessage.(*pgproto3.Query)
if msg.String != "baaa" {
t.Fatalf("expected query content to be baaa. actual: %s", msg.String)
}
default:
t.Fatalf("expected first step to be a command of type %T. actual: %T", &pgproto3.Query{}, cmd.FrontendMessage)
}
default:
t.Fatalf("expected first step to be a command. actual: %T", story.Steps[0])
}
})
t.Run("test unnamed parse", func(t *testing.T) {
builder := createBuilder(t.Name(), `-> P "" "baaa" [1]`)
story, _, err := builder.ParseNext()
if err != nil {
t.Fatal(err)
}
if story == nil {
t.Fatal("story is nil")
}
switch story.Steps[0].(type) {
case *Command:
cmd := story.Steps[0].(*Command)
switch cmd.FrontendMessage.(type) {
case *pgproto3.Parse:
msg := cmd.FrontendMessage.(*pgproto3.Parse)
if msg.Query != "baaa" {
t.Fatalf("expected query to be baaa. actual: %s", msg.Query)
}
if len(msg.ParameterOIDs) != 1 {
t.Fatalf("expected ParameterOIDs to be 1 element long. actual: %d", len(msg.ParameterOIDs))
}
if msg.ParameterOIDs[0] != 1 {
t.Fatalf("expected ParameterOIDs' first element to equal 1. actual: %d", msg.ParameterOIDs[0])
}
default:
t.Fatalf("expected first step to be a command of type %T. actual: %T", &pgproto3.Query{}, cmd.FrontendMessage)
}
default:
t.Fatalf("expected first step to be a command. actual: %T", story.Steps[0])
}
})
t.Run("test named parse", func(t *testing.T) {
builder := createBuilder(t.Name(), `-> P "stmt_name" "baaa" []`)
story, _, err := builder.ParseNext()
if err != nil {
t.Fatal(err)
}
if story == nil {
t.Fatal("story is nil")
}
switch story.Steps[0].(type) {
case *Command:
cmd := story.Steps[0].(*Command)
switch cmd.FrontendMessage.(type) {
case *pgproto3.Parse:
msg := cmd.FrontendMessage.(*pgproto3.Parse)
if msg.Name != "stmt_name" {
t.Fatalf("expected stmt name to be 'stmt_name'. actual: %s", msg.Name)
}
default:
t.Fatalf("expected first step to be a command of type %T. actual: %T", &pgproto3.Query{}, cmd.FrontendMessage)
}
default:
t.Fatalf("expected first step to be a command. actual: %T", story.Steps[0])
}
})
t.Run("test bind", func(t *testing.T) {
builder := createBuilder(t.Name(), `-> B "" "stmt_name" [1,baa,1.2]`)
story, _, err := builder.ParseNext()
if err != nil {
t.Fatal(err)
}
if story == nil {
t.Fatal("story is nil")
}
switch story.Steps[0].(type) {
case *Command:
cmd := story.Steps[0].(*Command)
switch cmd.FrontendMessage.(type) {
case *pgproto3.Bind:
msg := cmd.FrontendMessage.(*pgproto3.Bind)
if msg.PreparedStatement != "stmt_name" {
t.Fatalf("expected stmt name to be 'stmt_name'. actual: %s", msg.PreparedStatement)
}
i := math.Float64frombits(binary.LittleEndian.Uint64(msg.Parameters[0]))
if i != 1 {
t.Fatalf("expected parameter 0 to equal 1. actual %f", i)
}
s := string(msg.Parameters[1])
if s != "baa" {
t.Fatalf("expected parameter 1 to equal baa. actual %s", s)
}
f := math.Float64frombits(binary.LittleEndian.Uint64(msg.Parameters[2]))
if f != 1.2 {
t.Fatalf("expected parameter 2 to equal 1.2. actual %f", f)
}
default:
t.Fatalf("expected first step to be a command of type %T. actual: %T", &pgproto3.Query{}, cmd.FrontendMessage)
}
default:
t.Fatalf("expected first step to be a command. actual: %T", story.Steps[0])
}
})
t.Run("test execute", func(t *testing.T) {
builder := createBuilder(t.Name(), `-> E "portal_name" 10`)
story, _, err := builder.ParseNext()
if err != nil {
t.Fatal(err)
}
if story == nil {
t.Fatal("story is nil")
}
switch story.Steps[0].(type) {
case *Command:
cmd := story.Steps[0].(*Command)
switch cmd.FrontendMessage.(type) {
case *pgproto3.Execute:
msg := cmd.FrontendMessage.(*pgproto3.Execute)
if msg.Portal != "portal_name" {
t.Fatalf("expected portal name to be 'portal_name'. actual: %s", msg.Portal)
}
if msg.MaxRows != 10 {
t.Fatalf("expected max rows to be 10. actual: %d", msg.MaxRows)
}
default:
t.Fatalf("expected first step to be a command of type %T. actual: %T", &pgproto3.Query{}, cmd.FrontendMessage)
}
default:
t.Fatalf("expected first step to be a command. actual: %T", story.Steps[0])
}
})
t.Run("test describe", func(t *testing.T) {
builder := createBuilder(t.Name(), `-> D S "baa"`)
story, _, err := builder.ParseNext()
if err != nil {
t.Fatal(err)
}
if story == nil {
t.Fatal("story is nil")
}
switch story.Steps[0].(type) {
case *Command:
cmd := story.Steps[0].(*Command)
switch cmd.FrontendMessage.(type) {
case *pgproto3.Describe:
msg := cmd.FrontendMessage.(*pgproto3.Describe)
if msg.ObjectType != 'S' {
t.Fatalf("expected object type be 'S'. actual: %c", msg.ObjectType)
}
if msg.Name != "baa" {
t.Fatalf("expected stmt name to be 'baa'. actual: %s", msg.Name)
}
default:
t.Fatalf("expected first step to be a command of type %T. actual: %T", &pgproto3.Query{}, cmd.FrontendMessage)
}
default:
t.Fatalf("expected first step to be a command. actual: %T", story.Steps[0])
}
})
}