@@ -20,49 +20,39 @@ package at
20
20
import (
21
21
"context"
22
22
"database/sql/driver"
23
- "seata.apache.org/seata-go/pkg/datasource/sql/undo"
23
+ "github.com/agiledragon/gomonkey/v2"
24
+ "reflect"
24
25
"testing"
25
26
26
27
"github.com/stretchr/testify/assert"
27
28
29
+ "seata.apache.org/seata-go/pkg/datasource/sql/datasource"
30
+ "seata.apache.org/seata-go/pkg/datasource/sql/datasource/mysql"
28
31
"seata.apache.org/seata-go/pkg/datasource/sql/exec"
29
32
"seata.apache.org/seata-go/pkg/datasource/sql/parser"
30
33
"seata.apache.org/seata-go/pkg/datasource/sql/types"
34
+ "seata.apache.org/seata-go/pkg/datasource/sql/undo"
31
35
"seata.apache.org/seata-go/pkg/datasource/sql/util"
32
36
_ "seata.apache.org/seata-go/pkg/util/log"
33
37
)
34
38
35
39
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
+ },
44
51
},
45
52
},
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 ()
66
56
67
57
tests := []struct {
68
58
name string
@@ -74,25 +64,25 @@ func TestBuildSelectSQLByUpdate(t *testing.T) {
74
64
{
75
65
sourceQuery : "update t_user set name = ?, age = ? where id = ?" ,
76
66
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" ,
78
68
expectQueryArgs : []driver.Value {100 },
79
69
},
80
70
{
81
71
sourceQuery : "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age between ? and ?" ,
82
72
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" ,
84
74
expectQueryArgs : []driver.Value {100 , 18 , 28 },
85
75
},
86
76
{
87
77
sourceQuery : "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age in (?,?)" ,
88
78
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" ,
90
80
expectQueryArgs : []driver.Value {100 , 18 , 28 },
91
81
},
92
82
{
93
83
sourceQuery : "update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name desc limit ?" ,
94
84
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" ,
96
86
expectQueryArgs : []driver.Value {10 , 20 , 17 , "Beijing" , "Guangzhou" , 18 , 2 },
97
87
},
98
88
}
@@ -101,7 +91,7 @@ func TestBuildSelectSQLByUpdate(t *testing.T) {
101
91
c , err := parser .DoParser (tt .sourceQuery )
102
92
assert .Nil (t , err )
103
93
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 ))
105
95
assert .Nil (t , err )
106
96
assert .Equal (t , tt .expectQuery , query )
107
97
assert .Equal (t , tt .expectQueryArgs , util .NamedValueToValue (args ))
0 commit comments