Skip to content

Commit 4735d50

Browse files
committed
add tests
1 parent 662617c commit 4735d50

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

packages/cubejs-schema-compiler/test/integration/postgres/yaml-compiler.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,4 +641,86 @@ views:
641641
}]
642642
);
643643
});
644+
645+
it('calling cube\'s sql()', async () => {
646+
const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(
647+
`cubes:
648+
- name: simple_orders
649+
sql: >
650+
SELECT 1 AS id, 100 AS amount, 'new' status, '2025-04-15'::TIMESTAMP AS created_at
651+
UNION ALL
652+
SELECT 2 AS id, 200 AS amount, 'new' status, '2025-04-16'::TIMESTAMP AS created_at
653+
UNION ALL
654+
SELECT 3 AS id, 300 AS amount, 'processed' status, '2025-04-17'::TIMESTAMP AS created_at
655+
UNION ALL
656+
SELECT 4 AS id, 500 AS amount, 'processed' status, '2025-04-18'::TIMESTAMP AS created_at
657+
UNION ALL
658+
SELECT 5 AS id, 600 AS amount, 'shipped' status, '2025-04-19'::TIMESTAMP AS created_at
659+
660+
measures:
661+
- name: count
662+
type: count
663+
- name: total_amount
664+
sql: amount
665+
type: sum
666+
667+
dimensions:
668+
- name: status
669+
sql: status
670+
type: string
671+
672+
- name: simple_orders_sql_ext
673+
674+
sql: >
675+
SELECT * FROM ({simple_orders.sql()}) as parent
676+
WHERE status = 'new'
677+
678+
measures:
679+
- name: count
680+
type: count
681+
682+
- name: total_amount
683+
sql: amount
684+
type: sum
685+
686+
dimensions:
687+
- name: id
688+
sql: id
689+
type: number
690+
primary_key: true
691+
692+
- name: created_at
693+
sql: created_at
694+
type: time
695+
`
696+
);
697+
698+
await compiler.compile();
699+
700+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
701+
measures: ['simple_orders_sql_ext.count'],
702+
timeDimensions: [{
703+
dimension: 'simple_orders_sql_ext.created_at',
704+
granularity: 'day',
705+
dateRange: ['2025-04-01', '2025-05-01']
706+
}],
707+
timezone: 'UTC',
708+
preAggregationsSchema: ''
709+
});
710+
711+
const res = await dbRunner.evaluateQueryWithPreAggregations(query);
712+
713+
expect(res).toEqual(
714+
[
715+
{
716+
simple_orders_sql_ext__count: '1',
717+
simple_orders_sql_ext__created_at_day: '2025-04-15T00:00:00.000Z',
718+
},
719+
{
720+
simple_orders_sql_ext__count: '1',
721+
simple_orders_sql_ext__created_at_day: '2025-04-16T00:00:00.000Z',
722+
}
723+
]
724+
);
725+
});
644726
});

packages/cubejs-schema-compiler/test/unit/yaml-schema.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,52 @@ describe('Yaml Schema Testing', () => {
474474
await compiler.compile();
475475
});
476476
});
477+
478+
it('calling cube\'s sql()', async () => {
479+
const { compiler } = prepareYamlCompiler(
480+
`cubes:
481+
- name: simple_orders
482+
sql: >
483+
SELECT 1 AS id, 100 AS amount, 'new' status, now() AS created_at
484+
485+
measures:
486+
- name: count
487+
type: count
488+
- name: total_amount
489+
sql: amount
490+
type: sum
491+
492+
dimensions:
493+
- name: status
494+
sql: status
495+
type: string
496+
497+
- name: simple_orders_sql_ext
498+
499+
sql: >
500+
SELECT * FROM {simple_orders.sql()}
501+
WHERE status = 'processed'
502+
503+
measures:
504+
- name: count
505+
type: count
506+
507+
- name: total_amount
508+
sql: amount
509+
type: sum
510+
511+
dimensions:
512+
- name: id
513+
sql: id
514+
type: number
515+
primary_key: true
516+
517+
- name: created_at
518+
sql: created_at
519+
type: time
520+
`
521+
);
522+
523+
await compiler.compile();
524+
});
477525
});

0 commit comments

Comments
 (0)