-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase_test.go
229 lines (186 loc) · 5.42 KB
/
case_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
package squirrel
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCaseWithVal(t *testing.T) {
caseStmt := Case("number").
When("1", "one").
When("2", "two").
Else(Expr("?", "big number"))
qb := Select().
Column(caseStmt).
From("table")
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT CASE number " +
"WHEN 1 THEN CAST(? AS text) " +
"WHEN 2 THEN CAST(? AS text) " +
"ELSE ? " +
"END " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []any{"one", "two", "big number"}
assert.Equal(t, expectedArgs, args)
}
func TestCaseWithComplexVal(t *testing.T) {
caseStmt := Case("? > ?", 10, 5).
When("true", "T")
qb := Select().
Column(Alias(caseStmt, "complexCase")).
From("table")
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT (CASE ? > ? " +
"WHEN true THEN CAST(? AS text) " +
"END) AS complexCase " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []any{10, 5, "T"}
assert.Equal(t, expectedArgs, args)
}
func TestCaseWithNoVal(t *testing.T) {
caseStmt := Case().
When(Eq{"x": 0}, Expr("x is zero")).
When(Expr("x > ?", 1), Expr("CONCAT('x is greater than ', ?)", 2))
qb := Select().Column(caseStmt).From("table")
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT CASE " +
"WHEN x = ? THEN x is zero " +
"WHEN x > ? THEN CONCAT('x is greater than ', ?) " +
"END " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []any{0, 1, 2}
assert.Equal(t, expectedArgs, args)
}
func TestCaseWithExpr(t *testing.T) {
caseStmt := Case(Expr("x = ?", true)).
When("1 > 0", Expr("?::text", "it's true!")).
When("1 > 0", "test").
When("1 > 0", 42).
When("1 > 0", 42.1).
When("1 > 0", true).
Else(42)
qb := Select().Column(caseStmt).From("table")
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT CASE x = ? " +
"WHEN 1 > 0 THEN ?::text " +
"WHEN 1 > 0 THEN CAST(? AS text) " +
"WHEN 1 > 0 THEN CAST(? AS bigint) " +
"WHEN 1 > 0 THEN CAST(? AS double precision) " +
"WHEN 1 > 0 THEN CAST(? AS boolean) " +
"ELSE ? " +
"END " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []any{
true,
"it's true!",
"test",
42,
42.1,
true,
42,
}
assert.Equal(t, expectedArgs, args)
}
func TestMultipleCase(t *testing.T) {
caseStmtNoval := Case(Expr("x = ?", true)).
When("true", Expr("?", "it's true!")).
Else(42)
caseStmtExpr := Case().
When(Eq{"x": 0}, "x is zero").
When(Expr("x > ?", 1), Expr("CONCAT('x is greater than ', ?)", 2))
qb := Select().
Column(Alias(caseStmtNoval, "case_noval")).
Column(Alias(caseStmtExpr, "case_expr")).
From("table")
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT " +
"(CASE x = ? WHEN true THEN ? ELSE ? END) AS case_noval, " +
"(CASE WHEN x = ? THEN CAST(? AS text) WHEN x > ? THEN CONCAT('x is greater than ', ?) END) AS case_expr " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []any{
true, "it's true!",
42, 0, "x is zero", 1, 2,
}
assert.Equal(t, expectedArgs, args)
}
func TestCaseWithNoWhenClause(t *testing.T) {
caseStmt := Case("something").
Else("42")
qb := Select().Column(caseStmt).From("table")
_, _, err := qb.ToSql()
assert.Error(t, err)
assert.Equal(t, "case expression must contain at lease one WHEN clause", err.Error())
}
func TestCaseBuilderMustSql(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestCaseBuilderMustSql should have panicked!")
}
}()
Case("").MustSql()
}
func TestCaseNull(t *testing.T) {
caseStmt := Case().
When("1", nil).
Else(nil)
qb := Select().
Column(caseStmt).
From("table")
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT CASE " +
"WHEN 1 THEN ? " +
"ELSE ? " +
"END " +
"FROM table"
assert.Equal(t, expectedSql, sql)
assert.Equal(t, []any{nil, nil}, args)
}
func TestSqlTypeNameHelper(t *testing.T) {
tests := []struct {
name string
arg reflect.Type
want string
wantErr bool
}{
{"Bool", reflect.TypeOf(true), "boolean", false},
{"Int64", reflect.TypeOf(int64(1)), "bigint", false},
{"Uint64", reflect.TypeOf(uint64(1)), "bigint", false},
{"Int", reflect.TypeOf(int(1)), "bigint", false},
{"Uint", reflect.TypeOf(uint(1)), "bigint", false},
{"Int32", reflect.TypeOf(int32(1)), "integer", false},
{"Uint32", reflect.TypeOf(uint32(1)), "integer", false},
{"Int16", reflect.TypeOf(int16(1)), "smallint", false},
{"Uint16", reflect.TypeOf(uint16(1)), "smallint", false},
{"Int8", reflect.TypeOf(int8(1)), "smallint", false},
{"Uint8", reflect.TypeOf(uint8(1)), "smallint", false},
{"Float32", reflect.TypeOf(float32(1.0)), "double precision", false},
{"Float64", reflect.TypeOf(float64(1.0)), "double precision", false},
{"String", reflect.TypeOf(string("test")), "text", false},
{"Time", reflect.TypeOf(time.Time{}), "timestamp with time zone", false},
{"Slice", reflect.TypeOf([]int{1, 2, 3}), "bigint[]", false},
{"Unsupported", reflect.TypeOf(struct{}{}), "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t1 *testing.T) {
got, err := sqlTypeNameHelper(tt.arg)
if (err != nil) != tt.wantErr {
t1.Errorf("sqlTypeNameHelper() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t1.Errorf("sqlTypeNameHelper() = %v, want %v", got, tt.want)
}
})
}
}