-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsqlutil_test.go
More file actions
396 lines (348 loc) · 7.33 KB
/
Copy pathsqlutil_test.go
File metadata and controls
396 lines (348 loc) · 7.33 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package sqlutil
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
t.Parallel()
tests := []struct {
name string
opts []Option
wantErrIs error
}{
{
name: "succeeds with defaults",
wantErrIs: nil,
},
{
name: "fails with nil quoteIDFunc",
opts: []Option{WithQuoteIDFunc(nil)},
wantErrIs: ErrNilQuoteIDFunc,
},
{
name: "fails with nil quoteValueFunc",
opts: []Option{WithQuoteValueFunc(nil)},
wantErrIs: ErrNilQuoteValueFunc,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := New(tt.opts...)
if tt.wantErrIs == nil {
require.NoError(t, err)
return
}
require.ErrorIs(t, err, tt.wantErrIs)
})
}
}
func Test_defaultSQLUtil(t *testing.T) {
t.Parallel()
c := defaultSQLUtil()
require.NotNil(t, c.quoteIDFunc)
require.NotNil(t, c.quoteValueFunc)
}
func Test_validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
c *SQLUtil
wantErrIs error
}{
{
name: "fail with invalid quoteIDFunc function",
c: func() *SQLUtil {
c := defaultSQLUtil()
c.quoteIDFunc = nil
return c
}(),
wantErrIs: ErrNilQuoteIDFunc,
},
{
name: "fail with invalid quoteValueFunc function",
c: func() *SQLUtil {
c := defaultSQLUtil()
c.quoteValueFunc = nil
return c
}(),
wantErrIs: ErrNilQuoteValueFunc,
},
{
name: "succeed with no errors",
c: func() *SQLUtil {
c := defaultSQLUtil()
return c
}(),
wantErrIs: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.c.validate()
if tt.wantErrIs == nil {
require.NoError(t, err)
return
}
require.ErrorIs(t, err, tt.wantErrIs)
})
}
}
func Test_defaultQuoteID(t *testing.T) {
t.Parallel()
tests := []struct {
name string
id string
want string
}{
{
name: "empty string",
id: "",
want: "",
},
{
name: "single value",
id: "test1",
want: "`test1`",
},
{
name: "two parts",
id: "parent.child",
want: "`parent`.`child`",
},
{
name: "multiple parts",
id: "parent.child.name",
want: "`parent`.`child`.`name`",
},
{
name: "multiple parts with space",
id: "one two.three four",
want: "`one two`.`three four`",
},
{
name: "escape backtick",
id: "test`4",
want: "`test``4`",
},
{
name: "escape multiple backtick",
id: "test```4",
want: "`test``````4`",
},
{
name: "backslash kept literal",
id: `a\b`,
want: "`a\\b`",
},
{
name: "special characters",
id: "test" + string([]byte{'\'', '"', '`', 0, '\n', '\r', '\\', '\032'}),
want: "`test'\"``" + string([]byte{0, '\n', '\r', '\\', '\032'}) + "`",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := defaultQuoteID(tt.id)
require.Equal(t, tt.want, got, "QuoteID() got = %v, want %v", got, tt.want)
})
}
}
func Test_defaultQuoteValue(t *testing.T) {
t.Parallel()
tests := []struct {
name string
val string
want string
}{
{
name: "empty string is quoted",
val: "",
want: "''",
},
{
name: "simple value",
val: "test1",
want: "'test1'",
},
{
name: "value with spaces",
val: "one two three",
want: "'one two three'",
},
{
name: "escape single quote",
val: "test'2",
want: "'test''2'",
},
{
name: "escape multiple quotes",
val: "test'''2",
want: "'test''''''2'",
},
{
name: "special characters",
val: "test" + string([]byte{'\'', '"', '`', 0, '\n', '\r', '\\', '\032'}),
want: "'test''\"`\\0\\n\\r\\\\\\Z'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := defaultQuoteValue(tt.val)
require.Equal(t, tt.want, got, "QuoteID() got = %v, want %v", got, tt.want)
})
}
}
// Test_QuoteValue_Escaping exercises the default escaping behavior through the
// exported QuoteValue entry point, covering single quotes, backticks and every
// control character the escaper handles (\0, \n, \r, \\, \032/\Z).
func Test_QuoteValue_Escaping(t *testing.T) {
t.Parallel()
tests := []struct {
name string
val string
want string
}{
{
name: "empty string is quoted",
val: "",
want: "''",
},
{
name: "single quote doubled",
val: "o'reilly",
want: "'o''reilly'",
},
{
name: "backtick left untouched in value",
val: "a`b",
want: "'a`b'",
},
{
name: "null byte escaped",
val: "a" + string([]byte{0}) + "b",
want: `'a\0b'`,
},
{
name: "newline escaped",
val: "a\nb",
want: `'a\nb'`,
},
{
name: "carriage return escaped",
val: "a\rb",
want: `'a\rb'`,
},
{
name: "backslash escaped",
val: `a\b`,
want: `'a\\b'`,
},
{
name: "substitute (\\032) escaped to Z",
val: "a\032b",
want: `'a\Zb'`,
},
{
name: "all control characters and quote together",
val: string([]byte{'\'', 0, '\n', '\r', '\\', '\032', '`'}),
want: "'''\\0\\n\\r\\\\\\Z`'",
},
}
c, err := New()
require.NoError(t, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := c.QuoteValue(tt.val)
require.Equal(t, tt.want, got, "QuoteValue() got = %v, want %v", got, tt.want)
})
}
}
// Test_QuoteID_Escaping exercises the default quoting behavior through the
// exported QuoteID entry point: inside backtick quotes only the backtick is
// special (doubled); backslashes and control characters are kept literal.
func Test_QuoteID_Escaping(t *testing.T) {
t.Parallel()
tests := []struct {
name string
id string
want string
}{
{
name: "embedded backtick doubled",
id: "col`umn",
want: "`col``umn`",
},
{
name: "single quote left untouched in identifier",
id: "col'umn",
want: "`col'umn`",
},
{
name: "dotted identifier with backtick per segment",
id: "sch`ema.ta`ble",
want: "`sch``ema`.`ta``ble`",
},
{
name: "null byte kept literal",
id: "a" + string([]byte{0}) + "b",
want: "`a" + string([]byte{0}) + "b`",
},
{
name: "newline kept literal",
id: "a\nb",
want: "`a\nb`",
},
{
name: "carriage return kept literal",
id: "a\rb",
want: "`a\rb`",
},
{
name: "backslash kept literal",
id: `a\b`,
want: "`a\\b`",
},
{
name: "substitute (\\032) kept literal",
id: "a\032b",
want: "`a\032b`",
},
{
name: "all control characters kept literal and backtick doubled",
id: string([]byte{0, '\n', '\r', '\\', '\032', '`'}),
want: "`" + string([]byte{0, '\n', '\r', '\\', '\032'}) + "```",
},
}
c, err := New()
require.NoError(t, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := c.QuoteID(tt.id)
require.Equal(t, tt.want, got, "QuoteID() got = %v, want %v", got, tt.want)
})
}
}
func Test_QuoteID(t *testing.T) {
t.Parallel()
fn := func(s string) string { return "TEST" + s }
c := &SQLUtil{quoteIDFunc: fn}
s := "5237"
got := c.QuoteID(s)
want := fn(s)
require.Equal(t, want, got, "QuoteID() got = %v, want %v", got, want)
}
func Test_QuoteValue(t *testing.T) {
t.Parallel()
fn := func(s string) string { return "TEST" + s }
c := &SQLUtil{quoteValueFunc: fn}
s := "5237"
got := c.QuoteValue(s)
want := fn(s)
require.Equal(t, want, got, "QuoteValue() got = %v, want %v", got, want)
}