@@ -11,9 +11,11 @@ import {
11
11
mockShowTablesPollingResult ,
12
12
} from '../../../test/datasources' ;
13
13
import {
14
+ createLoadQuery ,
14
15
updateAccelerationsToCache ,
15
16
updateDatabasesToCache ,
16
17
updateTablesToCache ,
18
+ updateToCache ,
17
19
} from './cache_loader' ;
18
20
import { CatalogCacheManager } from './cache_manager' ;
19
21
@@ -146,10 +148,7 @@ describe('loadCacheTests', () => {
146
148
dataSourceName ,
147
149
expect . objectContaining ( {
148
150
name : databaseName ,
149
- tables : [
150
- { name : 'Table1' , columns : [ ] } ,
151
- { name : 'Table2' , columns : [ ] } ,
152
- ] ,
151
+ tables : [ { name : 'Table1' } , { name : 'Table2' } ] ,
153
152
lastUpdated : expect . any ( String ) ,
154
153
status : CachedDataSourceStatus . Updated ,
155
154
} )
@@ -166,55 +165,169 @@ describe('loadCacheTests', () => {
166
165
it ( 'should save empty accelerations cache and status failed when polling result is null' , ( ) => {
167
166
const pollingResult = null ;
168
167
169
- updateAccelerationsToCache ( pollingResult ) ;
168
+ updateAccelerationsToCache ( 'sampleDS' , pollingResult ) ;
170
169
171
170
// Verify that saveAccelerationsCache is called with the correct parameters
172
171
expect ( CatalogCacheManager . saveAccelerationsCache ) . toHaveBeenCalledWith ( {
173
172
version : CATALOG_CACHE_VERSION ,
174
- accelerations : [ ] ,
175
- lastUpdated : expect . any ( String ) ,
176
- status : CachedDataSourceStatus . Failed ,
173
+ dataSources : [
174
+ {
175
+ name : 'sampleDS' ,
176
+ accelerations : [ ] ,
177
+ lastUpdated : expect . any ( String ) ,
178
+ status : CachedDataSourceStatus . Failed ,
179
+ } ,
180
+ ] ,
177
181
} ) ;
178
182
} ) ;
179
183
180
184
it ( 'should save new accelerations cache when polling result is not null' , ( ) => {
181
- updateAccelerationsToCache ( mockShowIndexesPollingResult ) ;
185
+ updateAccelerationsToCache ( 'sampleDS' , mockShowIndexesPollingResult ) ;
182
186
183
187
// Verify that saveAccelerationsCache is called with the correct parameters
184
188
expect ( CatalogCacheManager . saveAccelerationsCache ) . toHaveBeenCalledWith ( {
185
189
version : CATALOG_CACHE_VERSION ,
186
- accelerations : [
187
- {
188
- flintIndexName : 'flint_mys3_default_http_logs_skipping_index' ,
189
- type : 'skipping' ,
190
- database : 'default' ,
191
- table : 'http_logs' ,
192
- indexName : 'skipping_index' ,
193
- autoRefresh : false ,
194
- status : 'Active' ,
195
- } ,
190
+ dataSources : [
196
191
{
197
- flintIndexName : 'flint_mys3_default_http_logs_status_clientip_and_day_index' ,
198
- type : 'covering' ,
199
- database : 'default' ,
200
- table : 'http_logs' ,
201
- indexName : 'status_clientip_and_day' ,
202
- autoRefresh : true ,
203
- status : 'Active' ,
192
+ name : 'sampleDS' ,
193
+ accelerations : [
194
+ {
195
+ flintIndexName : 'flint_mys3_default_http_logs_skipping_index' ,
196
+ type : 'skipping' ,
197
+ database : 'default' ,
198
+ table : 'http_logs' ,
199
+ indexName : 'skipping_index' ,
200
+ autoRefresh : false ,
201
+ status : 'Active' ,
202
+ } ,
203
+ {
204
+ flintIndexName : 'flint_mys3_default_http_logs_status_clientip_and_day_index' ,
205
+ type : 'covering' ,
206
+ database : 'default' ,
207
+ table : 'http_logs' ,
208
+ indexName : 'status_clientip_and_day' ,
209
+ autoRefresh : true ,
210
+ status : 'Active' ,
211
+ } ,
212
+ {
213
+ flintIndexName : 'flint_mys3_default_http_count_view' ,
214
+ type : 'materialized' ,
215
+ database : 'default' ,
216
+ table : '' ,
217
+ indexName : 'http_count_view' ,
218
+ autoRefresh : true ,
219
+ status : 'Active' ,
220
+ } ,
221
+ ] ,
222
+ lastUpdated : expect . any ( String ) ,
223
+ status : CachedDataSourceStatus . Updated ,
204
224
} ,
225
+ ] ,
226
+ } ) ;
227
+ } ) ;
228
+ } ) ;
229
+
230
+ describe ( 'updateToCache' , ( ) => {
231
+ it ( 'should call updateDatabasesToCache when loadCacheType is "databases"' , ( ) => {
232
+ const loadCacheType = 'databases' ;
233
+ const dataSourceName = 'TestDataSource' ;
234
+
235
+ updateToCache ( mockShowDatabasesPollingResult , loadCacheType , dataSourceName ) ;
236
+
237
+ // Verify that addOrUpdateDataSource is called
238
+ expect ( CatalogCacheManager . addOrUpdateDataSource ) . toHaveBeenCalled ( ) ;
239
+ expect ( CatalogCacheManager . updateDatabase ) . not . toHaveBeenCalled ( ) ;
240
+ expect ( CatalogCacheManager . saveAccelerationsCache ) . not . toHaveBeenCalled ( ) ;
241
+ } ) ;
242
+
243
+ it ( 'should call updateTablesToCache when loadCacheType is "tables"' , ( ) => {
244
+ const loadCacheType = 'tables' ;
245
+ const dataSourceName = 'TestDataSource' ;
246
+ const databaseName = 'TestDatabase' ;
247
+
248
+ CatalogCacheManager . addOrUpdateDataSource ( {
249
+ databases : [
205
250
{
206
- flintIndexName : 'flint_mys3_default_http_count_view' ,
207
- type : 'materialized' ,
208
- database : 'default' ,
209
- table : '' ,
210
- indexName : 'http_count_view' ,
211
- autoRefresh : true ,
212
- status : 'Active' ,
251
+ name : databaseName ,
252
+ lastUpdated : '' ,
253
+ status : CachedDataSourceStatus . Empty ,
254
+ tables : [ ] ,
213
255
} ,
214
256
] ,
215
- lastUpdated : expect . any ( String ) ,
257
+ name : dataSourceName ,
258
+ lastUpdated : new Date ( ) . toUTCString ( ) ,
216
259
status : CachedDataSourceStatus . Updated ,
217
260
} ) ;
261
+
262
+ updateToCache ( mockShowTablesPollingResult , loadCacheType , dataSourceName , databaseName ) ;
263
+
264
+ // Verify that updateDatabase is called
265
+ expect ( CatalogCacheManager . addOrUpdateDataSource ) . toHaveBeenCalled ( ) ;
266
+ expect ( CatalogCacheManager . updateDatabase ) . toHaveBeenCalled ( ) ;
267
+ expect ( CatalogCacheManager . saveAccelerationsCache ) . not . toHaveBeenCalled ( ) ;
268
+ } ) ;
269
+
270
+ it ( 'should call updateAccelerationsToCache when loadCacheType is "accelerations"' , ( ) => {
271
+ const loadCacheType = 'accelerations' ;
272
+ const dataSourceName = 'TestDataSource' ;
273
+
274
+ updateToCache ( mockShowIndexesPollingResult , loadCacheType , dataSourceName ) ;
275
+
276
+ // Verify that saveAccelerationsCache is called
277
+ expect ( CatalogCacheManager . addOrUpdateDataSource ) . not . toHaveBeenCalled ( ) ;
278
+ expect ( CatalogCacheManager . updateDatabase ) . not . toHaveBeenCalled ( ) ;
279
+ expect ( CatalogCacheManager . saveAccelerationsCache ) . toHaveBeenCalled ( ) ;
280
+ } ) ;
281
+
282
+ it ( 'should not call any update function when loadCacheType is not recognized' , ( ) => {
283
+ const pollResults = { } ;
284
+ const loadCacheType = '' ;
285
+ const dataSourceName = 'TestDataSource' ;
286
+
287
+ updateToCache ( pollResults , loadCacheType , dataSourceName ) ;
288
+
289
+ // Verify that no update function is called
290
+ expect ( CatalogCacheManager . addOrUpdateDataSource ) . not . toHaveBeenCalled ( ) ;
291
+ expect ( CatalogCacheManager . updateDatabase ) . not . toHaveBeenCalled ( ) ;
292
+ expect ( CatalogCacheManager . saveAccelerationsCache ) . not . toHaveBeenCalled ( ) ;
293
+ } ) ;
294
+ } ) ;
295
+
296
+ describe ( 'createLoadQuery' , ( ) => {
297
+ it ( 'should create a query for loading databases' , ( ) => {
298
+ const loadCacheType = 'databases' ;
299
+ const dataSourceName = 'example' ;
300
+ const expectedQuery = 'SHOW SCHEMAS IN `example`' ;
301
+ expect ( createLoadQuery ( loadCacheType , dataSourceName ) ) . toEqual ( expectedQuery ) ;
302
+ } ) ;
303
+
304
+ it ( 'should create a query for loading tables' , ( ) => {
305
+ const loadCacheType = 'tables' ;
306
+ const dataSourceName = 'example' ;
307
+ const databaseName = 'test' ;
308
+ const expectedQuery = 'SHOW TABLES IN `example`.`test`' ;
309
+ expect ( createLoadQuery ( loadCacheType , dataSourceName , databaseName ) ) . toEqual ( expectedQuery ) ;
310
+ } ) ;
311
+
312
+ it ( 'should create a query for loading accelerations' , ( ) => {
313
+ const loadCacheType = 'accelerations' ;
314
+ const dataSourceName = 'example' ;
315
+ const expectedQuery = 'SHOW FLINT INDEX in `example`' ;
316
+ expect ( createLoadQuery ( loadCacheType , dataSourceName ) ) . toEqual ( expectedQuery ) ;
317
+ } ) ;
318
+
319
+ it ( 'should return an empty string for unknown loadCacheType' , ( ) => {
320
+ const loadCacheType = 'unknownType' ;
321
+ const dataSourceName = 'example' ;
322
+ expect ( createLoadQuery ( loadCacheType , dataSourceName ) ) . toEqual ( '' ) ;
323
+ } ) ;
324
+
325
+ it ( 'should properly handle backticks in database name' , ( ) => {
326
+ const loadCacheType = 'tables' ;
327
+ const dataSourceName = 'example' ;
328
+ const databaseName = '`sample`' ;
329
+ const expectedQuery = 'SHOW TABLES IN `example`.`sample`' ;
330
+ expect ( createLoadQuery ( loadCacheType , dataSourceName , databaseName ) ) . toEqual ( expectedQuery ) ;
218
331
} ) ;
219
332
} ) ;
220
333
} ) ;
0 commit comments