|
1 | 1 | import { PostgresQuery } from '../../../src/adapter/PostgresQuery'; |
2 | | -import { prepareYamlCompiler } from '../../unit/PrepareCompiler'; |
| 2 | +import { prepareCompiler, prepareYamlCompiler } from '../../unit/PrepareCompiler'; |
3 | 3 | import { dbRunner } from './PostgresDBRunner'; |
4 | 4 |
|
5 | 5 | describe('YAMLCompiler', () => { |
@@ -642,7 +642,7 @@ views: |
642 | 642 | ); |
643 | 643 | }); |
644 | 644 |
|
645 | | - it('calling cube\'s sql()', async () => { |
| 645 | + it('calling cube\'s sql() (yaml-yaml)', async () => { |
646 | 646 | const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler( |
647 | 647 | `cubes: |
648 | 648 | - name: simple_orders |
@@ -672,7 +672,7 @@ views: |
672 | 672 | - name: simple_orders_sql_ext |
673 | 673 |
|
674 | 674 | sql: > |
675 | | - SELECT * FROM ({simple_orders.sql()}) as parent |
| 675 | + SELECT * FROM {simple_orders.sql()} as parent |
676 | 676 | WHERE status = 'new' |
677 | 677 |
|
678 | 678 | measures: |
@@ -723,4 +723,100 @@ views: |
723 | 723 | ] |
724 | 724 | ); |
725 | 725 | }); |
| 726 | + |
| 727 | + it('calling cube\'s sql() (yaml-js)', async () => { |
| 728 | + const { compiler, joinGraph, cubeEvaluator } = prepareCompiler([ |
| 729 | + { |
| 730 | + content: ` |
| 731 | +cube('simple_orders', { |
| 732 | + sql: \` |
| 733 | + SELECT 1 AS id, 100 AS amount, 'new' status, '2025-04-15'::TIMESTAMP AS created_at |
| 734 | + UNION ALL |
| 735 | + SELECT 2 AS id, 200 AS amount, 'new' status, '2025-04-16'::TIMESTAMP AS created_at |
| 736 | + UNION ALL |
| 737 | + SELECT 3 AS id, 300 AS amount, 'processed' status, '2025-04-17'::TIMESTAMP AS created_at |
| 738 | + UNION ALL |
| 739 | + SELECT 4 AS id, 500 AS amount, 'processed' status, '2025-04-18'::TIMESTAMP AS created_at |
| 740 | + UNION ALL |
| 741 | + SELECT 5 AS id, 600 AS amount, 'shipped' status, '2025-04-19'::TIMESTAMP AS created_at |
| 742 | + \`, |
| 743 | +
|
| 744 | + dimensions: { |
| 745 | + status: { |
| 746 | + sql: 'status', |
| 747 | + type: 'string', |
| 748 | + }, |
| 749 | + }, |
| 750 | +
|
| 751 | + measures: { |
| 752 | + count: { |
| 753 | + type: 'count', |
| 754 | + }, |
| 755 | + total_amount: { |
| 756 | + type: 'sum', |
| 757 | + sql: 'total_amount', |
| 758 | + }, |
| 759 | + }, |
| 760 | +}); |
| 761 | + `, |
| 762 | + fileName: 'cube.js', |
| 763 | + }, |
| 764 | + { |
| 765 | + content: `cubes: |
| 766 | + - name: simple_orders_sql_ext |
| 767 | +
|
| 768 | + sql: > |
| 769 | + SELECT * FROM {simple_orders.sql()} as parent |
| 770 | + WHERE status = 'new' |
| 771 | +
|
| 772 | + measures: |
| 773 | + - name: count |
| 774 | + type: count |
| 775 | +
|
| 776 | + - name: total_amount |
| 777 | + sql: amount |
| 778 | + type: sum |
| 779 | +
|
| 780 | + dimensions: |
| 781 | + - name: id |
| 782 | + sql: id |
| 783 | + type: number |
| 784 | + primary_key: true |
| 785 | +
|
| 786 | + - name: created_at |
| 787 | + sql: created_at |
| 788 | + type: time |
| 789 | + `, |
| 790 | + fileName: 'cube.yml', |
| 791 | + }, |
| 792 | + ]); |
| 793 | + |
| 794 | + await compiler.compile(); |
| 795 | + |
| 796 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { |
| 797 | + measures: ['simple_orders_sql_ext.count'], |
| 798 | + timeDimensions: [{ |
| 799 | + dimension: 'simple_orders_sql_ext.created_at', |
| 800 | + granularity: 'day', |
| 801 | + dateRange: ['2025-04-01', '2025-05-01'] |
| 802 | + }], |
| 803 | + timezone: 'UTC', |
| 804 | + preAggregationsSchema: '' |
| 805 | + }); |
| 806 | + |
| 807 | + const res = await dbRunner.evaluateQueryWithPreAggregations(query); |
| 808 | + |
| 809 | + expect(res).toEqual( |
| 810 | + [ |
| 811 | + { |
| 812 | + simple_orders_sql_ext__count: '1', |
| 813 | + simple_orders_sql_ext__created_at_day: '2025-04-15T00:00:00.000Z', |
| 814 | + }, |
| 815 | + { |
| 816 | + simple_orders_sql_ext__count: '1', |
| 817 | + simple_orders_sql_ext__created_at_day: '2025-04-16T00:00:00.000Z', |
| 818 | + } |
| 819 | + ] |
| 820 | + ); |
| 821 | + }); |
726 | 822 | }); |
0 commit comments