diff --git a/packages/cubejs-client-core/src/SqlQuery.ts b/packages/cubejs-client-core/src/SqlQuery.ts index 2c450d311ca8c..fc63ea67dace9 100644 --- a/packages/cubejs-client-core/src/SqlQuery.ts +++ b/packages/cubejs-client-core/src/SqlQuery.ts @@ -1,24 +1,26 @@ -export type SqlQueryTuple = [string, any[], any]; +export type SqlQueryTuple = [string, any[], any?]; export type SqlData = { aliasNameToMember: Record; cacheKeyQueries: SqlQueryTuple[]; - dataSource: boolean; + dataSource: string; external: boolean; sql: SqlQueryTuple; preAggregations: any[]; rollupMatchResults: any[]; }; +export type SqlQueryWrapper = { sql: SqlData }; + export default class SqlQuery { - private readonly sqlQuery: SqlData; + private readonly sqlQuery: SqlQueryWrapper; - public constructor(sqlQuery: SqlData) { + public constructor(sqlQuery: SqlQueryWrapper) { this.sqlQuery = sqlQuery; } public rawQuery(): SqlData { - return this.sqlQuery; + return this.sqlQuery.sql; } public sql(): string { diff --git a/packages/cubejs-client-core/test/SqlQuery.test.ts b/packages/cubejs-client-core/test/SqlQuery.test.ts new file mode 100644 index 0000000000000..306004fff7248 --- /dev/null +++ b/packages/cubejs-client-core/test/SqlQuery.test.ts @@ -0,0 +1,53 @@ +/** + * @license Apache-2.0 + * @copyright Cube Dev, Inc. + * @fileoverview SqlQuery class unit tests. + */ + +/* globals describe,it,expect */ + +import SqlQuery, { SqlQueryTuple, SqlData, SqlQueryWrapper } from '../src/SqlQuery'; + +describe('SqlQuery', () => { + const mockCacheKeyQueriesTuple: SqlQueryTuple = [ + 'SELECT FLOOR((-25200 + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key', + [], + { + external: false, + renewalThreshold: 60 + } + ]; + + const mockSqlTuple: SqlQueryTuple = [ + 'SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?', + ['Europe'], + ]; + + const mockSqlData: SqlData = { + aliasNameToMember: { base_orders__count: 'base_orders.count' }, + cacheKeyQueries: [mockCacheKeyQueriesTuple], + dataSource: 'default', + external: false, + sql: mockSqlTuple, + preAggregations: [], + rollupMatchResults: [], + }; + + const mockWrapper: SqlQueryWrapper = { + sql: mockSqlData, + }; + + it('should construct without error', () => { + expect(() => new SqlQuery(mockWrapper)).not.toThrow(); + }); + + it('rawQuery should return the original SqlData', () => { + const query = new SqlQuery(mockWrapper); + expect(query.rawQuery()).toEqual(mockSqlData); + }); + + it('sql should return the first element (SQL string) from the sql tuple', () => { + const query = new SqlQuery(mockWrapper); + expect(query.sql()).toBe('SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?'); + }); +});