-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
case_test.go
196 lines (161 loc) · 4.04 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
package pgq
import (
"reflect"
"testing"
)
func TestCaseWithVal(t *testing.T) {
t.Parallel()
caseStmt := Case("number").
When("1", "one").
When("2", "two").
Else(Expr("?", "big number"))
qb := Select().
Column(caseStmt).
From("atable")
sql, args, err := qb.SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := "SELECT CASE number " +
"WHEN 1 THEN one " +
"WHEN 2 THEN two " +
"ELSE $1 " +
"END " +
"FROM atable"
if sql != want {
t.Errorf("wanted %v, got %v instead", want, sql)
}
expectedArgs := []any{"big number"}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("wanted %v, got %v instead", expectedArgs, sql)
}
}
func TestCaseWithComplexVal(t *testing.T) {
t.Parallel()
caseStmt := Case("? > ?", 10, 5).
When("true", "'T'")
qb := Select().
Column(Alias{
Expr: caseStmt,
As: "complexCase",
}).
From("atable")
sql, args, err := qb.SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := "SELECT (CASE $1 > $2 " +
"WHEN true THEN 'T' " +
"END) AS complexCase " +
"FROM atable"
if sql != want {
t.Errorf("wanted %v, got %v instead", want, sql)
}
expectedArgs := []any{10, 5}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("wanted %v, got %v instead", expectedArgs, args)
}
}
func TestCaseWithNoVal(t *testing.T) {
t.Parallel()
caseStmt := Case().
When(Eq{"x": 0}, "x is zero").
When(Expr("x > ?", 1), Expr("CONCAT('x is greater than ', ?)", 2))
qb := Select().Column(caseStmt).From("atable")
sql, args, err := qb.SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := "SELECT CASE " +
"WHEN x = $1 THEN x is zero " +
"WHEN x > $2 THEN CONCAT('x is greater than ', $3) " +
"END " +
"FROM atable"
if sql != want {
t.Errorf("wanted %v, got %v instead", want, sql)
}
expectedArgs := []any{0, 1, 2}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("wanted %v, got %v instead", expectedArgs, args)
}
}
func TestCaseWithExpr(t *testing.T) {
t.Parallel()
caseStmt := Case(Expr("x = ?", true)).
When("true", Expr("?", "it's true!")).
Else("42")
qb := Select().Column(caseStmt).From("atable")
sql, args, err := qb.SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := "SELECT CASE x = $1 " +
"WHEN true THEN $2 " +
"ELSE 42 " +
"END " +
"FROM atable"
if sql != want {
t.Errorf("wanted %v, got %v instead", want, sql)
}
expectedArgs := []any{true, "it's true!"}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("wanted %v, got %v instead", expectedArgs, args)
}
}
func TestMultipleCase(t *testing.T) {
t.Parallel()
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{
Expr: caseStmtNoval,
As: "case_noval",
}).
Column(Alias{
Expr: caseStmtExpr,
As: "case_expr",
}).
From("atable")
sql, args, err := qb.SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := "SELECT " +
"(CASE x = $1 WHEN true THEN $2 ELSE 42 END) AS case_noval, " +
"(CASE WHEN x = $3 THEN 'x is zero' WHEN x > $4 THEN CONCAT('x is greater than ', $5) END) AS case_expr " +
"FROM atable"
if sql != want {
t.Errorf("wanted %v, got %v instead", want, sql)
}
expectedArgs := []any{
true, "it's true!",
0, 1, 2,
}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("wanted %v, got %v instead", expectedArgs, args)
}
}
func TestCaseWithNoWhenClause(t *testing.T) {
t.Parallel()
caseStmt := Case("something").
Else("42")
qb := Select().Column(caseStmt).From("atable")
_, _, err := qb.SQL()
want := "case expression must contain at lease one WHEN clause"
if err.Error() != want {
t.Errorf("wanted error to be %v, got %v instead", want, err)
}
}
func TestCaseBuilderMustSQL(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r == nil {
t.Errorf("TestCaseBuilderMustSQL should have panicked!")
}
}()
Case("").MustSQL()
}