11import * as fs from 'fs' ;
2+ import * as duckdb from 'duckdb' ;
23import * as sinon from 'ts-sinon' ;
4+ import * as path from 'path' ;
35import {
46 CacheLayerInfo ,
57 CacheLayerLoader ,
@@ -9,7 +11,7 @@ import {
911 vulcanCacheSchemaName ,
1012} from '@vulcan-sql/core' ;
1113import { MockDataSource , getQueryResults } from './mockDataSource' ;
12-
14+ const db = new duckdb . Database ( ':memory:' ) ;
1315describe ( 'Test cache layer loader' , ( ) => {
1416 const folderPath = 'loader-test-exported-parquets' ;
1517 const profiles = [
@@ -108,4 +110,80 @@ describe('Test cache layer loader', () => {
108110 // Set 50s timeout to test cache loader export and load data
109111 50 * 10000
110112 ) ;
113+ it . each ( [
114+ {
115+ templateName : 'template-1' ,
116+ cache : {
117+ cacheTableName : 'employees' ,
118+ sql : sinon . default . stub ( ) as any ,
119+ profile : profiles [ 0 ] . name ,
120+ folderSubpath : '2023' ,
121+ } as CacheLayerInfo ,
122+ } ,
123+ {
124+ templateName : 'template-1' ,
125+ cache : {
126+ cacheTableName : 'departments' ,
127+ sql : sinon . default . stub ( ) as any ,
128+ profile : profiles [ 1 ] . name ,
129+ folderSubpath : '2023' ,
130+ } as CacheLayerInfo ,
131+ } ,
132+ ] ) (
133+ 'Should use existed parquet to load cache table: $cache.cacheTableName' ,
134+ async ( { templateName, cache } ) => {
135+ // Arrange
136+ const { profile, cacheTableName, folderSubpath } = cache ;
137+ const dir = path . resolve (
138+ folderPath ,
139+ templateName ,
140+ profile ,
141+ cacheTableName ,
142+ folderSubpath !
143+ ) ;
144+ await createParquetFile ( dir , cacheTableName ) ;
145+ // Act
146+ const loader = new CacheLayerLoader ( options , stubFactory as any ) ;
147+ await loader . load ( templateName , cache ) ;
148+
149+ // Assert
150+ const actual = (
151+ await getQueryResults (
152+ "select * from information_schema.tables where table_schema = 'vulcan'"
153+ )
154+ ) . map ( ( row ) => {
155+ return {
156+ table : row [ 'table_name' ] ,
157+ schema : row [ 'table_schema' ] ,
158+ } ;
159+ } ) ;
160+ expect ( actual ) . toEqual (
161+ expect . arrayContaining ( [
162+ {
163+ table : cache . cacheTableName ,
164+ schema : vulcanCacheSchemaName ,
165+ } ,
166+ ] )
167+ ) ;
168+ } ,
169+ // Set 50s timeout to test cache loader export and load data
170+ 50 * 10000
171+ ) ;
111172} ) ;
173+
174+ async function createParquetFile ( path : string , fileName : string ) {
175+ if ( ! fs . existsSync ( path ) ) {
176+ fs . mkdirSync ( path , { recursive : true } ) ;
177+ }
178+ db . run ( `CREATE OR REPLACE TABLE parquet_table (i integer)` ) ;
179+ db . run ( `INSERT INTO parquet_table (i) VALUES (1)` ) ;
180+ return new Promise ( ( resolve , reject ) => {
181+ db . run (
182+ `COPY (SELECT * FROM parquet_table) TO '${ path } /${ fileName } .parquet' (FORMAT PARQUET);` ,
183+ ( err : any ) => {
184+ if ( err ) reject ( err ) ;
185+ resolve ( true ) ;
186+ }
187+ ) ;
188+ } ) ;
189+ }
0 commit comments