-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathupdate_test.go
73 lines (61 loc) · 1.54 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 sqlingo
import (
"context"
"errors"
"testing"
)
func TestUpdate(t *testing.T) {
db := newMockDatabase()
_, _ = db.Update(Table1).Set(field1, field2).Where(True()).Execute()
assertLastSql(t, "UPDATE `table1` SET `field1` = `field2`")
_, _ = db.Update(Table1).
Set(field1, 10).
Where(field2.Equals(2)).
OrderBy(field1.Desc()).
Limit(2).
Execute()
assertLastSql(t, "UPDATE `table1` SET `field1` = 10 WHERE `field2` = 2 ORDER BY `field1` DESC LIMIT 2")
_, _ = db.Update(Table1).
SetIf(true, field1, 10).
SetIf(false, field2, 10).
Where(True()).
Execute()
assertLastSql(t, "UPDATE `table1` SET `field1` = 10")
_, _ = db.Update(Table1).
SetIf(false, field1, 10).
Where(True()).
Execute()
assertLastSql(t, "/* UPDATE without SET clause */ DO 0")
_, _ = db.Update(Table1).Limit(3).Execute()
assertLastSql(t, "/* UPDATE without SET clause */ DO 0")
errExp := &expression{
builder: func(scope scope) (string, error) {
return "", errors.New("error")
},
}
if _, err := db.Update(Table1).
Set(field1, 10).
OrderBy(orderBy{by: errExp}).
Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.Update(Table1).
Set(field1, errExp).
Where(True()).
Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.Update(Table1).
Set(field1, 10).
Where(errExp).
Execute(); err == nil {
t.Error("should get error here")
}
if _, err := db.Update(Table1).
Set(field1, 10).
Where(True()).
WithContext(context.Background()).
Execute(); err != nil {
t.Error(err)
}
}