-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtx_test.go
319 lines (265 loc) · 7.37 KB
/
tx_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package orm_test
import (
"database/sql"
"testing"
"github.com/issue9/assert/v4"
"github.com/issue9/orm/v6"
"github.com/issue9/orm/v6/internal/test"
)
func TestTx_InsertMany(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
tx, err := t.DB.Begin()
a.NotError(err).NotNil(tx)
a.NotError(tx.Create(&UserInfo{}))
p1 := tx.NewEngine("p1_")
a.NotError(p1.Create(&UserInfo{}))
defer func() {
t.TB().Helper()
t.NotError(t.DB.Drop(&UserInfo{})).
NotError(t.DB.New("p1_").Drop(&UserInfo{}))
}()
a.NotError(tx.InsertMany(10, &UserInfo{
UID: 1,
FirstName: "f1",
LastName: "l1",
})).NotError(p1.InsertMany(10, &UserInfo{
UID: 1,
FirstName: "f1",
LastName: "l1",
}))
// 分批插入
a.NotError(tx.InsertMany(2, []orm.TableNamer{
&UserInfo{
UID: 2,
FirstName: "f2",
LastName: "l2",
},
&UserInfo{
UID: 3,
FirstName: "f3",
LastName: "l3",
},
&UserInfo{
UID: 4,
FirstName: "f4",
LastName: "l4",
},
&UserInfo{
UID: 5,
FirstName: "f5",
LastName: "l5",
},
&UserInfo{
UID: 6,
FirstName: "f6",
LastName: "l6",
},
}...))
// 单个元素插入
t.NotError(tx.InsertMany(10,
&UserInfo{
UID: 7,
FirstName: "f7",
LastName: "l7",
}))
t.NotError(tx.Commit())
u1 := &UserInfo{UID: 1}
found, err := t.DB.Select(u1)
t.NotError(err).True(found)
t.Equal(u1, &UserInfo{UID: 1, FirstName: "f1", LastName: "l1", Sex: "male"})
u2 := &UserInfo{LastName: "l2", FirstName: "f2"}
found, err = t.DB.Select(u2)
t.NotError(err).True(found).
Equal(u2, &UserInfo{UID: 2, FirstName: "f2", LastName: "l2", Sex: "male"})
u3 := &UserInfo{UID: 3}
found, err = t.DB.Select(u3)
t.NotError(err).True(found).
Equal(u3, &UserInfo{UID: 3, FirstName: "f3", LastName: "l3", Sex: "male"})
u4 := &UserInfo{UID: 4}
found, err = t.DB.Select(u4)
t.NotError(err).True(found).
Equal(u4, &UserInfo{UID: 4, FirstName: "f4", LastName: "l4", Sex: "male"})
u5 := &UserInfo{UID: 5}
found, err = t.DB.Select(u5)
t.NotError(err).True(found).
Equal(u5, &UserInfo{UID: 5, FirstName: "f5", LastName: "l5", Sex: "male"})
u6 := &UserInfo{UID: 6}
found, err = t.DB.Select(u6)
t.NotError(err).True(found).
Equal(u6, &UserInfo{UID: 6, FirstName: "f6", LastName: "l6", Sex: "male"})
u7 := &UserInfo{UID: 7}
found, err = t.DB.Select(u7)
t.NotError(err).True(found).
Equal(u7, &UserInfo{UID: 7, FirstName: "f7", LastName: "l7", Sex: "male"})
p1db := t.DB.New("p1_")
pu1 := &UserInfo{UID: 1}
found, err = p1db.Select(pu1)
t.NotError(err).True(found).
Equal(pu1, &UserInfo{UID: 1, FirstName: "f1", LastName: "l1", Sex: "male"})
pu7 := &UserInfo{UID: 7}
found, err = p1db.Select(pu7)
t.NotError(err).False(found)
})
}
func TestTx_LastInsertID(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
a.NotError(t.DB.Create(&User{}))
defer func() {
a.NotError(t.DB.Drop(&User{}))
}()
tx, err := t.DB.Begin()
t.NotError(err)
id, err := tx.LastInsertID(&User{Username: "1"})
t.NotError(err).Equal(id, 1)
id, err = tx.LastInsertID(&User{Username: "2"})
t.NotError(err).Equal(id, 2)
t.NotError(tx.Commit())
})
}
func TestTx_Insert(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
t.NotError(t.DB.Create(&User{}))
defer func() {
t.NotError(t.DB.Drop(&User{}))
}()
tx, err := t.DB.Begin()
t.NotError(err)
_, err = tx.Insert(&User{
Username: "u1",
})
t.NotError(err)
_, err = tx.Insert(&User{
Username: "u2",
})
a.NotError(err)
_, err = tx.Insert(&User{
Username: "u3",
})
t.NotError(err)
t.NotError(tx.Commit())
u1 := &User{ID: 1}
found, err := t.DB.Select(u1)
t.NotError(err).True(found)
t.Equal(u1, &User{ID: 1, Username: "u1"})
u3 := &User{ID: 3}
found, err = t.DB.Select(u3)
t.NotError(err).True(found)
t.Equal(u3, &User{ID: 3, Username: "u3"})
})
}
func TestTx_Update(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
initData(t)
defer clearData(t)
tx, err := t.DB.Begin()
t.NotError(err).NotNil(tx)
// update
_, err = tx.Update(&UserInfo{
UID: 1,
FirstName: "firstName1",
LastName: "lastName1",
Sex: "sex1",
})
t.NotError(err)
_, err = tx.Update(&UserInfo{
UID: 2,
FirstName: "firstName2",
LastName: "lastName2",
Sex: "sex2",
})
t.NotError(err)
t.NotError(tx.Commit())
u1 := &UserInfo{UID: 1}
found, err := t.DB.Select(u1)
t.NotError(err).True(found)
t.Equal(1, u1.UID).
Equal("firstName1", u1.FirstName).
Equal("lastName1", u1.LastName).
Equal("sex1", u1.Sex)
if t.DriverName == "mysql" {
t.Equal(u1.Any, []byte("55"))
} else {
t.Equal(u1.Any, "55")
}
u2 := &UserInfo{LastName: "lastName2", FirstName: "firstName2"}
found, err = t.DB.Select(u2)
t.NotError(err).True(found)
t.Equal(u2, &UserInfo{UID: 2, FirstName: "firstName2", LastName: "lastName2", Sex: "sex2"})
})
}
func TestTx_Save(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
initData(t)
defer clearData(t)
tx, err := t.DB.Begin()
t.NotError(err).NotNil(tx)
// 指定的行存在,采用 update
cnt, err := t.DB.SQLBuilder().Select().Column("count(*) AS cnt").From(orm.TableName(&Group{})).QueryInt("cnt")
a.NotError(err).Equal(cnt, 1)
lastid, isnew, err := tx.Save(&Group{ID: sql.NullInt64{Valid: true, Int64: 1}, Name: "save"})
a.NotError(err).False(isnew).Zero(lastid).
NotError(tx.Commit())
cnt, err = t.DB.SQLBuilder().Select().Column("count(*) AS cnt").From(orm.TableName(&Group{})).QueryInt("cnt")
a.NotError(err).Equal(cnt, 1) // 没有增加行数
// insert
tx, err = t.DB.Begin()
t.NotError(err).NotNil(tx)
lastid, isnew, err = t.DB.Save(&Group{ID: sql.NullInt64{Valid: true, Int64: 2}, Name: "save"})
a.Error(err)
// insert
tx, err = t.DB.Begin()
t.NotError(err).NotNil(tx)
lastid, isnew, err = t.DB.Save(&Group{Name: "save"})
a.NotError(err).True(isnew).Equal(lastid, 2)
cnt, err = t.DB.SQLBuilder().Select().Column("count(*) AS cnt").From(orm.TableName(&Group{})).QueryInt("cnt")
a.NotError(err).Equal(cnt, 2).
NotError(tx.Commit())
})
}
func TestTX(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
t.NotError(t.DB.Create(&User{})).
NotError(t.DB.Create(&UserInfo{}))
defer func() {
t.NotError(t.DB.Drop(&User{})).
NotError(t.DB.Drop(&UserInfo{}))
}()
// 回滚事务
tx, err := t.DB.Begin()
t.NotError(err).NotNil(tx)
_, err = tx.Insert(&User{Username: "u1"})
t.NotError(err)
_, err = tx.Insert(&User{Username: "u2"})
t.NotError(err)
_, err = tx.Insert(&User{Username: "u3"})
t.NotError(err)
t.NotError(tx.Rollback())
hasCount(t.DB, a, "users", 0) // tx 已经结束
// 正常提交
tx, err = t.DB.Begin()
t.NotError(err).NotNil(tx)
_, err = tx.Insert(&User{Username: "u1"})
t.NotError(err)
_, err = tx.Insert(&User{Username: "u2"})
t.NotError(err)
_, err = tx.Insert(&User{Username: "u3"})
t.NotError(err)
t.NotError(tx.Commit())
hasCount(t.DB, a, "users", 3)
})
}