forked from ecodeclub/eorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_test.go
197 lines (180 loc) · 5.23 KB
/
insert_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2021 gotomicro
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eorm
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInserter_Values(t *testing.T) {
type User struct {
Id int64
FirstName string
Ctime uint64
}
n := uint64(1000)
u := &User{
Id: 12,
FirstName: "Tom",
Ctime: n,
}
u1 := &User{
Id: 13,
FirstName: "Jerry",
Ctime: n,
}
db := memoryDB()
testCases := []CommonTestCase{
{
name: "no examples of values",
builder: NewInserter[User](db).Values(),
wantErr: errors.New("插入0行"),
},
{
name: "single example of values",
builder: NewInserter[User](db).Values(u),
wantSql: "INSERT INTO `user`(`id`,`first_name`,`ctime`) VALUES(?,?,?);",
wantArgs: []interface{}{int64(12), "Tom", n},
},
{
name: "multiple values of same type",
builder: NewInserter[User](db).Values(u, u1),
wantSql: "INSERT INTO `user`(`id`,`first_name`,`ctime`) VALUES(?,?,?),(?,?,?);",
wantArgs: []interface{}{int64(12), "Tom", n, int64(13), "Jerry", n},
},
{
name: "no example of a whole columns",
builder: NewInserter[User](db).Columns("Id", "FirstName").Values(u),
wantSql: "INSERT INTO `user`(`id`,`first_name`) VALUES(?,?);",
wantArgs: []interface{}{int64(12), "Tom"},
},
{
name: "an example with invalid columns",
builder: NewInserter[User](db).Columns("id", "FirstName").Values(u),
wantErr: errors.New("eorm: 未知字段 id"),
},
{
name: "no whole columns and multiple values of same type",
builder: NewInserter[User](db).Columns("Id", "FirstName").Values(u, u1),
wantSql: "INSERT INTO `user`(`id`,`first_name`) VALUES(?,?),(?,?);",
wantArgs: []interface{}{int64(12), "Tom", int64(13), "Jerry"},
},
}
for _, tc := range testCases {
c := tc
t.Run(tc.name, func(t *testing.T) {
q, err := c.builder.Build()
assert.Equal(t, c.wantErr, err)
if err != nil {
return
}
assert.Equal(t, c.wantSql, q.SQL)
assert.Equal(t, c.wantArgs, q.Args)
})
}
}
func TestInserter_Exec(t *testing.T) {
orm := memoryDB()
testCases := []struct {
name string
i *Inserter[TestModel]
wantErr string
wantAffected int64
}{
{
name: "invalid query",
i: NewInserter[TestModel](orm).Values(),
wantErr: "插入0行",
},
{
// 表没创建
name: "table not exist",
i: NewInserter[TestModel](orm).Values(&TestModel{}),
wantErr: "no such table: test_model",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := tc.i.Exec(context.Background())
if err != nil {
assert.EqualError(t, err, tc.wantErr)
return
}
assert.Nil(t, tc.wantErr)
affected, err := res.RowsAffected()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.wantAffected, affected)
})
}
}
func ExampleInserter_Build() {
db := memoryDB()
query, _ := NewInserter[TestModel](db).Values(&TestModel{
Id: 1,
Age: 18,
}).Build()
fmt.Printf("case1\n%s", query.string())
query, _ = NewInserter[TestModel](db).Values(&TestModel{}).Build()
fmt.Printf("case2\n%s", query.string())
// Output:
// case1
// SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?);
// Args: []interface {}{1, "", 18, (*sql.NullString)(nil)}
// case2
// SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?);
// Args: []interface {}{0, "", 0, (*sql.NullString)(nil)}
}
func ExampleInserter_Columns() {
db := memoryDB()
query, _ := NewInserter[TestModel](db).Values(&TestModel{
Id: 1,
Age: 18,
}).Columns("Id", "Age").Build()
fmt.Printf("case1\n%s", query.string())
query, _ = NewInserter[TestModel](db).Values(&TestModel{
Id: 1,
Age: 18,
}, &TestModel{}, &TestModel{FirstName: "Tom"}).Columns("Id", "Age").Build()
fmt.Printf("case2\n%s", query.string())
// Output:
// case1
// SQL: INSERT INTO `test_model`(`id`,`age`) VALUES(?,?);
// Args: []interface {}{1, 18}
// case2
// SQL: INSERT INTO `test_model`(`id`,`age`) VALUES(?,?),(?,?),(?,?);
// Args: []interface {}{1, 18, 0, 0, 0, 0}
}
func ExampleInserter_Values() {
db := memoryDB()
query, _ := NewInserter[TestModel](db).Values(&TestModel{
Id: 1,
Age: 18,
}, &TestModel{}).Build()
fmt.Println(query.string())
// Output:
// SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?),(?,?,?,?);
// Args: []interface {}{1, "", 18, (*sql.NullString)(nil), 0, "", 0, (*sql.NullString)(nil)}
}
func ExampleNewInserter() {
db := memoryDB()
tm := &TestModel{}
query, _ := NewInserter[TestModel](db).Values(tm).Build()
fmt.Printf("SQL: %s", query.SQL)
// Output:
// SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?);
}