forked from fluge/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
73 lines (62 loc) · 1.69 KB
/
update_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
package squirrel
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUpdateBuilderToSql(t *testing.T) {
b := Update("").
Prefix("WITH prefix AS ?", 0).
Table("a").
Set("b", Expr("? + 1", 1)).
SetMap(Eq{"c": 2}).
Where("d = ?", 3).
OrderBy("e").
Limit(4).
Offset(5).
Suffix("RETURNING ?", 6)
sql, args := b.ToSql()
//assert.NoError(t, err)
expectedSql :=
"WITH prefix AS ? " +
"UPDATE a SET b = ? + 1, c = ? WHERE d = ? " +
"ORDER BY e LIMIT 4 OFFSET 5 " +
"RETURNING ?"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{0, 1, 2, 3, 6}
assert.Equal(t, expectedArgs, args)
}
func TestUpdateBuilderToSqlErr(t *testing.T) {
sql, args := Update("a").Set("x", 1).ToSql()
//assert.Error(t, err)
t.Log(sql,args)
sql2, args1 := Update("x").Set("sa",1).ToSql()
t.Log(sql2,args1)
}
func TestUpdateBuilderPlaceholders(t *testing.T) {
b := Update("test").SetMap(Eq{"x": 1, "y": 2})
sql, _ := b.PlaceholderFormat(Question).ToSql()
assert.Equal(t, "UPDATE test SET x = ?, y = ?", sql)
sql, _ = b.PlaceholderFormat(Dollar).ToSql()
assert.Equal(t, "UPDATE test SET x = $1, y = $2", sql)
}
//func TestUpdateBuilderRunners(t *testing.T) {
// db := &DBStub{}
// b := Update("test").Set("x", 1).RunWith(db)
//
// expectedSql := "UPDATE test SET x = ?"
//
// b.Exec()
// assert.Equal(t, expectedSql, db.LastExecSql)
//}
//
//func TestUpdateBuilderNoRunner(t *testing.T) {
// b := Update("test").Set("x", 1)
//
// _, err := b.Exec()
// assert.Equal(t, RunnerNotSet, err)
//}
func TestUpdateBuilder_IncrBy(t *testing.T) {
a, _:= Update("test").Set("x", 1).IncrBy("a", 1).ToSql()
expectedSql := "UPDATE test SET x = ?, a = a+?"
assert.Equal(t, expectedSql, a)
}