Skip to content

Commit a5a1955

Browse files
committed
recover update test
1 parent 0774046 commit a5a1955

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

pkg/datasource/sql/exec/at/update_executor_test.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,39 @@ package at
2020
import (
2121
"context"
2222
"database/sql/driver"
23-
"seata.apache.org/seata-go/pkg/datasource/sql/undo"
23+
"github.com/agiledragon/gomonkey/v2"
24+
"reflect"
2425
"testing"
2526

2627
"github.com/stretchr/testify/assert"
2728

29+
"seata.apache.org/seata-go/pkg/datasource/sql/datasource"
30+
"seata.apache.org/seata-go/pkg/datasource/sql/datasource/mysql"
2831
"seata.apache.org/seata-go/pkg/datasource/sql/exec"
2932
"seata.apache.org/seata-go/pkg/datasource/sql/parser"
3033
"seata.apache.org/seata-go/pkg/datasource/sql/types"
34+
"seata.apache.org/seata-go/pkg/datasource/sql/undo"
3135
"seata.apache.org/seata-go/pkg/datasource/sql/util"
3236
_ "seata.apache.org/seata-go/pkg/util/log"
3337
)
3438

3539
func TestBuildSelectSQLByUpdate(t *testing.T) {
36-
MetaDataMap := map[string]*types.TableMeta{
37-
"t_user": {
38-
TableName: "t_user",
39-
Indexs: map[string]types.IndexMeta{
40-
"id": {
41-
IType: types.IndexTypePrimaryKey,
42-
Columns: []types.ColumnMeta{
43-
{ColumnName: "id"},
40+
undo.InitUndoConfig(undo.Config{OnlyCareUpdateColumns: true})
41+
datasource.RegisterTableCache(types.DBTypeMySQL, mysql.NewTableMetaInstance(nil, nil))
42+
stub := gomonkey.ApplyMethod(reflect.TypeOf(datasource.GetTableCache(types.DBTypeMySQL)), "GetTableMeta",
43+
func(_ *mysql.TableMetaCache, ctx context.Context, dbName, tableName string) (*types.TableMeta, error) {
44+
return &types.TableMeta{
45+
Indexs: map[string]types.IndexMeta{
46+
"id": {
47+
IType: types.IndexTypePrimaryKey,
48+
Columns: []types.ColumnMeta{
49+
{ColumnName: "id"},
50+
},
4451
},
4552
},
46-
},
47-
Columns: map[string]types.ColumnMeta{
48-
"id": {
49-
ColumnDef: nil,
50-
ColumnName: "id",
51-
},
52-
"name": {
53-
ColumnDef: nil,
54-
ColumnName: "name",
55-
},
56-
"age": {
57-
ColumnDef: nil,
58-
ColumnName: "age",
59-
},
60-
},
61-
ColumnNames: []string{"id", "name", "age"},
62-
},
63-
}
64-
65-
undo.InitUndoConfig(undo.Config{OnlyCareUpdateColumns: true})
53+
}, nil
54+
})
55+
defer stub.Reset()
6656

6757
tests := []struct {
6858
name string
@@ -74,25 +64,25 @@ func TestBuildSelectSQLByUpdate(t *testing.T) {
7464
{
7565
sourceQuery: "update t_user set name = ?, age = ? where id = ?",
7666
sourceQueryArgs: []driver.Value{"Jack", 1, 100},
77-
expectQuery: "SELECT SQL_NO_CACHE name,age,t_user.id FROM t_user WHERE id=? FOR UPDATE",
67+
expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? FOR UPDATE",
7868
expectQueryArgs: []driver.Value{100},
7969
},
8070
{
8171
sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age between ? and ?",
8272
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28},
83-
expectQuery: "SELECT SQL_NO_CACHE name,age,t_user.id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? FOR UPDATE",
73+
expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? FOR UPDATE",
8474
expectQueryArgs: []driver.Value{100, 18, 28},
8575
},
8676
{
8777
sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age in (?,?)",
8878
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28},
89-
expectQuery: "SELECT SQL_NO_CACHE name,age,t_user.id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) FOR UPDATE",
79+
expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) FOR UPDATE",
9080
expectQueryArgs: []driver.Value{100, 18, 28},
9181
},
9282
{
9383
sourceQuery: "update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name desc limit ?",
9484
sourceQueryArgs: []driver.Value{"Jack", 1, 10, 20, 17, "Beijing", "Guangzhou", 18, 2},
95-
expectQuery: "SELECT SQL_NO_CACHE name,age,t_user.id FROM t_user WHERE kk BETWEEN ? AND ? AND id=? AND addr IN (?,?) AND age>? ORDER BY name DESC LIMIT ? FOR UPDATE",
85+
expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE kk BETWEEN ? AND ? AND id=? AND addr IN (?,?) AND age>? ORDER BY name DESC LIMIT ? FOR UPDATE",
9686
expectQueryArgs: []driver.Value{10, 20, 17, "Beijing", "Guangzhou", 18, 2},
9787
},
9888
}
@@ -101,7 +91,7 @@ func TestBuildSelectSQLByUpdate(t *testing.T) {
10191
c, err := parser.DoParser(tt.sourceQuery)
10292
assert.Nil(t, err)
10393
executor := NewUpdateExecutor(c, &types.ExecContext{Values: tt.sourceQueryArgs, NamedValues: util.ValueToNamedValue(tt.sourceQueryArgs)}, []exec.SQLHook{})
104-
query, args, err := executor.(*updateExecutor).buildBeforeImageSQL(context.Background(), MetaDataMap["t_user"], "", util.ValueToNamedValue(tt.sourceQueryArgs))
94+
query, args, err := executor.(*updateExecutor).buildBeforeImageSQL(context.Background(), util.ValueToNamedValue(tt.sourceQueryArgs))
10595
assert.Nil(t, err)
10696
assert.Equal(t, tt.expectQuery, query)
10797
assert.Equal(t, tt.expectQueryArgs, util.NamedValueToValue(args))

0 commit comments

Comments
 (0)