@@ -39,7 +39,7 @@ cube('Bar', {
3939 type: 'count'
4040 }
4141 },
42-
42+
4343 dimensions: {
4444 time: {
4545 sql: 'timestamp',
@@ -57,6 +57,28 @@ const repositoryWithoutContent: SchemaFileRepository = {
5757 dataSchemaFiles : ( ) => Promise . resolve ( [ { fileName : 'main.js' , content : '' } ] ) ,
5858} ;
5959
60+ const repositoryWithDataSource : SchemaFileRepository = {
61+ localPath : ( ) => __dirname ,
62+ dataSchemaFiles : ( ) => Promise . resolve ( [ { fileName : 'main.js' , content : `
63+ cube('Bar', {
64+ sql: 'select * from bar',
65+
66+ measures: {
67+ count: {
68+ type: 'count'
69+ }
70+ },
71+ dimensions: {
72+ time: {
73+ sql: 'timestamp',
74+ type: 'time'
75+ }
76+ },
77+ dataSource: 'main'
78+ });
79+ ` } ] ) ,
80+ } ;
81+
6082describe ( 'index.test' , ( ) => {
6183 beforeEach ( ( ) => {
6284 delete process . env . CUBEJS_EXT_DB_TYPE ;
@@ -366,6 +388,49 @@ describe('index.test', () => {
366388 expect ( metaConfigExtendedSpy ) . toHaveBeenCalled ( ) ;
367389 metaConfigExtendedSpy . mockClear ( ) ;
368390 } ) ;
391+
392+ test ( 'CompilerApi dataSources default' , async ( ) => {
393+ const dataSources = await compilerApi . dataSources ( {
394+ driverFactory : jest . fn ( async ( ) => true )
395+ } ) ;
396+
397+ expect ( dataSources ) . toHaveProperty ( 'dataSources' ) ;
398+ expect ( dataSources . dataSources ) . toEqual ( [ ] ) ;
399+ } ) ;
400+ } ) ;
401+
402+ describe ( 'CompilerApi dataSources method' , ( ) => {
403+ const logger = jest . fn ( ( ) => { } ) ;
404+ const compilerApi = new CompilerApi (
405+ repositoryWithDataSource ,
406+ async ( ) => 'mysql' ,
407+ { logger }
408+ ) ;
409+
410+ const dataSourcesSpy = jest . spyOn ( compilerApi , 'dataSources' ) ;
411+ test ( 'CompilerApi dataSources' , async ( ) => {
412+ const dataSources = await compilerApi . dataSources ( {
413+ driverFactory : jest . fn ( async ( ) => true )
414+ } ) ;
415+
416+ expect ( dataSources ) . toHaveProperty ( 'dataSources' ) ;
417+ expect ( dataSources . dataSources ) . toEqual ( [ 'main' ] ) ;
418+ expect ( dataSourcesSpy ) . toHaveBeenCalled ( ) ;
419+ dataSourcesSpy . mockClear ( ) ;
420+ } ) ;
421+
422+ test ( 'CompilerApi dataSources with driverFactory error' , async ( ) => {
423+ const dataSources = await compilerApi . dataSources ( {
424+ driverFactory : jest . fn ( async ( ) => {
425+ throw new Error ( 'Some driverFactory error' ) ;
426+ } )
427+ } ) ;
428+
429+ expect ( dataSources ) . toHaveProperty ( 'dataSources' ) ;
430+ expect ( dataSources . dataSources ) . toEqual ( [ ] ) ;
431+ expect ( dataSourcesSpy ) . toHaveBeenCalled ( ) ;
432+ dataSourcesSpy . mockClear ( ) ;
433+ } ) ;
369434 } ) ;
370435
371436 test ( 'Should create instance of CubejsServerCore, dbType from process.env.CUBEJS_DB_TYPE' , ( ) => {
0 commit comments