-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate_test.go
73 lines (70 loc) · 1.87 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 sqlbuilder
import (
"testing"
)
func TestUpdate(t *testing.T) {
table1 := NewTable(
"TABLE_A",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
IntColumn("test1", nil),
IntColumn("test2", nil),
)
table2 := NewTable(
"TABLE_B",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
)
tableJoined := table1.InnerJoin(table2, table1.C("test1").Eq(table2.C("id")))
var cases = []statementTestCase{{
stmt: Update(table1).Where(table1.C("id").Eq(1)).
Set(table1.C("test1"), 10).
Set(table1.C("test2"), 20).
OrderBy(true, table1.C("test1")).
Limit(1).
Offset(2),
query: `UPDATE "TABLE_A" SET "test1"=?, "test2"=? WHERE "TABLE_A"."id"=? ORDER BY "TABLE_A"."test1" DESC LIMIT ? OFFSET ?;`,
args: []interface{}{int64(10), int64(20), int64(1), 1, 2},
errmsg: "",
}, {
stmt: Update(table1).Where(table1.C("id").Eq(1)).
Set(table1.C("test1"), 10).
Set(table1.C("test2"), 20),
query: `UPDATE "TABLE_A" SET "test1"=?, "test2"=? WHERE "TABLE_A"."id"=?;`,
args: []interface{}{int64(10), int64(20), int64(1)},
errmsg: "",
}, {
stmt: Update(nil).Where(table1.C("id").Eq(1)).
Set(table1.C("test1"), 10).
Set(table1.C("test2"), 20),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: table is nil.",
}, {
stmt: Update(table1).Where(table1.C("id").Eq(1)),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: length of sets is 0.",
}, {
stmt: Update(table1).Where(table1.C("id").Eq(1)).
Set(table1.C("test1"), "foo"),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: int column not accept string.",
}, {
stmt: Update(tableJoined),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: length of sets is 0.",
}}
for num, c := range cases {
mes, args, ok := c.Run()
if !ok {
t.Errorf(mes+" (case no.%d)", append(args, num)...)
}
}
}