Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Sep 30, 2024
1 parent 577aa38 commit 3c4a4cc
Showing 1 changed file with 84 additions and 9 deletions.
93 changes: 84 additions & 9 deletions packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@ describe('pre-aggregations', () => {
`
cube(\`Users\`, {
sql: \`SELECT * FROM public.users\`,
preAggregations: {
usersRollup: {
dimensions: [CUBE.id],
},
},
measures: {
count: {
type: \`count\`,
},
},
dimensions: {
id: {
sql: \`id\`,
type: \`string\`,
primaryKey: true,
},
name: {
sql: \`name\`,
type: \`string\`,
},
},
});
cube('Orders', {
sql: \`SELECT * FROM orders\`,
preAggregations: {
ordersRollup: {
measures: [CUBE.count],
Expand All @@ -52,20 +52,20 @@ describe('pre-aggregations', () => {
rollups: [Users.usersRollup, CUBE.ordersRollup],
},
},
joins: {
Users: {
relationship: \`belongsTo\`,
sql: \`\${CUBE.userId} = \${Users.id}\`,
},
},
measures: {
count: {
type: \`count\`,
},
},
dimensions: {
id: {
sql: \`id\`,
Expand Down Expand Up @@ -232,4 +232,79 @@ describe('pre-aggregations', () => {
expect(indexesSql[0].indexName).toEqual('orders_indexes_orders_by_day_with_day_by_status_regular_index');
expect(indexesSql[1].indexName).toEqual('orders_indexes_orders_by_day_with_day_by_status_agg_index');
});

it('pre-aggregation with FILTER_PARAMS', async () => {
const { compiler, cubeEvaluator, joinGraph } = prepareYamlCompiler(
createSchemaYaml({
cubes: [
{
name: 'orders',
sql_table: 'orders',
measures: [{
name: 'count',
type: 'count',
}],
dimensions: [
{
name: 'created_at',
sql: 'created_at',
type: 'time',
},
{
name: 'updated_at',
sql: '{created_at}',
type: 'time',
},
{
name: 'status',
sql: 'status',
type: 'string',
}
],
preAggregations: [
{
name: 'orders_by_day_with_day',
measures: ['count'],
dimensions: ['status'],
timeDimension: 'created_at',
granularity: 'day',
partition_granularity: 'day',
build_range_start: {
sql: 'SELECT \'2022-01-01\'::timestamp',
},
build_range_end: {
sql: 'SELECT \'2024-01-01\'::timestamp'
},
refresh_key: {
every: '4 hours',
sql: `
SELECT max(created_at) as max_created_at
FROM orders
WHERE {FILTER_PARAMS.orders.created_at.filter('date(created_at)')}`,
},
},
]
}
]
})
);

await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
'orders.count'
],
timeDimensions: [{
dimension: 'orders.created_at',
granularity: 'day',
dateRange: ['2023-01-01', '2023-01-10']
}],
dimensions: ['orders.status']
});

const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
expect(preAggregationsDescription[0].loadSql[0].includes('WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz)'));
expect(preAggregationsDescription[0].loadSql[1]).toEqual(['__FROM_PARTITION_RANGE', '__TO_PARTITION_RANGE']);
});
});

0 comments on commit 3c4a4cc

Please sign in to comment.