forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
case_test.go
150 lines (113 loc) · 3.22 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
package squirrel
import (
"testing"
"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 one " +
"WHEN 2 THEN two " +
"ELSE ? " +
"END " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{"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 'T' " +
"END) AS complexCase " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{10, 5}
assert.Equal(t, expectedArgs, args)
}
func TestCaseWithNoVal(t *testing.T) {
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("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 := []interface{}{0, 1, 2}
assert.Equal(t, expectedArgs, args)
}
func TestCaseWithExpr(t *testing.T) {
caseStmt := Case(Expr("x = ?", true)).
When("true", Expr("?", "it's true!")).
Else("42")
qb := Select().Column(caseStmt).From("table")
sql, args, err := qb.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT CASE x = ? " +
"WHEN true THEN ? " +
"ELSE 42 " +
"END " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{true, "it's true!"}
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 42 END) AS case_noval, " +
"(CASE WHEN x = ? THEN 'x is zero' WHEN x > ? THEN CONCAT('x is greater than ', ?) END) AS case_expr " +
"FROM table"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{
true, "it's true!",
0, 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()
}