@@ -52,41 +52,64 @@ func TestBuildWhereConditionByPKs(t *testing.T) {
52
52
func TestBuildLockKey (t * testing.T ) {
53
53
var builder BasicUndoLogBuilder
54
54
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
+
55
78
tests := []struct {
56
79
name string
57
80
metaData types.TableMeta
58
81
records types.RecordImage
59
82
expected string
60
83
}{
61
84
{
62
- name : "Two Primary Keys" ,
63
- metaData : types.TableMeta {
85
+ "Two Primary Keys" ,
86
+ types.TableMeta {
64
87
TableName : "test_name" ,
65
88
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 },
67
90
},
68
91
},
69
- records : types.RecordImage {
92
+ types.RecordImage {
70
93
TableName : "test_name" ,
71
94
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" ) }},
74
97
},
75
98
},
76
- expected : "test_name:1_one,2_two" ,
99
+ "test_name:1_one,2_two" ,
77
100
},
78
101
{
79
102
name : "Single Primary Key" ,
80
103
metaData : types.TableMeta {
81
104
TableName : "single_key" ,
82
105
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 }},
84
107
},
85
108
},
86
109
records : types.RecordImage {
87
110
TableName : "single_key" ,
88
111
Rows : []types.RowImage {
89
- {Columns : []types.ColumnImage {{ KeyType : types . IndexTypePrimaryKey , ColumnName : "id" , Value : 100 } }},
112
+ {Columns : []types.ColumnImage {getColumnImage ( "id" , 100 ) }},
90
113
},
91
114
},
92
115
expected : "single_key:100" ,
@@ -96,13 +119,13 @@ func TestBuildLockKey(t *testing.T) {
96
119
metaData : types.TableMeta {
97
120
TableName : "mixed_key" ,
98
121
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 },
100
123
},
101
124
},
102
125
records : types.RecordImage {
103
126
TableName : "mixed_key" ,
104
127
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 ) }},
106
129
},
107
130
},
108
131
expected : "mixed_key:Alice_25" ,
@@ -112,41 +135,24 @@ func TestBuildLockKey(t *testing.T) {
112
135
metaData : types.TableMeta {
113
136
TableName : "empty" ,
114
137
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 }},
116
139
},
117
140
},
118
141
records : types.RecordImage {TableName : "empty" },
119
142
expected : "empty:" ,
120
143
},
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
- },
138
144
{
139
145
name : "Special Characters" ,
140
146
metaData : types.TableMeta {
141
147
TableName : "special" ,
142
148
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 }},
144
150
},
145
151
},
146
152
records : types.RecordImage {
147
153
TableName : "special" ,
148
154
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" ) }},
150
156
},
151
157
},
152
158
expected : "special:a,b_c" ,
@@ -156,13 +162,13 @@ func TestBuildLockKey(t *testing.T) {
156
162
metaData : types.TableMeta {
157
163
TableName : "error_key" ,
158
164
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 }},
160
166
},
161
167
},
162
168
records : types.RecordImage {
163
169
TableName : "error_key" ,
164
170
Rows : []types.RowImage {
165
- {Columns : []types.ColumnImage {{ KeyType : types . IndexTypePrimaryKey , ColumnName : "id" , Value : 1 } }},
171
+ {Columns : []types.ColumnImage {getColumnImage ( "id" , 1 ) }},
166
172
},
167
173
},
168
174
expected : "error_key:" ,
0 commit comments