MCPcopy Index your code
hub / github.com/Masterminds/squirrel / TestMultipleCase

Function TestMultipleCase

case_test.go:99–128  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

97}
98
99func TestMultipleCase(t *testing.T) {
100 caseStmtNoval := Case(Expr("x = ?", true)).
101 When("true", Expr("?", "it's true!")).
102 Else("42")
103 caseStmtExpr := Case().
104 When(Eq{"x": 0}, "'x is zero'").
105 When(Expr("x > ?", 1), Expr("CONCAT('x is greater than ', ?)", 2))
106
107 qb := Select().
108 Column(Alias(caseStmtNoval, "case_noval")).
109 Column(Alias(caseStmtExpr, "case_expr")).
110 From("table")
111
112 sql, args, err := qb.ToSql()
113
114 assert.NoError(t, err)
115
116 expectedSql := "SELECT " +
117 "(CASE x = ? WHEN true THEN ? ELSE 42 END) AS case_noval, " +
118 "(CASE WHEN x = ? THEN 'x is zero' WHEN x > ? THEN CONCAT('x is greater than ', ?) END) AS case_expr " +
119 "FROM table"
120
121 assert.Equal(t, expectedSql, sql)
122
123 expectedArgs := []interface{}{
124 true, "it's true!",
125 0, 1, 2,
126 }
127 assert.Equal(t, expectedArgs, args)
128}
129
130func TestCaseWithNoWhenClause(t *testing.T) {
131 caseStmt := Case("something").

Callers

nothing calls this directly

Calls 9

CaseFunction · 0.85
ExprFunction · 0.85
SelectFunction · 0.85
AliasFunction · 0.85
ElseMethod · 0.80
WhenMethod · 0.80
ColumnMethod · 0.80
ToSqlMethod · 0.65
FromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…