From a5a1955debffad0fa487514446b441af66104072 Mon Sep 17 00:00:00 2001 From: lxfeng1997 <824141436@qq.com> Date: Fri, 10 Jan 2025 19:35:28 +0800 Subject: [PATCH] recover update test --- .../sql/exec/at/update_executor_test.go | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/pkg/datasource/sql/exec/at/update_executor_test.go b/pkg/datasource/sql/exec/at/update_executor_test.go index 34173aaf..55b2e139 100644 --- a/pkg/datasource/sql/exec/at/update_executor_test.go +++ b/pkg/datasource/sql/exec/at/update_executor_test.go @@ -20,49 +20,39 @@ package at import ( "context" "database/sql/driver" - "seata.apache.org/seata-go/pkg/datasource/sql/undo" + "github.com/agiledragon/gomonkey/v2" + "reflect" "testing" "github.com/stretchr/testify/assert" + "seata.apache.org/seata-go/pkg/datasource/sql/datasource" + "seata.apache.org/seata-go/pkg/datasource/sql/datasource/mysql" "seata.apache.org/seata-go/pkg/datasource/sql/exec" "seata.apache.org/seata-go/pkg/datasource/sql/parser" "seata.apache.org/seata-go/pkg/datasource/sql/types" + "seata.apache.org/seata-go/pkg/datasource/sql/undo" "seata.apache.org/seata-go/pkg/datasource/sql/util" _ "seata.apache.org/seata-go/pkg/util/log" ) func TestBuildSelectSQLByUpdate(t *testing.T) { - MetaDataMap := map[string]*types.TableMeta{ - "t_user": { - TableName: "t_user", - Indexs: map[string]types.IndexMeta{ - "id": { - IType: types.IndexTypePrimaryKey, - Columns: []types.ColumnMeta{ - {ColumnName: "id"}, + undo.InitUndoConfig(undo.Config{OnlyCareUpdateColumns: true}) + datasource.RegisterTableCache(types.DBTypeMySQL, mysql.NewTableMetaInstance(nil, nil)) + stub := gomonkey.ApplyMethod(reflect.TypeOf(datasource.GetTableCache(types.DBTypeMySQL)), "GetTableMeta", + func(_ *mysql.TableMetaCache, ctx context.Context, dbName, tableName string) (*types.TableMeta, error) { + return &types.TableMeta{ + Indexs: map[string]types.IndexMeta{ + "id": { + IType: types.IndexTypePrimaryKey, + Columns: []types.ColumnMeta{ + {ColumnName: "id"}, + }, }, }, - }, - Columns: map[string]types.ColumnMeta{ - "id": { - ColumnDef: nil, - ColumnName: "id", - }, - "name": { - ColumnDef: nil, - ColumnName: "name", - }, - "age": { - ColumnDef: nil, - ColumnName: "age", - }, - }, - ColumnNames: []string{"id", "name", "age"}, - }, - } - - undo.InitUndoConfig(undo.Config{OnlyCareUpdateColumns: true}) + }, nil + }) + defer stub.Reset() tests := []struct { name string @@ -74,25 +64,25 @@ func TestBuildSelectSQLByUpdate(t *testing.T) { { sourceQuery: "update t_user set name = ?, age = ? where id = ?", sourceQueryArgs: []driver.Value{"Jack", 1, 100}, - expectQuery: "SELECT SQL_NO_CACHE name,age,t_user.id FROM t_user WHERE id=? FOR UPDATE", + expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? FOR UPDATE", expectQueryArgs: []driver.Value{100}, }, { sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age between ? and ?", sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28}, - expectQuery: "SELECT SQL_NO_CACHE name,age,t_user.id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? FOR UPDATE", + expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? FOR UPDATE", expectQueryArgs: []driver.Value{100, 18, 28}, }, { sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age in (?,?)", sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28}, - expectQuery: "SELECT SQL_NO_CACHE name,age,t_user.id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) FOR UPDATE", + expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) FOR UPDATE", expectQueryArgs: []driver.Value{100, 18, 28}, }, { sourceQuery: "update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name desc limit ?", sourceQueryArgs: []driver.Value{"Jack", 1, 10, 20, 17, "Beijing", "Guangzhou", 18, 2}, - 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", + 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", expectQueryArgs: []driver.Value{10, 20, 17, "Beijing", "Guangzhou", 18, 2}, }, } @@ -101,7 +91,7 @@ func TestBuildSelectSQLByUpdate(t *testing.T) { c, err := parser.DoParser(tt.sourceQuery) assert.Nil(t, err) executor := NewUpdateExecutor(c, &types.ExecContext{Values: tt.sourceQueryArgs, NamedValues: util.ValueToNamedValue(tt.sourceQueryArgs)}, []exec.SQLHook{}) - query, args, err := executor.(*updateExecutor).buildBeforeImageSQL(context.Background(), MetaDataMap["t_user"], "", util.ValueToNamedValue(tt.sourceQueryArgs)) + query, args, err := executor.(*updateExecutor).buildBeforeImageSQL(context.Background(), util.ValueToNamedValue(tt.sourceQueryArgs)) assert.Nil(t, err) assert.Equal(t, tt.expectQuery, query) assert.Equal(t, tt.expectQueryArgs, util.NamedValueToValue(args))