|
|
@@ -50,22 +50,135 @@ 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 |
|
|
|
|
|
|
|
columnID := types.ColumnMeta{ |
|
|
|
ColumnName: "id", |
|
|
|
} |
|
|
|
columnUserId := types.ColumnMeta{ |
|
|
|
ColumnName: "userId", |
|
|
|
} |
|
|
|
columnName := types.ColumnMeta{ |
|
|
|
ColumnName: "name", |
|
|
|
} |
|
|
|
columnAge := types.ColumnMeta{ |
|
|
|
ColumnName: "age", |
|
|
|
} |
|
|
|
columnNonExistent := types.ColumnMeta{ |
|
|
|
ColumnName: "non_existent", |
|
|
|
} |
|
|
|
|
|
|
|
columnsTwoPk := []types.ColumnMeta{columnID, columnUserId} |
|
|
|
columnsMixPk := []types.ColumnMeta{columnName, columnAge} |
|
|
|
|
|
|
|
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"}}}, |
|
|
|
getColumnImage := func(columnName string, value interface{}) types.ColumnImage { |
|
|
|
return types.ColumnImage{KeyType: types.IndexTypePrimaryKey, ColumnName: columnName, Value: value} |
|
|
|
} |
|
|
|
|
|
|
|
tests := []struct { |
|
|
|
name string |
|
|
|
metaData types.TableMeta |
|
|
|
records types.RecordImage |
|
|
|
expected string |
|
|
|
}{ |
|
|
|
{ |
|
|
|
"Two Primary Keys", |
|
|
|
types.TableMeta{ |
|
|
|
TableName: "test_name", |
|
|
|
Indexs: map[string]types.IndexMeta{ |
|
|
|
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: columnsTwoPk}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
types.RecordImage{ |
|
|
|
TableName: "test_name", |
|
|
|
Rows: []types.RowImage{ |
|
|
|
{[]types.ColumnImage{getColumnImage("id", 1), getColumnImage("userId", "one")}}, |
|
|
|
{[]types.ColumnImage{getColumnImage("id", 2), getColumnImage("userId", "two")}}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
"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{columnID}}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
records: types.RecordImage{ |
|
|
|
TableName: "single_key", |
|
|
|
Rows: []types.RowImage{ |
|
|
|
{Columns: []types.ColumnImage{getColumnImage("id", 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: columnsMixPk}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
records: types.RecordImage{ |
|
|
|
TableName: "mixed_key", |
|
|
|
Rows: []types.RowImage{ |
|
|
|
{Columns: []types.ColumnImage{getColumnImage("name", "Alice"), getColumnImage("age", 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{columnID}}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
records: types.RecordImage{TableName: "empty"}, |
|
|
|
expected: "empty:", |
|
|
|
}, |
|
|
|
{ |
|
|
|
name: "Special Characters", |
|
|
|
metaData: types.TableMeta{ |
|
|
|
TableName: "special", |
|
|
|
Indexs: map[string]types.IndexMeta{ |
|
|
|
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{columnID}}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
records: types.RecordImage{ |
|
|
|
TableName: "special", |
|
|
|
Rows: []types.RowImage{ |
|
|
|
{Columns: []types.ColumnImage{getColumnImage("id", "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{columnNonExistent}}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
records: types.RecordImage{ |
|
|
|
TableName: "error_key", |
|
|
|
Rows: []types.RowImage{ |
|
|
|
{Columns: []types.ColumnImage{getColumnImage("id", 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) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |