forked from ecodeclub/eorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_test.go
317 lines (295 loc) · 8.2 KB
/
transaction_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
// Copyright 2021 ecodeclub
//
// 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"
"database/sql"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/ecodeclub/eorm/internal/datasource"
"github.com/ecodeclub/eorm/internal/datasource/single"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTx_Commit(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer func() { _ = mockDB.Close() }()
db, err := OpenDS("mysql", single.NewDB(mockDB))
if err != nil {
t.Fatal(err)
}
defer func() {
mock.ExpectClose()
_ = db.Close()
}()
// 事务正常提交
mock.ExpectBegin()
mock.ExpectCommit()
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
assert.Nil(t, err)
err = tx.Commit()
assert.Nil(t, err)
}
func TestTx_Rollback(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer func() { _ = mockDB.Close() }()
db, err := OpenDS("mysql", single.NewDB(mockDB))
if err != nil {
t.Fatal(err)
}
// 事务回滚
mock.ExpectBegin()
mock.ExpectRollback()
tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
assert.Nil(t, err)
err = tx.Rollback()
assert.Nil(t, err)
}
func TestTx_QueryContext(t *testing.T) {
testCases := []struct {
name string
query Query
mockOrder func(mock sqlmock.Sqlmock)
sourceFunc func(db *sql.DB, t *testing.T) datasource.DataSource
wantResp []string
wantErr error
isCommit bool
}{
{
name: "err",
mockOrder: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectQuery("SELECT `xx` FROM `test_model`").
WillReturnError(errors.New("未知字段"))
mock.ExpectRollback()
},
sourceFunc: func(db *sql.DB, t *testing.T) datasource.DataSource {
return single.NewDB(db)
},
query: Query{
SQL: "SELECT `xx` FROM `test_model`",
},
wantErr: errors.New("未知字段"),
isCommit: false,
},
{
name: "commit",
mockOrder: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectQuery("SELECT `first_name` FROM `test_model`").
WillReturnRows(sqlmock.NewRows([]string{"first_name"}).AddRow("value"))
mock.ExpectCommit()
},
sourceFunc: func(db *sql.DB, t *testing.T) datasource.DataSource {
return single.NewDB(db)
},
query: Query{
SQL: "SELECT `first_name` FROM `test_model`",
},
isCommit: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer func(db *sql.DB) { _ = db.Close() }(mockDB)
tc.mockOrder(mock)
source := tc.sourceFunc(mockDB, t)
orm, err := OpenDS("mysql", source)
require.NoError(t, err)
tx, err := orm.BeginTx(context.Background(), &sql.TxOptions{})
require.NoError(t, err)
rows, queryErr := tx.queryContext(context.Background(), datasource.Query(tc.query))
assert.Equal(t, queryErr, tc.wantErr)
if queryErr != nil {
return
}
if tc.isCommit {
err = tx.Commit()
} else {
err = tx.Rollback()
}
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.NotNil(t, rows)
var resp []string
for rows.Next() {
val := new(string)
err := rows.Scan(val)
assert.Nil(t, err)
if err != nil {
return
}
assert.NotNil(t, val)
resp = append(resp, *val)
}
assert.ElementsMatch(t, tc.wantResp, resp)
if err = mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
})
}
}
func TestTx_ExecContext(t *testing.T) {
testCases := []struct {
name string
query Query
mockOrder func(mock sqlmock.Sqlmock)
sourceFunc func(db *sql.DB, t *testing.T) datasource.DataSource
wantVal sql.Result
wantBeginTxErr error
wantErr error
isCommit bool
}{
//{
// name: "source err",
// mockOrder: func(mock sqlmock.Sqlmock) {
// mock.ExpectBegin()
// mock.ExpectExec("DELETE FROM `test_model` WHERE `id`=").WithArgs(1).WillReturnResult(sqlmock.NewResult(10, 20))
// mock.ExpectCommit()
// },
// sourceFunc: func(db *sql.DB, t *testing.T) datasource.DataSource {
// clusterDB := cluster.NewClusterDB(map[string]*masterslave.MasterSlavesDB{
// "db0": masterslave.NewMasterSlavesDB(db),
// })
// return clusterDB
// },
// query: Query{
// SQL: "DELETE FROM `test_model` WHERE `id`=",
// Args: []any{1},
// },
// wantBeginTxErr: errors.New("eorm: 未实现 TxBeginner 接口"),
//},
{
name: "commit err",
mockOrder: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec("DELETE FROM `test_model` WHERE `id`=").WithArgs(1).WillReturnResult(sqlmock.NewResult(10, 20))
mock.ExpectCommit().WillReturnError(errors.New("commit 错误"))
},
sourceFunc: func(db *sql.DB, t *testing.T) datasource.DataSource {
return single.NewDB(db)
},
query: Query{
SQL: "DELETE FROM `test_model` WHERE `id`=",
Args: []any{1},
},
wantErr: errors.New("commit 错误"),
isCommit: true,
},
{
name: "rollback err",
mockOrder: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec("DELETE FROM `test_model` WHERE `id`=").WithArgs(1).WillReturnResult(sqlmock.NewResult(10, 20))
mock.ExpectRollback().WillReturnError(errors.New("rollback 错误"))
},
sourceFunc: func(db *sql.DB, t *testing.T) datasource.DataSource {
return single.NewDB(db)
},
query: Query{
SQL: "DELETE FROM `test_model` WHERE `id`=",
Args: []any{1},
},
wantErr: errors.New("rollback 错误"),
},
{
name: "commit",
mockOrder: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec("DELETE FROM `test_model` WHERE `id`=").WithArgs(1).WillReturnResult(sqlmock.NewResult(10, 20))
mock.ExpectCommit()
},
sourceFunc: func(db *sql.DB, t *testing.T) datasource.DataSource {
return single.NewDB(db)
},
query: Query{
SQL: "DELETE FROM `test_model` WHERE `id`=",
Args: []any{1},
},
wantVal: sqlmock.NewResult(10, 20),
isCommit: true,
},
{
name: "rollback",
mockOrder: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec("DELETE FROM `test_model` WHERE `id`=").WithArgs(1).WillReturnResult(sqlmock.NewResult(10, 20))
mock.ExpectRollback()
},
sourceFunc: func(db *sql.DB, t *testing.T) datasource.DataSource {
return single.NewDB(db)
},
query: Query{
SQL: "DELETE FROM `test_model` WHERE `id`=",
Args: []any{1},
},
wantVal: sqlmock.NewResult(10, 20),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer func(db *sql.DB) { _ = db.Close() }(mockDB)
tc.mockOrder(mock)
source := tc.sourceFunc(mockDB, t)
orm, err := OpenDS("mysql", source)
require.NoError(t, err)
tx, err := orm.BeginTx(context.Background(), &sql.TxOptions{})
assert.Equal(t, tc.wantBeginTxErr, err)
if err != nil {
return
}
result, err := tx.execContext(context.Background(), datasource.Query(tc.query))
require.NoError(t, err)
if tc.isCommit {
err = tx.Commit()
} else {
err = tx.Rollback()
}
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
rowsAffectedExpect, err := tc.wantVal.RowsAffected()
require.NoError(t, err)
rowsAffected, err := result.RowsAffected()
require.NoError(t, err)
assert.Equal(t, rowsAffectedExpect, rowsAffected)
lastInsertIdExpected, err := tc.wantVal.LastInsertId()
require.NoError(t, err)
lastInsertId, err := result.LastInsertId()
require.NoError(t, err)
assert.Equal(t, lastInsertIdExpected, lastInsertId)
if err = mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
})
}
}