-
Notifications
You must be signed in to change notification settings - Fork 8
/
misc_test.go
395 lines (371 loc) · 9.94 KB
/
misc_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
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
package sq
import (
"bytes"
"context"
"testing"
"time"
"github.com/bokwoon95/sq/internal/testutil"
)
func TestValueExpression(t *testing.T) {
t.Run("alias", func(t *testing.T) {
t.Parallel()
expr := Value(1).As("num")
if diff := testutil.Diff(expr.GetAlias(), "num"); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
tests := []TestTable{{
description: "basic",
item: Value(Param("xyz", 42)),
wantQuery: "?",
wantArgs: []any{42},
wantParams: map[string][]int{"xyz": {0}},
}, {
description: "In", item: Value(1).In([]int{18, 21, 32}),
wantQuery: "? IN (?, ?, ?)", wantArgs: []any{1, 18, 21, 32},
}, {
description: "Eq", item: Value(1).Eq(34),
wantQuery: "? = ?", wantArgs: []any{1, 34},
}, {
description: "Ne", item: Value(1).Ne(34),
wantQuery: "? <> ?", wantArgs: []any{1, 34},
}, {
description: "Lt", item: Value(1).Lt(34),
wantQuery: "? < ?", wantArgs: []any{1, 34},
}, {
description: "Le", item: Value(1).Le(34),
wantQuery: "? <= ?", wantArgs: []any{1, 34},
}, {
description: "Gt", item: Value(1).Gt(34),
wantQuery: "? > ?", wantArgs: []any{1, 34},
}, {
description: "Ge", item: Value(1).Ge(34),
wantQuery: "? >= ?", wantArgs: []any{1, 34},
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
tt.assert(t)
})
}
}
func TestLiteralExpression(t *testing.T) {
t.Run("alias", func(t *testing.T) {
t.Parallel()
expr := Literal(1).As("num")
if diff := testutil.Diff(expr.GetAlias(), "num"); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
tests := []TestTable{{
description: "binary",
item: Literal([]byte{0xab, 0xcd, 0xef}),
wantQuery: "x'abcdef'",
}, {
description: "time", item: Literal(time.Unix(0, 0).UTC()),
wantQuery: "'1970-01-01 00:00:00'",
}, {
description: "In", item: Literal(1).In([]any{Literal(18), Literal(21), Literal(32)}),
wantQuery: "1 IN (18, 21, 32)",
}, {
description: "Eq", item: Literal(true).Eq(Literal(false)),
wantQuery: "TRUE = FALSE",
}, {
description: "Ne", item: Literal("one").Ne(Literal("thirty four")),
wantQuery: "'one' <> 'thirty four'",
}, {
description: "Lt", item: Literal(1).Lt(Literal(34)),
wantQuery: "1 < 34",
}, {
description: "Le", item: Literal(1).Le(Literal(34)),
wantQuery: "1 <= 34",
}, {
description: "Gt", item: Literal(1).Gt(Literal(34)),
wantQuery: "1 > 34",
}, {
description: "Ge", item: Literal(1).Ge(Literal(34)),
wantQuery: "1 >= 34",
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
tt.assert(t)
})
}
}
func TestDialectExpression(t *testing.T) {
t.Parallel()
expr := DialectValue(Expr("default")).
DialectValue(DialectSQLite, Expr("sqlite")).
DialectValue(DialectPostgres, Expr("postgres")).
DialectValue(DialectMySQL, Expr("mysql")).
DialectExpr(DialectSQLServer, "{}", Expr("sqlserver"))
var tt TestTable
tt.item = expr
// default
tt.wantQuery = "default"
tt.assert(t)
// sqlite
tt.dialect = DialectSQLite
tt.wantQuery = "sqlite"
tt.assert(t)
// postgres
tt.dialect = DialectPostgres
tt.wantQuery = "postgres"
tt.assert(t)
// mysql
tt.dialect = DialectMySQL
tt.wantQuery = "mysql"
tt.assert(t)
// sqlserver
tt.dialect = DialectSQLServer
tt.wantQuery = "sqlserver"
tt.assert(t)
}
func TestCaseExpressions(t *testing.T) {
t.Run("name and alias", func(t *testing.T) {
t.Parallel()
// CaseExpression
caseExpr := CaseWhen(Value(true), 1).As("result_a")
if diff := testutil.Diff(caseExpr.GetAlias(), "result_a"); diff != "" {
t.Error(testutil.Callers(), diff)
}
// SimpleCaseExpression
simpleCaseExpr := Case(1).When(1, 2).As("result_b")
if diff := testutil.Diff(simpleCaseExpr.GetAlias(), "result_b"); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
t.Run("CaseExpression", func(t *testing.T) {
t.Parallel()
TestTable{
item: CaseWhen(Expr("x = y"), 1).When(Expr("a = b"), 2).Else(3),
wantQuery: "CASE WHEN x = y THEN ? WHEN a = b THEN ? ELSE ? END",
wantArgs: []any{1, 2, 3},
}.assert(t)
})
t.Run("SimpleCaseExpression", func(t *testing.T) {
t.Parallel()
TestTable{
item: Case(Expr("a")).When(1, 2).When(3, 4).Else(5),
wantQuery: "CASE a WHEN ? THEN ? WHEN ? THEN ? ELSE ? END",
wantArgs: []any{1, 2, 3, 4, 5},
}.assert(t)
})
t.Run("CaseExpression cannot be empty", func(t *testing.T) {
t.Parallel()
TestTable{item: CaseExpression{}}.assertNotOK(t)
})
t.Run("SimpleCaseExpression cannot be empty", func(t *testing.T) {
t.Parallel()
TestTable{item: SimpleCaseExpression{}}.assertNotOK(t)
})
errTests := []TestTable{{
description: "CASE WHEN predicate err", item: CaseWhen(FaultySQL{}, 1),
}, {
description: "CASE WHEN result err", item: CaseWhen(Value(true), FaultySQL{}),
}, {
description: "CASE WHEN fallback err", item: CaseWhen(Value(true), 1).Else(FaultySQL{}),
}, {
description: "CASE expression err", item: Case(FaultySQL{}).When(1, 2),
}, {
description: "CASE value err", item: Case(1).When(FaultySQL{}, 2),
}, {
description: "CASE result err", item: Case(1).When(2, FaultySQL{}),
}, {
description: "CASE fallback err", item: Case(1).When(2, 3).Else(FaultySQL{}),
}}
for _, tt := range errTests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
tt.assertErr(t, ErrFaultySQL)
})
}
}
func TestSelectValues(t *testing.T) {
type TestTable struct {
description string
dialect string
item SelectValues
wantQuery string
wantArgs []any
}
t.Run("dialect alias and fields", func(t *testing.T) {
selectValues := SelectValues{
Alias: "aaa",
}
if diff := testutil.Diff(selectValues.GetAlias(), "aaa"); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(selectValues.GetDialect(), ""); diff != "" {
t.Error(testutil.Callers(), diff)
}
_, ok := selectValues.SetFetchableFields(nil)
if diff := testutil.Diff(ok, false); diff != "" {
t.Error(testutil.Callers(), diff)
}
gotField, _, _ := ToSQL("", selectValues.Field("bbb"), nil)
if diff := testutil.Diff(gotField, "aaa.bbb"); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
tests := []TestTable{{
description: "empty",
item: SelectValues{},
wantQuery: "",
wantArgs: nil,
}, {
description: "no columns",
item: SelectValues{
RowValues: [][]any{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
},
wantQuery: "SELECT ?, ?, ?" +
" UNION ALL SELECT ?, ?, ?" +
" UNION ALL SELECT ?, ?, ?",
wantArgs: []any{1, 2, 3, 4, 5, 6, 7, 8, 9},
}, {
description: "postgres",
dialect: DialectPostgres,
item: SelectValues{
Columns: []string{"a", "b", "c"},
RowValues: [][]any{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
},
wantQuery: "SELECT $1 AS a, $2 AS b, $3 AS c" +
" UNION ALL SELECT $4, $5, $6" +
" UNION ALL SELECT $7, $8, $9",
wantArgs: []any{1, 2, 3, 4, 5, 6, 7, 8, 9},
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
var gotArgs []any
err := tt.item.WriteSQL(context.Background(), tt.dialect, &buf, &gotArgs, nil)
if err != nil {
t.Fatal(testutil.Callers(), err)
}
gotQuery := buf.String()
if diff := testutil.Diff(gotQuery, tt.wantQuery); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(gotArgs, tt.wantArgs); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
}
}
func TestTableValues(t *testing.T) {
type TestTable struct {
description string
dialect string
item TableValues
wantQuery string
wantArgs []any
}
t.Run("dialect alias columns and fields", func(t *testing.T) {
tableValues := TableValues{
Alias: "aaa",
Columns: []string{"a", "b", "c"},
}
if diff := testutil.Diff(tableValues.GetAlias(), "aaa"); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(tableValues.GetDialect(), ""); diff != "" {
t.Error(testutil.Callers(), diff)
}
_, ok := tableValues.SetFetchableFields(nil)
if diff := testutil.Diff(ok, false); diff != "" {
t.Error(testutil.Callers(), diff)
}
gotColumns := tableValues.GetColumns()
wantColumns := []string{"a", "b", "c"}
if diff := testutil.Diff(gotColumns, wantColumns); diff != "" {
t.Error(testutil.Callers(), diff)
}
gotField, _, _ := ToSQL("", tableValues.Field("bbb"), nil)
wantField := "aaa.bbb"
if diff := testutil.Diff(gotField, wantField); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
tests := []TestTable{{
description: "empty",
item: TableValues{},
wantQuery: "",
wantArgs: nil,
}, {
description: "no columns",
item: TableValues{
RowValues: [][]any{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
},
wantQuery: "VALUES (?, ?, ?)" +
", (?, ?, ?)" +
", (?, ?, ?)",
wantArgs: []any{1, 2, 3, 4, 5, 6, 7, 8, 9},
}, {
description: "postgres",
dialect: DialectPostgres,
item: TableValues{
Columns: []string{"a", "b", "c"},
RowValues: [][]any{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
},
wantQuery: "VALUES ($1, $2, $3)" +
", ($4, $5, $6)" +
", ($7, $8, $9)",
wantArgs: []any{1, 2, 3, 4, 5, 6, 7, 8, 9},
}, {
description: "mysql",
dialect: DialectMySQL,
item: TableValues{
Columns: []string{"a", "b", "c"},
RowValues: [][]any{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
},
wantQuery: "VALUES ROW(?, ?, ?)" +
", ROW(?, ?, ?)" +
", ROW(?, ?, ?)",
wantArgs: []any{1, 2, 3, 4, 5, 6, 7, 8, 9},
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
var gotArgs []any
err := tt.item.WriteSQL(context.Background(), tt.dialect, &buf, &gotArgs, nil)
if err != nil {
t.Fatal(testutil.Callers(), err)
}
gotQuery := buf.String()
if diff := testutil.Diff(gotQuery, tt.wantQuery); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(gotArgs, tt.wantArgs); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
}
}