Skip to content

Commit b659fb7

Browse files
committed
add tests
1 parent 933b8f1 commit b659fb7

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
cubes:
2+
- name: line_items
3+
sql_table: public.line_items
4+
5+
dimensions:
6+
- name: id
7+
sql: id
8+
type: number
9+
primary_key: true
10+
11+
- name: product_id
12+
sql: product_id
13+
type: number
14+
15+
- name: order_id
16+
sql: order_id
17+
type: number
18+
19+
- name: quantity
20+
sql: quantity
21+
type: number
22+
23+
- name: price
24+
sql: price
25+
type: number
26+
27+
- name: orders
28+
sql_table: public.orders
29+
joins:
30+
- name: line_items
31+
sql: "{CUBE}.id = {line_items}.order_id"
32+
relationship: one_to_many
33+
dimensions:
34+
- name: id
35+
sql: id
36+
type: number
37+
primary_key: true
38+
- name: user_id
39+
sql: user_id
40+
type: number
41+
- name: amount
42+
sql: amount
43+
type: number
44+
- name: status
45+
sql: status
46+
type: string
47+
- name: status_new
48+
sql: status || '_new'
49+
type: string
50+
- name: created_at
51+
sql: created_at
52+
type: time
53+
measures:
54+
- name: count
55+
type: count
56+
- name: total_qty
57+
type: sum
58+
sql: amount
59+
60+
pre_aggregations:
61+
- name: pre_agg_with_multiplied_measures
62+
dimensions:
63+
- orders.status
64+
- line_items.product_id
65+
measures:
66+
- orders.count
67+
- orders.total_qty
68+
timeDimension: orders.created_at
69+
granularity: month

packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts

+163
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'fs';
2+
import path from 'path';
13
import { prepareJsCompiler, prepareYamlCompiler } from './PrepareCompiler';
24
import { createECommerceSchema, createSchemaYaml } from './utils';
35
import { PostgresQuery, queryClass, QueryFactory } from '../../src';
@@ -447,4 +449,165 @@ describe('pre-aggregations', () => {
447449
expect(preAggregationsDescription[0].invalidateKeyQueries[0][0].includes('WHERE ((date(created_at) >= $1::timestamptz AND date(created_at) <= $2::timestamptz))')).toBeTruthy();
448450
expect(preAggregationsDescription[0].invalidateKeyQueries[0][1]).toEqual(['__FROM_PARTITION_RANGE', '__TO_PARTITION_RANGE']);
449451
});
452+
453+
describe('rollup with multiplied measure', () => {
454+
let compiler;
455+
let cubeEvaluator;
456+
let joinGraph;
457+
458+
beforeAll(async () => {
459+
const modelContent = fs.readFileSync(
460+
path.join(process.cwd(), '/test/unit/fixtures/orders_and_items_multiplied_pre_agg.yml'),
461+
'utf8'
462+
);
463+
({ compiler, cubeEvaluator, joinGraph } = prepareYamlCompiler(modelContent));
464+
await compiler.compile();
465+
});
466+
467+
it('measure is unmultiplied in query but multiplied in pre-agg', async () => {
468+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
469+
measures: [
470+
'orders.total_qty'
471+
],
472+
dimensions: [],
473+
timeDimensions: [
474+
{
475+
dimension: 'orders.created_at',
476+
dateRange: [
477+
'2017-05-01',
478+
'2025-05-01'
479+
]
480+
}
481+
]
482+
});
483+
484+
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
485+
// Pre-agg should not match
486+
expect(preAggregationsDescription).toEqual([]);
487+
});
488+
489+
it('measure is unmultiplied in query but multiplied in pre-agg + granularity', async () => {
490+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
491+
measures: [
492+
'orders.total_qty'
493+
],
494+
dimensions: [],
495+
timeDimensions: [
496+
{
497+
dimension: 'orders.created_at',
498+
dateRange: [
499+
'2017-05-01',
500+
'2025-05-01'
501+
],
502+
granularity: 'month'
503+
}
504+
]
505+
});
506+
507+
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
508+
// Pre-agg should not match
509+
expect(preAggregationsDescription).toEqual([]);
510+
});
511+
512+
it('measure is unmultiplied in query but multiplied in pre-agg + granularity + local dimension', async () => {
513+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
514+
measures: [
515+
'orders.total_qty'
516+
],
517+
dimensions: [
518+
'orders.status'
519+
],
520+
timeDimensions: [
521+
{
522+
dimension: 'orders.created_at',
523+
dateRange: [
524+
'2017-05-01',
525+
'2025-05-01'
526+
],
527+
granularity: 'month'
528+
}
529+
]
530+
});
531+
532+
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
533+
// Pre-agg should not match
534+
expect(preAggregationsDescription).toEqual([]);
535+
});
536+
537+
it('measure is unmultiplied in query but multiplied in pre-agg + granularity + external dimension', async () => {
538+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
539+
measures: [
540+
'orders.total_qty'
541+
],
542+
dimensions: [
543+
'line_items.product_id'
544+
],
545+
timeDimensions: [
546+
{
547+
dimension: 'orders.created_at',
548+
dateRange: [
549+
'2017-05-01',
550+
'2025-05-01'
551+
],
552+
granularity: 'month'
553+
}
554+
]
555+
});
556+
557+
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
558+
// Pre-agg should not match
559+
expect(preAggregationsDescription).toEqual([]);
560+
});
561+
562+
it('partial-match of query with pre-agg: 1 measure + all dimensions, no granularity', async () => {
563+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
564+
measures: [
565+
'orders.total_qty'
566+
],
567+
dimensions: [
568+
'orders.status',
569+
'line_items.product_id'
570+
],
571+
timeDimensions: [
572+
{
573+
dimension: 'orders.created_at',
574+
dateRange: [
575+
'2017-05-01',
576+
'2025-05-01'
577+
]
578+
}
579+
]
580+
});
581+
582+
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
583+
// Pre-agg should not match
584+
expect(preAggregationsDescription).toEqual([]);
585+
});
586+
587+
it('full-match of query with pre-agg: 1 measure + granularity + all dimensions', async () => {
588+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
589+
measures: [
590+
'orders.total_qty'
591+
],
592+
dimensions: [
593+
'orders.status',
594+
'line_items.product_id'
595+
],
596+
timeDimensions: [
597+
{
598+
dimension: 'orders.created_at',
599+
dateRange: [
600+
'2017-05-01',
601+
'2025-05-01'
602+
],
603+
granularity: 'month'
604+
}
605+
]
606+
});
607+
608+
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
609+
expect(preAggregationsDescription.length).toEqual(1);
610+
expect(preAggregationsDescription[0].preAggregationId).toEqual('orders.pre_agg_with_multiplied_measures');
611+
});
612+
});
450613
});

0 commit comments

Comments
 (0)