diff --git a/pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go b/pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go index 152c0a47d..238dd5835 100644 --- a/pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go +++ b/pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go @@ -50,22 +50,129 @@ func TestBuildWhereConditionByPKs(t *testing.T) { } func TestBuildLockKey(t *testing.T) { - metaData := types.TableMeta{ - TableName: "test_name", - Indexs: map[string]types.IndexMeta{ - "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}, {ColumnName: "userId"}}}, - }, - } + var builder BasicUndoLogBuilder - records := types.RecordImage{ - TableName: "test_name", - Rows: []types.RowImage{ - {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "userId", Value: "one"}}}, - {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 2}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "userId", Value: "two"}}}, + tests := []struct { + name string + metaData types.TableMeta + records types.RecordImage + expected string + }{ + { + name: "Two Primary Keys", + metaData: types.TableMeta{ + TableName: "test_name", + Indexs: map[string]types.IndexMeta{ + "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}, {ColumnName: "userId"}}}, + }, + }, + records: types.RecordImage{ + TableName: "test_name", + Rows: []types.RowImage{ + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "userId", Value: "one"}}}, + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 2}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "userId", Value: "two"}}}, + }, + }, + expected: "test_name:1_one,2_two", + }, + { + name: "Single Primary Key", + metaData: types.TableMeta{ + TableName: "single_key", + Indexs: map[string]types.IndexMeta{ + "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}}, + }, + }, + records: types.RecordImage{ + TableName: "single_key", + Rows: []types.RowImage{ + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 100}}}, + }, + }, + expected: "single_key:100", + }, + { + name: "Mixed Type Keys", + metaData: types.TableMeta{ + TableName: "mixed_key", + Indexs: map[string]types.IndexMeta{ + "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "name"}, {ColumnName: "age"}}}, + }, + }, + records: types.RecordImage{ + TableName: "mixed_key", + Rows: []types.RowImage{ + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "name", Value: "Alice"}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "age", Value: 25}}}, + }, + }, + expected: "mixed_key:Alice_25", + }, + { + name: "Empty Records", + metaData: types.TableMeta{ + TableName: "empty", + Indexs: map[string]types.IndexMeta{ + "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}}, + }, + }, + records: types.RecordImage{TableName: "empty"}, + expected: "empty:", + }, + { + name: "Duplicate Primary Keys", + metaData: types.TableMeta{ + TableName: "dupes", + Indexs: map[string]types.IndexMeta{ + "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}}, + }, + }, + records: types.RecordImage{ + TableName: "dupes", + Rows: []types.RowImage{ + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}}}, + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}}}, + }, + }, + expected: "dupes:1,1", + }, + { + name: "Special Characters", + metaData: types.TableMeta{ + TableName: "special", + Indexs: map[string]types.IndexMeta{ + "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}}, + }, + }, + records: types.RecordImage{ + TableName: "special", + Rows: []types.RowImage{ + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: "a,b_c"}}}, + }, + }, + expected: "special:a,b_c", + }, + { + name: "Non-existent Key Name", + metaData: types.TableMeta{ + TableName: "error_key", + Indexs: map[string]types.IndexMeta{ + "PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "non_existent"}}}, + }, + }, + records: types.RecordImage{ + TableName: "error_key", + Rows: []types.RowImage{ + {Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}}}, + }, + }, + expected: "error_key:", }, } - builder := BasicUndoLogBuilder{} - lockKeys := builder.buildLockKey2(&records, metaData) - assert.Equal(t, "test_name:1_one,2_two", lockKeys) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + lockKeys := builder.buildLockKey2(&tt.records, tt.metaData) + assert.Equal(t, tt.expected, lockKeys) + }) + } }