(t *testing.T)
| 97 | } |
| 98 | |
| 99 | func 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 | |
| 130 | func TestCaseWithNoWhenClause(t *testing.T) { |
| 131 | caseStmt := Case("something"). |
nothing calls this directly
no test coverage detected
searching dependent graphs…