|
| 1 | +import { PostgresQuery } from '../../../src/adapter/PostgresQuery'; |
| 2 | +import { prepareJsCompiler } from '../../unit/PrepareCompiler'; |
| 3 | +import { dbRunner } from './PostgresDBRunner'; |
| 4 | + |
| 5 | +describe('PostgresCumulativeMeasures', () => { |
| 6 | + jest.setTimeout(200000); |
| 7 | + |
| 8 | + const { compiler, joinGraph, cubeEvaluator } = prepareJsCompiler(` |
| 9 | + cube(\`visitors\`, { |
| 10 | + sql: \` |
| 11 | + select * from visitors |
| 12 | + \`, |
| 13 | +
|
| 14 | + joins: {}, |
| 15 | +
|
| 16 | + measures: { |
| 17 | + count: { |
| 18 | + type: 'count' |
| 19 | + }, |
| 20 | +
|
| 21 | + unboundedCount: { |
| 22 | + type: 'count', |
| 23 | + rollingWindow: { |
| 24 | + trailing: 'unbounded' |
| 25 | + } |
| 26 | + } |
| 27 | + }, |
| 28 | +
|
| 29 | + dimensions: { |
| 30 | + id: { |
| 31 | + type: 'number', |
| 32 | + sql: 'id', |
| 33 | + primaryKey: true |
| 34 | + }, |
| 35 | + source: { |
| 36 | + type: 'string', |
| 37 | + sql: 'source' |
| 38 | + }, |
| 39 | + createdAt: { |
| 40 | + type: 'time', |
| 41 | + sql: 'created_at' |
| 42 | + }, |
| 43 | + }, |
| 44 | +
|
| 45 | + preAggregations: {} |
| 46 | + }) |
| 47 | + `); |
| 48 | + |
| 49 | + it('should group by the created_at field on the calculated granularity for unbounded trailing windows without dimension', () => compiler.compile().then(async () => { |
| 50 | + const query = new PostgresQuery( |
| 51 | + { joinGraph, cubeEvaluator, compiler }, |
| 52 | + { |
| 53 | + measures: ['visitors.count', 'visitors.unboundedCount'], |
| 54 | + timeDimensions: [ |
| 55 | + { |
| 56 | + dimension: 'visitors.createdAt', |
| 57 | + granularity: 'day', |
| 58 | + dateRange: ['2017-01-01', '2017-01-30'], |
| 59 | + }, |
| 60 | + ], |
| 61 | + timezone: 'America/Los_Angeles', |
| 62 | + order: [ |
| 63 | + { |
| 64 | + id: 'visitors.createdAt', |
| 65 | + }, |
| 66 | + ], |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + const queryAndParams = query.buildSqlAndParams(); |
| 71 | + console.log(queryAndParams); |
| 72 | + |
| 73 | + expect(await dbRunner.testQuery(query.buildSqlAndParams())).toEqual([ |
| 74 | + { |
| 75 | + visitors__count: '1', |
| 76 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 77 | + visitors__unbounded_count: '2', |
| 78 | + }, |
| 79 | + { |
| 80 | + visitors__count: '1', |
| 81 | + visitors__created_at_day: '2017-01-04T00:00:00.000Z', |
| 82 | + visitors__unbounded_count: '3', |
| 83 | + }, |
| 84 | + { |
| 85 | + visitors__count: '1', |
| 86 | + visitors__created_at_day: '2017-01-05T00:00:00.000Z', |
| 87 | + visitors__unbounded_count: '4', |
| 88 | + }, |
| 89 | + { |
| 90 | + visitors__count: '2', |
| 91 | + visitors__created_at_day: '2017-01-06T00:00:00.000Z', |
| 92 | + visitors__unbounded_count: '6', |
| 93 | + } |
| 94 | + ]); |
| 95 | + })); |
| 96 | + |
| 97 | + it('should group by the created_at field on the calculated granularity for unbounded trailing windows with dimension', () => compiler.compile().then(async () => { |
| 98 | + const query = new PostgresQuery( |
| 99 | + { joinGraph, cubeEvaluator, compiler }, |
| 100 | + { |
| 101 | + measures: ['visitors.count', 'visitors.unboundedCount'], |
| 102 | + dimensions: ['visitors.source'], |
| 103 | + timeDimensions: [ |
| 104 | + { |
| 105 | + dimension: 'visitors.createdAt', |
| 106 | + granularity: 'day', |
| 107 | + dateRange: ['2017-01-01', '2017-01-30'], |
| 108 | + }, |
| 109 | + ], |
| 110 | + timezone: 'America/Los_Angeles', |
| 111 | + order: [ |
| 112 | + { |
| 113 | + id: 'visitors.createdAt', |
| 114 | + }, |
| 115 | + ], |
| 116 | + } |
| 117 | + ); |
| 118 | + |
| 119 | + const queryAndParams = query.buildSqlAndParams(); |
| 120 | + console.log(queryAndParams); |
| 121 | + |
| 122 | + expect(await dbRunner.testQuery(query.buildSqlAndParams())).toEqual([ |
| 123 | + { |
| 124 | + visitors__count: '1', |
| 125 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 126 | + visitors__source: 'some', |
| 127 | + visitors__unbounded_count: '1' |
| 128 | + }, |
| 129 | + { |
| 130 | + visitors__count: '1', |
| 131 | + visitors__created_at_day: '2017-01-04T00:00:00.000Z', |
| 132 | + visitors__source: 'some', |
| 133 | + visitors__unbounded_count: '2', |
| 134 | + }, |
| 135 | + { |
| 136 | + visitors__count: '1', |
| 137 | + visitors__created_at_day: '2017-01-05T00:00:00.000Z', |
| 138 | + visitors__source: 'google', |
| 139 | + visitors__unbounded_count: '1', |
| 140 | + }, |
| 141 | + { |
| 142 | + visitors__count: '2', |
| 143 | + visitors__created_at_day: '2017-01-06T00:00:00.000Z', |
| 144 | + visitors__source: null, |
| 145 | + visitors__unbounded_count: '3', |
| 146 | + } |
| 147 | + ]); |
| 148 | + })); |
| 149 | +}); |
0 commit comments