|
| 1 | +import { |
| 2 | + getEnv, |
| 3 | +} from '@cubejs-backend/shared'; |
| 4 | +import { PostgresQuery } from '../../../src/adapter/PostgresQuery'; |
| 5 | +import { prepareYamlCompiler } from '../../unit/PrepareCompiler'; |
| 6 | +import { dbRunner } from './PostgresDBRunner'; |
| 7 | + |
| 8 | +describe('Member Expression', () => { |
| 9 | + jest.setTimeout(200000); |
| 10 | + |
| 11 | + const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(` |
| 12 | +cubes: |
| 13 | + - name: customers |
| 14 | + sql: > |
| 15 | + SELECT 9 as ID, 'state1' as STATE, 'New York' as CITY |
| 16 | +
|
| 17 | + dimensions: |
| 18 | + - name: id |
| 19 | + sql: ID |
| 20 | + type: number |
| 21 | + primary_key: true |
| 22 | +
|
| 23 | + - name: state |
| 24 | + sql: STATE |
| 25 | + type: string |
| 26 | +
|
| 27 | + - name: city |
| 28 | + sql: CITY |
| 29 | + type: string |
| 30 | +
|
| 31 | +
|
| 32 | + measures: |
| 33 | + - name: count |
| 34 | + type: count |
| 35 | +
|
| 36 | +views: |
| 37 | + - name: customers_view |
| 38 | +
|
| 39 | + cubes: |
| 40 | + - join_path: customers |
| 41 | + includes: |
| 42 | + - count |
| 43 | +
|
| 44 | + - city |
| 45 | +
|
| 46 | + `); |
| 47 | + |
| 48 | + async function runQueryTest(q, expectedResult) { |
| 49 | + /* if (!getEnv('nativeSqlPlanner')) { |
| 50 | + return; |
| 51 | + } */ |
| 52 | + await compiler.compile(); |
| 53 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, q); |
| 54 | + |
| 55 | + console.log(query.buildSqlAndParams()); |
| 56 | + |
| 57 | + const res = await dbRunner.testQuery(query.buildSqlAndParams()); |
| 58 | + console.log(JSON.stringify(res)); |
| 59 | + |
| 60 | + expect(res).toEqual( |
| 61 | + expectedResult |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + it('member expression over views', async () => runQueryTest({ |
| 66 | + measures: [ |
| 67 | + { |
| 68 | + // eslint-disable-next-line no-new-func |
| 69 | + expression: new Function( |
| 70 | + 'customers_view', |
| 71 | + // eslint-disable-next-line no-template-curly-in-string |
| 72 | + 'return `${customers_view.count}`' |
| 73 | + ), |
| 74 | + // eslint-disable-next-line no-template-curly-in-string |
| 75 | + definition: '${customers_view.count}', |
| 76 | + expressionName: 'count', |
| 77 | + cubeName: 'customers_view', |
| 78 | + }, |
| 79 | + { |
| 80 | + // eslint-disable-next-line no-new-func |
| 81 | + expression: new Function( |
| 82 | + 'customers_view', |
| 83 | + // eslint-disable-next-line no-template-curly-in-string |
| 84 | + 'return `${customers_view.city}`' |
| 85 | + ), |
| 86 | + // eslint-disable-next-line no-template-curly-in-string |
| 87 | + definition: '${customers_view.city}', |
| 88 | + expressionName: 'city', |
| 89 | + cubeName: 'customers_view', |
| 90 | + }, |
| 91 | + { |
| 92 | + // eslint-disable-next-line no-new-func |
| 93 | + expression: new Function( |
| 94 | + // eslint-disable-next-line no-template-curly-in-string |
| 95 | + 'return `\'NULL\'`' |
| 96 | + ), |
| 97 | + // eslint-disable-next-line no-template-curly-in-string |
| 98 | + definition: 'CAST(NULL AS STRING)', |
| 99 | + expressionName: 'cubejoinfield', |
| 100 | + cubeName: 'customers_view', |
| 101 | + }, |
| 102 | + ], |
| 103 | + allowUngroupedWithoutPrimaryKey: true, |
| 104 | + ungrouped: true, |
| 105 | + }, [ |
| 106 | + |
| 107 | + { orders__date_year: '2023-01-01T00:00:00.000Z', |
| 108 | + orders__revenue: '15', |
| 109 | + orders__revenue_1_y_ago: '5', |
| 110 | + orders__cagr_1_y: '2.0000000000000000' }, |
| 111 | + { orders__date_year: '2024-01-01T00:00:00.000Z', orders__revenue: '30', orders__revenue_1_y_ago: '15', orders__cagr_1_y: '1.0000000000000000' }, |
| 112 | + { orders__date_year: '2025-01-01T00:00:00.000Z', orders__revenue: '5', orders__revenue_1_y_ago: '30', orders__cagr_1_y: '-0.83333333333333333333' }])); |
| 113 | +}); |
0 commit comments