@@ -3,6 +3,7 @@ import { MysqlDBRunner, PostgresDBRunner } from '@cubejs-backend/testing-shared'
33import cubejs , { CubeApi } from '@cubejs-client/core' ;
44// eslint-disable-next-line import/no-extraneous-dependencies
55import { afterAll , beforeAll , expect , jest } from '@jest/globals' ;
6+ import { Client as PgClient } from 'pg' ;
67import { BirdBox , getBirdbox } from '../src' ;
78import {
89 DEFAULT_API_TOKEN ,
@@ -11,12 +12,43 @@ import {
1112 JEST_BEFORE_ALL_DEFAULT_TIMEOUT ,
1213} from './smoke-tests' ;
1314
15+ // TODO: Random port?
16+ const pgPort = 5656 ;
17+ let connectionId = 0 ;
18+
19+ async function createPostgresClient ( user : string , password : string ) {
20+ connectionId ++ ;
21+ const currentConnId = connectionId ;
22+
23+ console . debug ( `[pg] new connection ${ currentConnId } ` ) ;
24+
25+ const conn = new PgClient ( {
26+ database : 'db' ,
27+ port : pgPort ,
28+ host : '127.0.0.1' ,
29+ user,
30+ password,
31+ ssl : false ,
32+ } ) ;
33+ conn . on ( 'error' , ( err ) => {
34+ console . log ( err ) ;
35+ } ) ;
36+ conn . on ( 'end' , ( ) => {
37+ console . debug ( `[pg] end ${ currentConnId } ` ) ;
38+ } ) ;
39+
40+ await conn . connect ( ) ;
41+
42+ return conn ;
43+ }
44+
1445describe ( 'multidb' , ( ) => {
1546 jest . setTimeout ( 60 * 5 * 1000 ) ;
1647 let db : StartedTestContainer ;
1748 let db2 : StartedTestContainer ;
1849 let birdbox : BirdBox ;
1950 let client : CubeApi ;
51+ let connection : PgClient ;
2052
2153 beforeAll ( async ( ) => {
2254 db = await PostgresDBRunner . startContainer ( { } ) ;
@@ -39,6 +71,9 @@ describe('multidb', () => {
3971 CUBEJS_DB_USER2 : 'root' ,
4072 CUBEJS_DB_PASS2 : 'Test1test' ,
4173
74+ CUBEJS_PG_SQL_PORT : `${ pgPort } ` ,
75+ CUBESQL_SQL_PUSH_DOWN : 'true' ,
76+
4277 ...DEFAULT_CONFIG ,
4378 } ,
4479 {
@@ -49,6 +84,7 @@ describe('multidb', () => {
4984 client = cubejs ( async ( ) => DEFAULT_API_TOKEN , {
5085 apiUrl : birdbox . configuration . apiUrl ,
5186 } ) ;
87+ connection = await createPostgresClient ( 'admin' , 'admin_password' ) ;
5288 } , JEST_BEFORE_ALL_DEFAULT_TIMEOUT ) ;
5389
5490 afterAll ( async ( ) => {
@@ -69,4 +105,32 @@ describe('multidb', () => {
69105 } ) ;
70106 expect ( response . rawData ( ) ) . toMatchSnapshot ( 'query' ) ;
71107 } ) ;
108+
109+ test ( 'SQL pushdown queries to different data sources: Products' , async ( ) => {
110+ const res = await connection . query ( `
111+ SELECT
112+ name
113+ FROM
114+ Products
115+ WHERE
116+ LOWER(name) = 'apples'
117+ GROUP BY
118+ 1
119+ ` ) ;
120+ expect ( res . rows ) . toMatchSnapshot ( ) ;
121+ } ) ;
122+
123+ test ( 'SQL pushdown queries to different data sources: Suppliers' , async ( ) => {
124+ const res = await connection . query ( `
125+ SELECT
126+ company
127+ FROM
128+ Suppliers
129+ WHERE
130+ LOWER(company) = 'fruits inc'
131+ GROUP BY
132+ 1
133+ ` ) ;
134+ expect ( res . rows ) . toMatchSnapshot ( ) ;
135+ } ) ;
72136} ) ;
0 commit comments