-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathnamed_args_test.go
More file actions
350 lines (334 loc) · 8.58 KB
/
named_args_test.go
File metadata and controls
350 lines (334 loc) · 8.58 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package pgx_test
import (
"context"
"testing"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNamedArgsRewriteQuery(t *testing.T) {
t.Parallel()
for i, tt := range []struct {
sql string
args []any
namedArgs pgx.NamedArgs
expectedSQL string
expectedArgs []any
}{
{
sql: "select * from users where id = @id",
namedArgs: pgx.NamedArgs{"id": int32(42)},
expectedSQL: "select * from users where id = $1",
expectedArgs: []any{int32(42)},
},
{
sql: "select * from t where foo < @abc and baz = @def and bar < @abc",
namedArgs: pgx.NamedArgs{"abc": int32(42), "def": int32(1)},
expectedSQL: "select * from t where foo < $1 and baz = $2 and bar < $1",
expectedArgs: []any{int32(42), int32(1)},
},
{
sql: "select @a::int, @b::text",
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
expectedSQL: "select $1::int, $2::text",
expectedArgs: []any{int32(42), "foo"},
},
{
sql: "select @Abc::int, @b_4::text, @_c::int",
namedArgs: pgx.NamedArgs{"Abc": int32(42), "b_4": "foo", "_c": int32(1)},
expectedSQL: "select $1::int, $2::text, $3::int",
expectedArgs: []any{int32(42), "foo", int32(1)},
},
{
sql: "at end @",
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
expectedSQL: "at end @",
expectedArgs: []any{},
},
{
sql: "ignores without valid character after @ foo bar",
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
expectedSQL: "ignores without valid character after @ foo bar",
expectedArgs: []any{},
},
{
sql: "name cannot start with number @1 foo bar",
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
expectedSQL: "name cannot start with number @1 foo bar",
expectedArgs: []any{},
},
{
sql: `select *, '@foo' as "@bar" from users where id = @id`,
namedArgs: pgx.NamedArgs{"id": int32(42)},
expectedSQL: `select *, '@foo' as "@bar" from users where id = $1`,
expectedArgs: []any{int32(42)},
},
{
sql: `select * -- @foo
from users -- @single line comments
where id = @id;`,
namedArgs: pgx.NamedArgs{"id": int32(42)},
expectedSQL: `select * -- @foo
from users -- @single line comments
where id = $1;`,
expectedArgs: []any{int32(42)},
},
{
sql: `select * /* @multi line
@comment
*/
/* /* with @nesting */ */
from users
where id = @id;`,
namedArgs: pgx.NamedArgs{"id": int32(42)},
expectedSQL: `select * /* @multi line
@comment
*/
/* /* with @nesting */ */
from users
where id = $1;`,
expectedArgs: []any{int32(42)},
},
{
sql: "extra provided argument",
namedArgs: pgx.NamedArgs{"extra": int32(1)},
expectedSQL: "extra provided argument",
expectedArgs: []any{},
},
{
sql: "@missing argument",
namedArgs: pgx.NamedArgs{},
expectedSQL: "$1 argument",
expectedArgs: []any{nil},
},
// test comments and quotes
} {
sql, args, err := tt.namedArgs.RewriteQuery(context.Background(), nil, tt.sql, tt.args)
require.NoError(t, err)
assert.Equalf(t, tt.expectedSQL, sql, "%d", i)
assert.Equalf(t, tt.expectedArgs, args, "%d", i)
}
}
func TestStrictNamedArgsRewriteQuery(t *testing.T) {
t.Parallel()
for i, tt := range []struct {
sql string
namedArgs pgx.StrictNamedArgs
expectedSQL string
expectedArgs []any
isExpectedError bool
}{
{
sql: "no arguments",
namedArgs: pgx.StrictNamedArgs{},
expectedSQL: "no arguments",
expectedArgs: []any{},
isExpectedError: false,
},
{
sql: "@all @matches",
namedArgs: pgx.StrictNamedArgs{"all": int32(1), "matches": int32(2)},
expectedSQL: "$1 $2",
expectedArgs: []any{int32(1), int32(2)},
isExpectedError: false,
},
{
sql: "extra provided argument",
namedArgs: pgx.StrictNamedArgs{"extra": int32(1)},
isExpectedError: true,
},
{
sql: "@missing argument",
namedArgs: pgx.StrictNamedArgs{},
isExpectedError: true,
},
} {
sql, args, err := tt.namedArgs.RewriteQuery(context.Background(), nil, tt.sql, nil)
if tt.isExpectedError {
assert.Errorf(t, err, "%d", i)
} else {
require.NoErrorf(t, err, "%d", i)
assert.Equalf(t, tt.expectedSQL, sql, "%d", i)
assert.Equalf(t, tt.expectedArgs, args, "%d", i)
}
}
}
func TestStructArgs(t *testing.T) {
t.Parallel()
for _, tt := range []struct {
name string
input any
sql string
expectedSQL string
expectedArgs []any
expectError bool
}{
{
name: "basic",
input: struct {
ID int `db:"id"`
Name string `db:"name,omitempty"`
Skip string `db:"-"`
}{ID: 42, Name: "x", Skip: "ignored"},
sql: "select * from t where id=@id and name=@name",
expectedSQL: "select * from t where id=$1 and name=$2",
expectedArgs: []any{42, "x"},
},
{
name: "pointer",
input: func() any {
type S struct {
ID int `db:"id"`
}
return &S{ID: 7}
}(),
sql: "select * from t where id=@id",
expectedSQL: "select * from t where id=$1",
expectedArgs: []any{7},
},
{
name: "unexported fields omitted (missing placeholders become nil)",
input: struct {
id int `db:"id"`
ID int `db:"ID"`
}{id: 1, ID: 2},
sql: "select * from t where ID=@ID and id=@id",
expectedSQL: "select * from t where ID=$1 and id=$2",
expectedArgs: []any{2, nil},
},
{
name: "missing db tag falls back to field name",
input: struct {
ID int
}{ID: 9},
sql: "select * from t where ID=@ID",
expectedSQL: "select * from t where ID=$1",
expectedArgs: []any{9},
},
{
name: "duplicate keys error",
input: struct {
A int `db:"x"`
B int `db:"x"`
}{A: 1, B: 2},
sql: "select * from t where x=@x",
expectError: true,
},
{
name: "nil pointer returns error",
input: func() any {
type S struct {
ID int `db:"id"`
}
var s *S
return s
}(),
sql: "select * from t where id=@id",
expectError: true,
},
{
name: "non struct returns error",
input: 42,
sql: "select * from t where id=@id",
expectError: true,
},
{
name: "nil input returns error",
input: nil,
sql: "select * from t where id=@id",
expectError: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
qr := pgx.StructArgs(tt.input)
sql, args, err := qr.RewriteQuery(context.Background(), nil, tt.sql, nil)
if tt.expectError {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.expectedSQL, sql)
assert.EqualValues(t, tt.expectedArgs, args)
})
}
}
func TestStrictStructArgs(t *testing.T) {
t.Parallel()
type MyInt int
for _, tt := range []struct {
name string
input any
sql string
expectedSQL string
expectedArgs []any
expectError bool
}{
{
name: "fallback to field name without db tag",
input: struct {
ID int
}{ID: 1},
sql: "select * from t where ID=@ID",
expectedSQL: "select * from t where ID=$1",
expectedArgs: []any{1},
},
{
name: "empty db tag errors",
input: struct {
ID int `db:","`
}{ID: 1},
sql: "select * from t where ID=@ID",
expectError: true,
},
{
name: "duplicate keys error",
input: struct {
A int `db:"x"`
B int `db:"x"`
}{A: 1, B: 2},
sql: "select * from t where x=@x",
expectError: true,
},
{
name: "skips anonymous embedded structs without flattening",
input: func() any {
type Embedded struct {
ID int `db:"id"`
}
type S struct {
Embedded
Name string `db:"name"`
}
return S{Embedded: Embedded{ID: 1}, Name: "x"}
}(),
sql: "select * from t where name=@name and id=@id",
expectError: true,
},
{
name: "anonymous embedded non-struct still requires tag in strict mode",
input: func() any {
type S struct {
MyInt
}
return S{MyInt: 1}
}(),
sql: "select * from t where MyInt=@MyInt",
expectedSQL: "select * from t where MyInt=$1",
expectedArgs: []any{MyInt(1)},
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
qr := pgx.StrictStructArgs(tt.input)
sql, args, err := qr.RewriteQuery(context.Background(), nil, tt.sql, nil)
if tt.expectError {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.expectedSQL, sql)
assert.EqualValues(t, tt.expectedArgs, args)
})
}
}