Skip to content

Commit feb9393

Browse files
author
Usman Yasin
committed
fix: correct SqlQuery types and add unit tests
1 parent 76a5b71 commit feb9393

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

packages/cubejs-client-core/src/SqlQuery.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
export type SqlQueryTuple = [string, any[], any];
1+
export type SqlQueryTuple = [string, any[], any?];
22

33
export type SqlData = {
44
aliasNameToMember: Record<string, string>;
55
cacheKeyQueries: SqlQueryTuple[];
6-
dataSource: boolean;
6+
dataSource: string;
77
external: boolean;
88
sql: SqlQueryTuple;
99
preAggregations: any[];
1010
rollupMatchResults: any[];
1111
};
1212

13-
type SqlQueryWrapper = { sql: SqlData };
13+
export type SqlQueryWrapper = { sql: SqlData };
1414

1515
export default class SqlQuery {
1616
private readonly sqlQuery: SqlQueryWrapper;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license Apache-2.0
3+
* @copyright Cube Dev, Inc.
4+
* @fileoverview SqlQuery class unit tests.
5+
*/
6+
7+
/* globals describe,it,expect */
8+
9+
import SqlQuery, { SqlQueryTuple, SqlData, SqlQueryWrapper } from '../src/SqlQuery';
10+
11+
describe('SqlQuery', () => {
12+
const mockCacheKeyQueriesTuple: SqlQueryTuple = [
13+
'SELECT FLOOR((-25200 + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key',
14+
[],
15+
{
16+
external: false,
17+
renewalThreshold: 60
18+
}
19+
];
20+
21+
const mockSqlTuple: SqlQueryTuple = [
22+
'SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?',
23+
['Europe'],
24+
];
25+
26+
const mockSqlData: SqlData = {
27+
aliasNameToMember: { base_orders__count: 'base_orders.count' },
28+
cacheKeyQueries: [mockCacheKeyQueriesTuple],
29+
dataSource: 'default',
30+
external: false,
31+
sql: mockSqlTuple,
32+
preAggregations: [],
33+
rollupMatchResults: [],
34+
};
35+
36+
const mockWrapper: SqlQueryWrapper = {
37+
sql: mockSqlData,
38+
};
39+
40+
it('should construct without error', () => {
41+
expect(() => new SqlQuery(mockWrapper)).not.toThrow();
42+
});
43+
44+
it('rawQuery should return the original SqlData', () => {
45+
const query = new SqlQuery(mockWrapper);
46+
expect(query.rawQuery()).toEqual(mockSqlData);
47+
});
48+
49+
it('sql should return the first element (SQL string) from the sql tuple', () => {
50+
const query = new SqlQuery(mockWrapper);
51+
expect(query.sql()).toBe('SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?');
52+
});
53+
});

0 commit comments

Comments
 (0)