|
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', () => { |
@@ -676,7 +676,7 @@ views: |
676 | 676 | ); |
677 | 677 | }); |
678 | 678 |
|
679 | | - it('calling cube\'s sql()', async () => { |
| 679 | + it('calling cube\'s sql() (yaml-yaml)', async () => { |
680 | 680 | const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler( |
681 | 681 | `cubes: |
682 | 682 | - name: simple_orders |
@@ -706,7 +706,7 @@ views: |
706 | 706 | - name: simple_orders_sql_ext |
707 | 707 |
|
708 | 708 | sql: > |
709 | | - SELECT * FROM ({simple_orders.sql()}) as parent |
| 709 | + SELECT * FROM {simple_orders.sql()} as parent |
710 | 710 | WHERE status = 'new' |
711 | 711 |
|
712 | 712 | measures: |
@@ -757,4 +757,100 @@ views: |
757 | 757 | ] |
758 | 758 | ); |
759 | 759 | }); |
| 760 | + |
| 761 | + it('calling cube\'s sql() (yaml-js)', async () => { |
| 762 | + const { compiler, joinGraph, cubeEvaluator } = prepareCompiler([ |
| 763 | + { |
| 764 | + content: ` |
| 765 | +cube('simple_orders', { |
| 766 | + sql: \` |
| 767 | + SELECT 1 AS id, 100 AS amount, 'new' status, '2025-04-15'::TIMESTAMP AS created_at |
| 768 | + UNION ALL |
| 769 | + SELECT 2 AS id, 200 AS amount, 'new' status, '2025-04-16'::TIMESTAMP AS created_at |
| 770 | + UNION ALL |
| 771 | + SELECT 3 AS id, 300 AS amount, 'processed' status, '2025-04-17'::TIMESTAMP AS created_at |
| 772 | + UNION ALL |
| 773 | + SELECT 4 AS id, 500 AS amount, 'processed' status, '2025-04-18'::TIMESTAMP AS created_at |
| 774 | + UNION ALL |
| 775 | + SELECT 5 AS id, 600 AS amount, 'shipped' status, '2025-04-19'::TIMESTAMP AS created_at |
| 776 | + \`, |
| 777 | +
|
| 778 | + dimensions: { |
| 779 | + status: { |
| 780 | + sql: 'status', |
| 781 | + type: 'string', |
| 782 | + }, |
| 783 | + }, |
| 784 | +
|
| 785 | + measures: { |
| 786 | + count: { |
| 787 | + type: 'count', |
| 788 | + }, |
| 789 | + total_amount: { |
| 790 | + type: 'sum', |
| 791 | + sql: 'total_amount', |
| 792 | + }, |
| 793 | + }, |
| 794 | +}); |
| 795 | + `, |
| 796 | + fileName: 'cube.js', |
| 797 | + }, |
| 798 | + { |
| 799 | + content: `cubes: |
| 800 | + - name: simple_orders_sql_ext |
| 801 | +
|
| 802 | + sql: > |
| 803 | + SELECT * FROM {simple_orders.sql()} as parent |
| 804 | + WHERE status = 'new' |
| 805 | +
|
| 806 | + measures: |
| 807 | + - name: count |
| 808 | + type: count |
| 809 | +
|
| 810 | + - name: total_amount |
| 811 | + sql: amount |
| 812 | + type: sum |
| 813 | +
|
| 814 | + dimensions: |
| 815 | + - name: id |
| 816 | + sql: id |
| 817 | + type: number |
| 818 | + primary_key: true |
| 819 | +
|
| 820 | + - name: created_at |
| 821 | + sql: created_at |
| 822 | + type: time |
| 823 | + `, |
| 824 | + fileName: 'cube.yml', |
| 825 | + }, |
| 826 | + ]); |
| 827 | + |
| 828 | + await compiler.compile(); |
| 829 | + |
| 830 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { |
| 831 | + measures: ['simple_orders_sql_ext.count'], |
| 832 | + timeDimensions: [{ |
| 833 | + dimension: 'simple_orders_sql_ext.created_at', |
| 834 | + granularity: 'day', |
| 835 | + dateRange: ['2025-04-01', '2025-05-01'] |
| 836 | + }], |
| 837 | + timezone: 'UTC', |
| 838 | + preAggregationsSchema: '' |
| 839 | + }); |
| 840 | + |
| 841 | + const res = await dbRunner.evaluateQueryWithPreAggregations(query); |
| 842 | + |
| 843 | + expect(res).toEqual( |
| 844 | + [ |
| 845 | + { |
| 846 | + simple_orders_sql_ext__count: '1', |
| 847 | + simple_orders_sql_ext__created_at_day: '2025-04-15T00:00:00.000Z', |
| 848 | + }, |
| 849 | + { |
| 850 | + simple_orders_sql_ext__count: '1', |
| 851 | + simple_orders_sql_ext__created_at_day: '2025-04-16T00:00:00.000Z', |
| 852 | + } |
| 853 | + ] |
| 854 | + ); |
| 855 | + }); |
760 | 856 | }); |
0 commit comments