Skip to content

Commit 886d3b6

Browse files
author
FinnTew
committed
update the unit test of buildLockKey
1 parent f1428a9 commit 886d3b6

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,41 +52,64 @@ func TestBuildWhereConditionByPKs(t *testing.T) {
5252
func TestBuildLockKey(t *testing.T) {
5353
var builder BasicUndoLogBuilder
5454

55+
columnID := types.ColumnMeta{
56+
ColumnName: "id",
57+
}
58+
columnUserId := types.ColumnMeta{
59+
ColumnName: "userId",
60+
}
61+
columnName := types.ColumnMeta{
62+
ColumnName: "name",
63+
}
64+
columnAge := types.ColumnMeta{
65+
ColumnName: "age",
66+
}
67+
columnNonExistent := types.ColumnMeta{
68+
ColumnName: "non_existent",
69+
}
70+
71+
columnsTwoPk := []types.ColumnMeta{columnID, columnUserId}
72+
columnsMixPk := []types.ColumnMeta{columnName, columnAge}
73+
74+
getColumnImage := func(columnName string, value interface{}) types.ColumnImage {
75+
return types.ColumnImage{KeyType: types.IndexTypePrimaryKey, ColumnName: columnName, Value: value}
76+
}
77+
5578
tests := []struct {
5679
name string
5780
metaData types.TableMeta
5881
records types.RecordImage
5982
expected string
6083
}{
6184
{
62-
name: "Two Primary Keys",
63-
metaData: types.TableMeta{
85+
"Two Primary Keys",
86+
types.TableMeta{
6487
TableName: "test_name",
6588
Indexs: map[string]types.IndexMeta{
66-
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}, {ColumnName: "userId"}}},
89+
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: columnsTwoPk},
6790
},
6891
},
69-
records: types.RecordImage{
92+
types.RecordImage{
7093
TableName: "test_name",
7194
Rows: []types.RowImage{
72-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "userId", Value: "one"}}},
73-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 2}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "userId", Value: "two"}}},
95+
{[]types.ColumnImage{getColumnImage("id", 1), getColumnImage("userId", "one")}},
96+
{[]types.ColumnImage{getColumnImage("id", 2), getColumnImage("userId", "two")}},
7497
},
7598
},
76-
expected: "test_name:1_one,2_two",
99+
"test_name:1_one,2_two",
77100
},
78101
{
79102
name: "Single Primary Key",
80103
metaData: types.TableMeta{
81104
TableName: "single_key",
82105
Indexs: map[string]types.IndexMeta{
83-
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}},
106+
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{columnID}},
84107
},
85108
},
86109
records: types.RecordImage{
87110
TableName: "single_key",
88111
Rows: []types.RowImage{
89-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 100}}},
112+
{Columns: []types.ColumnImage{getColumnImage("id", 100)}},
90113
},
91114
},
92115
expected: "single_key:100",
@@ -96,13 +119,13 @@ func TestBuildLockKey(t *testing.T) {
96119
metaData: types.TableMeta{
97120
TableName: "mixed_key",
98121
Indexs: map[string]types.IndexMeta{
99-
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "name"}, {ColumnName: "age"}}},
122+
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: columnsMixPk},
100123
},
101124
},
102125
records: types.RecordImage{
103126
TableName: "mixed_key",
104127
Rows: []types.RowImage{
105-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "name", Value: "Alice"}, {KeyType: types.IndexTypePrimaryKey, ColumnName: "age", Value: 25}}},
128+
{Columns: []types.ColumnImage{getColumnImage("name", "Alice"), getColumnImage("age", 25)}},
106129
},
107130
},
108131
expected: "mixed_key:Alice_25",
@@ -112,41 +135,24 @@ func TestBuildLockKey(t *testing.T) {
112135
metaData: types.TableMeta{
113136
TableName: "empty",
114137
Indexs: map[string]types.IndexMeta{
115-
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}},
138+
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{columnID}},
116139
},
117140
},
118141
records: types.RecordImage{TableName: "empty"},
119142
expected: "empty:",
120143
},
121-
{
122-
name: "Duplicate Primary Keys",
123-
metaData: types.TableMeta{
124-
TableName: "dupes",
125-
Indexs: map[string]types.IndexMeta{
126-
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}},
127-
},
128-
},
129-
records: types.RecordImage{
130-
TableName: "dupes",
131-
Rows: []types.RowImage{
132-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}}},
133-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}}},
134-
},
135-
},
136-
expected: "dupes:1,1",
137-
},
138144
{
139145
name: "Special Characters",
140146
metaData: types.TableMeta{
141147
TableName: "special",
142148
Indexs: map[string]types.IndexMeta{
143-
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "id"}}},
149+
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{columnID}},
144150
},
145151
},
146152
records: types.RecordImage{
147153
TableName: "special",
148154
Rows: []types.RowImage{
149-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: "a,b_c"}}},
155+
{Columns: []types.ColumnImage{getColumnImage("id", "a,b_c")}},
150156
},
151157
},
152158
expected: "special:a,b_c",
@@ -156,13 +162,13 @@ func TestBuildLockKey(t *testing.T) {
156162
metaData: types.TableMeta{
157163
TableName: "error_key",
158164
Indexs: map[string]types.IndexMeta{
159-
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{{ColumnName: "non_existent"}}},
165+
"PRIMARY_KEY": {IType: types.IndexTypePrimaryKey, Columns: []types.ColumnMeta{columnNonExistent}},
160166
},
161167
},
162168
records: types.RecordImage{
163169
TableName: "error_key",
164170
Rows: []types.RowImage{
165-
{Columns: []types.ColumnImage{{KeyType: types.IndexTypePrimaryKey, ColumnName: "id", Value: 1}}},
171+
{Columns: []types.ColumnImage{getColumnImage("id", 1)}},
166172
},
167173
},
168174
expected: "error_key:",

0 commit comments

Comments
 (0)