Skip to content

Commit

Permalink
improve unit testing of buildLockKey
Browse files Browse the repository at this point in the history
  • Loading branch information
FinnTew committed Nov 28, 2024
1 parent 4d8e1e7 commit 13d9b7a
Showing 1 changed file with 121 additions and 14 deletions.
135 changes: 121 additions & 14 deletions pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 13d9b7a

Please sign in to comment.