forked from ecodeclub/eorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharding_insert_test.go
335 lines (319 loc) · 12.7 KB
/
sharding_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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// 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"
"fmt"
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/ecodeclub/eorm/internal/datasource"
"github.com/ecodeclub/eorm/internal/datasource/cluster"
"github.com/ecodeclub/eorm/internal/datasource/masterslave"
"github.com/ecodeclub/eorm/internal/datasource/shardingsource"
"github.com/ecodeclub/eorm/internal/errs"
"github.com/ecodeclub/eorm/internal/model"
"github.com/ecodeclub/eorm/internal/sharding"
"github.com/ecodeclub/eorm/internal/sharding/hash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.uber.org/multierr"
)
func newMockErr(dbName string) error {
return fmt.Errorf("mock error for %s", dbName)
}
type OrderInsert struct {
UserId int `eorm:"primary_key"`
OrderId int64
Content string
Account float64
}
func TestShardingInsert_Build(t *testing.T) {
r := model.NewMetaRegistry()
dbBase, tableBase, dsBase := 2, 3, 2
dbPattern, tablePattern, dsPattern := "order_db_%d", "order_tab_%d", "%d.db.cluster.company.com:3306"
_, err := r.Register(&OrderInsert{},
model.WithTableShardingAlgorithm(&hash.Hash{
ShardingKey: "UserId",
DBPattern: &hash.Pattern{Name: dbPattern, Base: dbBase},
TablePattern: &hash.Pattern{Name: tablePattern, Base: tableBase},
DsPattern: &hash.Pattern{Name: dsPattern, Base: dsBase},
}))
require.NoError(t, err)
m := map[string]*masterslave.MasterSlavesDB{
"order_db_0": MasterSlavesMemoryDB(),
"order_db_1": MasterSlavesMemoryDB(),
"order_db_2": MasterSlavesMemoryDB(),
}
clusterDB := cluster.NewClusterDB(m)
ds := map[string]datasource.DataSource{
"0.db.cluster.company.com:3306": clusterDB,
"1.db.cluster.company.com:3306": clusterDB,
}
shardingDB, err := OpenDS("sqlite3",
shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
require.NoError(t, err)
testCases := []struct {
name string
builder sharding.QueryBuilder
wantQs []sharding.Query
wantErr error
}{
{
name: "插入一个元素",
builder: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{UserId: 1, OrderId: 1, Content: "1", Account: 1.0},
}),
wantQs: []sharding.Query{
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);", "`order_db_1`", "`order_tab_1`"),
Args: []any{1, int64(1), "1", 1.0},
DB: "order_db_1",
Datasource: "1.db.cluster.company.com:3306",
},
},
},
{
name: "插入多个元素",
builder: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{UserId: 1, OrderId: 1, Content: "1", Account: 1.0},
{UserId: 2, OrderId: 2, Content: "2", Account: 2.0},
{UserId: 3, OrderId: 3, Content: "3", Account: 3.0},
{UserId: 4, OrderId: 4, Content: "4", Account: 4.0},
}),
wantQs: []sharding.Query{
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);", "`order_db_0`", "`order_tab_1`"),
Args: []any{4, int64(4), "4", 4.0},
DB: "order_db_0",
Datasource: "0.db.cluster.company.com:3306",
},
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);", "`order_db_0`", "`order_tab_2`"),
Args: []any{2, int64(2), "2", 2.0},
DB: "order_db_0",
Datasource: "0.db.cluster.company.com:3306",
},
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);", "`order_db_1`", "`order_tab_0`"),
Args: []any{3, int64(3), "3", 3.0},
DB: "order_db_1",
Datasource: "1.db.cluster.company.com:3306",
},
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);", "`order_db_1`", "`order_tab_1`"),
Args: []any{1, int64(1), "1", 1.0},
DB: "order_db_1",
Datasource: "1.db.cluster.company.com:3306",
},
},
},
{
name: "插入多个元素, 但是不同的元素会被分配到同一个库",
builder: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{UserId: 1, OrderId: 1, Content: "1", Account: 1.0},
{UserId: 7, OrderId: 7, Content: "7", Account: 7.0},
}),
wantQs: []sharding.Query{
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?),(?,?,?,?);", "`order_db_1`", "`order_tab_1`"),
Args: []any{1, int64(1), "1", 1.0, 7, int64(7), "7", 7.0},
DB: "order_db_1",
Datasource: "1.db.cluster.company.com:3306",
},
},
},
{
name: "插入多个元素, 有不同的元素会被分配到同一个库表",
builder: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{UserId: 1, OrderId: 1, Content: "1", Account: 1.0},
{UserId: 7, OrderId: 7, Content: "7", Account: 7.0},
{UserId: 2, OrderId: 2, Content: "2", Account: 2.0},
{UserId: 8, OrderId: 8, Content: "8", Account: 8.0},
{UserId: 3, OrderId: 3, Content: "3", Account: 3.0},
}),
wantQs: []sharding.Query{
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?),(?,?,?,?);", "`order_db_0`", "`order_tab_2`"),
Args: []any{2, int64(2), "2", 2.0, 8, int64(8), "8", 8.0},
DB: "order_db_0",
Datasource: "0.db.cluster.company.com:3306",
},
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);", "`order_db_1`", "`order_tab_0`"),
Args: []any{3, int64(3), "3", 3.0},
DB: "order_db_1",
Datasource: "1.db.cluster.company.com:3306",
},
{
SQL: fmt.Sprintf("INSERT INTO %s.%s(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?),(?,?,?,?);", "`order_db_1`", "`order_tab_1`"),
Args: []any{1, int64(1), "1", 1.0, 7, int64(7), "7", 7.0},
DB: "order_db_1",
Datasource: "1.db.cluster.company.com:3306",
},
},
},
{
name: "插入时,插入的列没有包含分库分表的列",
builder: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{OrderId: 1, Content: "1", Account: 1.0},
}).Columns([]string{"OrderId", "Content", "Account"}),
wantErr: errs.ErrInsertShardingKeyNotFound,
},
{
name: "插入时,忽略主键,但主键为shardingKey报错",
builder: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{OrderId: 1, Content: "1", Account: 1.0},
}).IgnorePK(),
wantErr: errs.ErrInsertShardingKeyNotFound,
},
{
name: "values中没有元素报错",
builder: NewShardingInsert[OrderInsert](shardingDB),
wantErr: errors.New("插入0行"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
qs, err := tc.builder.Build(context.Background())
require.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.ElementsMatch(t, tc.wantQs, qs)
})
}
}
type ShardingInsertSuite struct {
suite.Suite
mock01 sqlmock.Sqlmock
mockDB01 *sql.DB
mock02 sqlmock.Sqlmock
mockDB02 *sql.DB
}
func (s *ShardingInsertSuite) SetupSuite() {
t := s.T()
var err error
s.mockDB01, s.mock01, err = sqlmock.New()
if err != nil {
t.Fatal(err)
}
s.mockDB02, s.mock02, err = sqlmock.New()
if err != nil {
t.Fatal(err)
}
}
func (s *ShardingInsertSuite) TearDownTest() {
_ = s.mockDB01.Close()
_ = s.mockDB02.Close()
}
func (s *ShardingInsertSuite) TestShardingInsert_Exec() {
r := model.NewMetaRegistry()
dbBase, tableBase := 2, 3
dbPattern, tablePattern, dsPattern := "order_db_%d", "order_tab_%d", "0.db.cluster.company.com:3306"
_, err := r.Register(&OrderInsert{},
model.WithTableShardingAlgorithm(&hash.Hash{
ShardingKey: "UserId",
DBPattern: &hash.Pattern{Name: dbPattern, Base: dbBase},
TablePattern: &hash.Pattern{Name: tablePattern, Base: tableBase},
DsPattern: &hash.Pattern{Name: dsPattern, NotSharding: true},
}))
require.NoError(s.T(), err)
m := map[string]*masterslave.MasterSlavesDB{
"order_db_0": MasterSlavesMockDB(s.mockDB01),
"order_db_1": MasterSlavesMockDB(s.mockDB02),
}
clusterDB := cluster.NewClusterDB(m)
ds := map[string]datasource.DataSource{
"0.db.cluster.company.com:3306": clusterDB,
}
shardingDB, err := OpenDS("mysql",
shardingsource.NewShardingDataSource(ds), DBWithMetaRegistry(r))
require.NoError(s.T(), err)
testcases := []struct {
name string
si *ShardingInserter[OrderInsert]
mockDb func()
wantErr error
wantAffectedRows int64
}{
{
name: "跨表插入全部成功",
si: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{UserId: 1, OrderId: 1, Content: "1", Account: 1.0},
{UserId: 2, OrderId: 2, Content: "2", Account: 2.0},
{UserId: 3, OrderId: 3, Content: "3", Account: 3.0},
}),
mockDb: func() {
s.mock02.MatchExpectationsInOrder(false)
s.mock02.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_1`.`order_tab_1`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(1, int64(1), "1", 1.0).WillReturnResult(sqlmock.NewResult(1, 1))
s.mock02.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_1`.`order_tab_0`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(3, int64(3), "3", 3.0).WillReturnResult(sqlmock.NewResult(1, 1))
s.mock01.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_0`.`order_tab_2`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(2, int64(2), "2", 2.0).WillReturnResult(sqlmock.NewResult(1, 1))
},
wantAffectedRows: 3,
},
{
name: "部分插入失败",
si: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{UserId: 1, OrderId: 1, Content: "1", Account: 1.0},
{UserId: 2, OrderId: 2, Content: "2", Account: 2.0},
{UserId: 3, OrderId: 3, Content: "3", Account: 3.0},
}),
mockDb: func() {
s.mock02.MatchExpectationsInOrder(false)
s.mock02.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_1`.`order_tab_1`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(1, int64(1), "1", 1.0).WillReturnError(newMockErr("db01"))
s.mock02.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_1`.`order_tab_0`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(3, int64(3), "3", 3.0).WillReturnResult(sqlmock.NewResult(1, 1))
s.mock01.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_0`.`order_tab_2`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(2, int64(2), "2", 2.0).WillReturnResult(sqlmock.NewResult(1, 1))
},
wantErr: multierr.Combine(newMockErr("db01")),
},
{
name: "全部插入失败",
si: NewShardingInsert[OrderInsert](shardingDB).Values([]*OrderInsert{
{UserId: 1, OrderId: 1, Content: "1", Account: 1.0},
{UserId: 2, OrderId: 2, Content: "2", Account: 2.0},
{UserId: 3, OrderId: 3, Content: "3", Account: 3.0},
}),
mockDb: func() {
s.mock02.MatchExpectationsInOrder(false)
s.mock02.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_1`.`order_tab_1`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(1, int64(1), "1", 1.0).WillReturnError(newMockErr("db"))
s.mock02.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_1`.`order_tab_0`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(3, int64(3), "3", 3.0).WillReturnError(newMockErr("db"))
s.mock01.ExpectExec(regexp.QuoteMeta("INSERT INTO `order_db_0`.`order_tab_2`(`user_id`,`order_id`,`content`,`account`) VALUES(?,?,?,?);")).WithArgs(2, int64(2), "2", 2.0).WillReturnError(newMockErr("db"))
},
wantErr: multierr.Combine(newMockErr("db"), newMockErr("db"), newMockErr("db")),
},
}
for _, tc := range testcases {
s.T().Run(tc.name, func(t *testing.T) {
tc.mockDb()
res := tc.si.Exec(context.Background())
require.Equal(t, tc.wantErr, res.Err())
if res.Err() != nil {
return
}
affectRows, err := res.RowsAffected()
require.NoError(t, err)
assert.Equal(t, tc.wantAffectedRows, affectRows)
})
}
}
func TestShardingInsertSuite(t *testing.T) {
suite.Run(t, &ShardingInsertSuite{})
}
func MasterSlavesMockDB(db *sql.DB) *masterslave.MasterSlavesDB {
return masterslave.NewMasterSlavesDB(db)
}