-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater_test.go
104 lines (102 loc) · 2.38 KB
/
updater_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package morm
import (
"github.com/NotFound1911/morm/errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestUpdater_Build(t *testing.T) {
db := memoryDB(t)
testCases := []struct {
name string
u QueryBuilder
want *Query
wantErr error
}{
{
name: "no columns",
u: NewUpdater[TestModel](db),
wantErr: errs.NewErrNoUpdatedColumns(),
},
{
name: "single column",
u: NewUpdater[TestModel](db).Update(&TestModel{
Age: 18,
}).Set(C("Age")),
want: &Query{
SQL: "UPDATE `test_model` SET `age`=?;",
Args: []any{int8(18)},
},
},
{
name: "assignment",
u: NewUpdater[TestModel](db).Update(&TestModel{
Age: 18,
FirstName: "Tom",
}).Set(C("Age"), Assign("FirstName", "test")),
want: &Query{
SQL: "UPDATE `test_model` SET `age`=?,`first_name`=?;",
Args: []any{int8(18), "test"},
},
},
{
name: "where",
u: NewUpdater[TestModel](db).Update(&TestModel{
Age: 18,
FirstName: "Tom",
}).Set(C("Age"), Assign("FirstName", "test")).
Where(C("Id").EQ(1)),
want: &Query{
SQL: "UPDATE `test_model` SET `age`=?,`first_name`=? WHERE `id` = ?;",
Args: []any{int8(18), "test", 1},
},
},
{
name: "incremental",
u: NewUpdater[TestModel](db).Update(&TestModel{
Age: 18,
FirstName: "Tom",
}).Set(Assign("Age", C("Age").Add(1))),
want: &Query{
SQL: "UPDATE `test_model` SET `age`=`age` + ?;",
Args: []any{1},
},
},
{
name: "incremental-raw",
u: NewUpdater[TestModel](db).Update(&TestModel{
Age: 18,
FirstName: "Tom",
}).Set(Assign("Age", Raw("`age`+?", 1))),
want: &Query{
SQL: "UPDATE `test_model` SET `age`=`age`+?;",
Args: []any{1},
},
},
{
name: "non-zero",
u: NewUpdater[TestModel](db).Set(AssignNotZeroColumns(&TestModel{Id: 13})...),
want: &Query{
SQL: "UPDATE `test_model` SET `id`=?;",
Args: []any{int64(13)},
},
},
{
name: "non-nil",
u: NewUpdater[TestModel](db).Set(AssignNotNilColumns(&TestModel{Id: 13})...),
want: &Query{
SQL: "UPDATE `test_model` SET `id`=?,`first_name`=?,`age`=?;",
Args: []any{int64(13), "", int8(0)},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
q, err := tc.u.Build()
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.want, q)
})
}
}