@@ -24,6 +24,7 @@ import (
24
24
"testing"
25
25
"time"
26
26
27
+ "github.com/DATA-DOG/go-sqlmock"
27
28
"github.com/agiledragon/gomonkey/v2"
28
29
"github.com/go-sql-driver/mysql"
29
30
"github.com/stretchr/testify/assert"
40
41
type mockTrigger struct {
41
42
}
42
43
44
+ // LoadOne simulates loading table metadata, including id, name, and age columns.
43
45
func (m * mockTrigger ) LoadOne (ctx context.Context , dbName string , table string , conn * sql.Conn ) (* types.TableMeta , error ) {
44
- return nil , nil
46
+
47
+ return & types.TableMeta {
48
+ TableName : table ,
49
+ Columns : map [string ]types.ColumnMeta {
50
+ "id" : {ColumnName : "id" },
51
+ "name" : {ColumnName : "name" },
52
+ "age" : {ColumnName : "age" },
53
+ },
54
+ Indexs : map [string ]types.IndexMeta {
55
+ "id" : {
56
+ Name : "PRIMARY" ,
57
+ IType : types .IndexTypePrimaryKey ,
58
+ Columns : []types.ColumnMeta {{ColumnName : "id" }},
59
+ },
60
+ "id_name_age" : {
61
+ Name : "name_age_idx" ,
62
+ IType : types .IndexUnique ,
63
+ Columns : []types.ColumnMeta {{ColumnName : "name" }, {ColumnName : "age" }},
64
+ },
65
+ },
66
+ ColumnNames : []string {"id" , "name" , "age" },
67
+ }, nil
45
68
}
46
69
47
70
func (m * mockTrigger ) LoadAll (ctx context.Context , dbName string , conn * sql.Conn , tables ... string ) ([]types.TableMeta , error ) {
@@ -77,7 +100,7 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
77
100
size : 0 ,
78
101
expireDuration : EexpireTime ,
79
102
cache : map [string ]* entry {
80
- "test " : {
103
+ "TEST " : {
81
104
value : types.TableMeta {},
82
105
lastAccess : time .Now (),
83
106
},
@@ -120,7 +143,116 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
120
143
go c .refresh (tt .args .ctx )
121
144
time .Sleep (time .Second * 3 )
122
145
123
- assert .Equal (t , c .cache ["test" ].value , tt .want )
146
+ assert .Equal (t , c .cache ["TEST" ].value , tt .want )
147
+ })
148
+ }
149
+ }
150
+
151
+ func TestBaseTableMetaCache_GetTableMeta (t * testing.T ) {
152
+ var (
153
+ tableMeta1 types.TableMeta
154
+ tableMeta2 types.TableMeta
155
+ columns = make (map [string ]types.ColumnMeta )
156
+ index = make (map [string ]types.IndexMeta )
157
+ index2 = make (map [string ]types.IndexMeta )
158
+ columnMeta1 []types.ColumnMeta
159
+ columnMeta2 []types.ColumnMeta
160
+ ColumnNames []string
161
+ )
162
+ columnId := types.ColumnMeta {
163
+ ColumnDef : nil ,
164
+ ColumnName : "id" ,
165
+ }
166
+ columnName := types.ColumnMeta {
167
+ ColumnDef : nil ,
168
+ ColumnName : "name" ,
169
+ }
170
+ columnAge := types.ColumnMeta {
171
+ ColumnDef : nil ,
172
+ ColumnName : "age" ,
173
+ }
174
+ columns ["id" ] = columnId
175
+ columns ["name" ] = columnName
176
+ columns ["age" ] = columnAge
177
+ columnMeta1 = append (columnMeta1 , columnId )
178
+ columnMeta2 = append (columnMeta2 , columnName , columnAge )
179
+ index ["id" ] = types.IndexMeta {
180
+ Name : "PRIMARY" ,
181
+ IType : types .IndexTypePrimaryKey ,
182
+ Columns : columnMeta1 ,
183
+ }
184
+ index ["id_name_age" ] = types.IndexMeta {
185
+ Name : "name_age_idx" ,
186
+ IType : types .IndexUnique ,
187
+ Columns : columnMeta2 ,
188
+ }
189
+
190
+ ColumnNames = []string {"id" , "name" , "age" }
191
+ tableMeta1 = types.TableMeta {
192
+ TableName : "T_USER1" ,
193
+ Columns : columns ,
194
+ Indexs : index ,
195
+ ColumnNames : ColumnNames ,
196
+ }
197
+
198
+ index2 ["id_name_age" ] = types.IndexMeta {
199
+ Name : "name_age_idx" ,
200
+ IType : types .IndexUnique ,
201
+ Columns : columnMeta2 ,
202
+ }
203
+
204
+ tableMeta2 = types.TableMeta {
205
+ TableName : "T_USER2" ,
206
+ Columns : columns ,
207
+ Indexs : index2 ,
208
+ ColumnNames : ColumnNames ,
209
+ }
210
+ tests := []types.TableMeta {tableMeta1 , tableMeta2 }
211
+ // Use sqlmock to simulate a database connection
212
+ db , mock , err := sqlmock .New ()
213
+ if err != nil {
214
+ t .Fatalf ("Failed to create sqlmock: %v" , err )
215
+ }
216
+ defer db .Close ()
217
+ for _ , tt := range tests {
218
+ t .Run (tt .TableName , func (t * testing.T ) {
219
+ mockTrigger := & mockTrigger {}
220
+ // Mock a query response
221
+ mock .ExpectQuery ("SELECT" ).WillReturnRows (sqlmock .NewRows ([]string {"id" , "name" , "age" }))
222
+ // Create a mock database connection
223
+ conn , err := db .Conn (context .Background ())
224
+ if err != nil {
225
+ t .Fatalf ("Failed to get connection: %v" , err )
226
+ }
227
+ defer conn .Close ()
228
+ cache := & BaseTableMetaCache {
229
+ trigger : mockTrigger ,
230
+ cache : map [string ]* entry {
231
+ "T_USER" : {
232
+ value : tableMeta2 ,
233
+ lastAccess : time .Now (),
234
+ },
235
+ "T_USER1" : {
236
+ value : tableMeta1 ,
237
+ lastAccess : time .Now (),
238
+ },
239
+ },
240
+ lock : sync.RWMutex {},
241
+ }
242
+
243
+ meta , _ := cache .GetTableMeta (context .Background (), "db" , tt .TableName , conn )
244
+
245
+ if meta .TableName != tt .TableName {
246
+ t .Errorf ("GetTableMeta() got TableName = %v, want %v" , meta .TableName , tt .TableName )
247
+ }
248
+ // Ensure the retrieved table is cached
249
+ cache .lock .RLock ()
250
+ _ , cached := cache .cache [tt .TableName ]
251
+ cache .lock .RUnlock ()
252
+
253
+ if ! cached {
254
+ t .Errorf ("GetTableMeta() got TableName = %v, want %v" , meta .TableName , tt .TableName )
255
+ }
124
256
})
125
257
}
126
258
}
0 commit comments