Skip to content

Commit

Permalink
add tests for custom intervals time series
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Sep 25, 2024
1 parent 4e1deea commit 4d1bf3e
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 4 deletions.
305 changes: 301 additions & 4 deletions packages/cubejs-client-core/src/tests/ResultSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,303 @@ describe('ResultSet', () => {
];
expect(resultSet.timeSeries(timeDimension)).toEqual(output);
});

test('it generates array of dates - custom interval - 1 year, origin - 2020-01-01', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2023-12-31'],
granularity: 'one_year',
dimension: 'Events.time'
};
const output = [
'2021-01-01T00:00:00.000',
'2022-01-01T00:00:00.000',
'2023-01-01T00:00:00.000'
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.one_year': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '1 year',
title: '1 year',
interval: '1 year',
origin: '2020-01-01',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 1 year, origin - 2025-03-01', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2022-12-31'],
granularity: 'one_year',
dimension: 'Events.time'
};
const output = [
'2020-03-01T00:00:00.000',
'2021-03-01T00:00:00.000',
'2022-03-01T00:00:00.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.one_year': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '1 year',
title: '1 year',
interval: '1 year',
origin: '2025-03-01',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 1 year, offset - 2 months', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2022-12-31'],
granularity: 'one_year',
dimension: 'Events.time'
};
const output = [
'2020-03-01T00:00:00.000',
'2021-03-01T00:00:00.000',
'2022-03-01T00:00:00.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.one_year': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '1 year',
title: '1 year',
interval: '1 year',
offset: '2 months',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 2 months, origin - 2019-01-01', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2021-12-31'],
granularity: 'two_months',
dimension: 'Events.time'
};
const output = [
'2021-01-01T00:00:00.000',
'2021-03-01T00:00:00.000',
'2021-05-01T00:00:00.000',
'2021-07-01T00:00:00.000',
'2021-09-01T00:00:00.000',
'2021-11-01T00:00:00.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.two_months': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '2 months',
title: '2 months',
interval: '2 months',
origin: '2019-01-01',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 2 months, no offset', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2021-12-31'],
granularity: 'two_months',
dimension: 'Events.time'
};
const output = [
'2021-01-01T00:00:00.000',
'2021-03-01T00:00:00.000',
'2021-05-01T00:00:00.000',
'2021-07-01T00:00:00.000',
'2021-09-01T00:00:00.000',
'2021-11-01T00:00:00.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.two_months': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '2 months',
title: '2 months',
interval: '2 months',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 2 months, origin - 2019-03-15', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2021-12-31'],
granularity: 'two_months',
dimension: 'Events.time'
};
const output = [
'2020-11-15T00:00:00.000',
'2021-01-15T00:00:00.000',
'2021-03-15T00:00:00.000',
'2021-05-15T00:00:00.000',
'2021-07-15T00:00:00.000',
'2021-09-15T00:00:00.000',
'2021-11-15T00:00:00.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.two_months': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '2 months',
title: '2 months',
interval: '2 months',
origin: '2019-03-15',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 1 months 2 weeks 3 days, origin - 2021-01-25', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2021-12-31'],
granularity: 'one_mo_two_we_three_d',
dimension: 'Events.time'
};
const output = [
'2020-12-08T00:00:00.000',
'2021-01-25T00:00:00.000',
'2021-03-14T00:00:00.000',
'2021-05-01T00:00:00.000',
'2021-06-18T00:00:00.000',
'2021-08-04T00:00:00.000',
'2021-09-21T00:00:00.000',
'2021-11-07T00:00:00.000',
'2021-12-24T00:00:00.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.one_mo_two_we_three_d': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '1 months 2 weeks 3 days',
title: '1 months 2 weeks 3 days',
interval: '1 months 2 weeks 3 days',
origin: '2021-01-25',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 3 weeks, origin - 2020-12-15', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2021-03-01'],
granularity: 'three_weeks',
dimension: 'Events.time'
};
const output = [
'2020-12-15T00:00:00.000',
'2021-01-05T00:00:00.000',
'2021-01-26T00:00:00.000',
'2021-02-16T00:00:00.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.three_weeks': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '3 weeks',
title: '3 weeks',
interval: '3 weeks',
origin: '2020-12-15',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds, origin - 2021-01-01', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-01-01', '2021-12-31'],
granularity: 'two_mo_3w_4d_5h_6m_7s',
dimension: 'Events.time'
};
const output = [
'2021-01-01T00:00:00.000',
'2021-03-26T05:06:07.000',
'2021-06-20T10:12:14.000',
'2021-09-14T15:18:21.000',
'2021-12-09T20:24:28.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.two_mo_3w_4d_5h_6m_7s': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: 'two_mo_3w_4d_5h_6m_7s',
title: 'two_mo_3w_4d_5h_6m_7s',
interval: '2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds',
origin: '2021-01-01',
},
},
})).toEqual(output);
});

test('it generates array of dates - custom interval - 10 minutes 15 seconds, origin - 2021-02-01 09:59:45', () => {
const resultSet = new ResultSet({});
const timeDimension = {
dateRange: ['2021-02-01 10:00:00', '2021-02-01 12:00:00'],
granularity: 'ten_min_fifteen_sec',
dimension: 'Events.time'
};
const output = [
'2021-02-01T09:59:45.000',
'2021-02-01T10:10:00.000',
'2021-02-01T10:20:15.000',
'2021-02-01T10:30:30.000',
'2021-02-01T10:40:45.000',
'2021-02-01T10:51:00.000',
'2021-02-01T11:01:15.000',
'2021-02-01T11:11:30.000',
'2021-02-01T11:21:45.000',
'2021-02-01T11:32:00.000',
'2021-02-01T11:42:15.000',
'2021-02-01T11:52:30.000',
];
expect(resultSet.timeSeries(timeDimension, 1, {
'Events.time.ten_min_fifteen_sec': {
title: 'Time Dimension',
shortTitle: 'TD',
type: 'time',
granularity: {
name: '10 minutes 15 seconds',
title: '10 minutes 15 seconds',
interval: '10 minutes 15 seconds',
origin: '2021-02-01 09:59:45',
},
},
})).toEqual(output);
});
});

describe('chartPivot', () => {
Expand Down Expand Up @@ -145,7 +442,7 @@ describe('ResultSet', () => {
expect(resultSet.chartPivot()).toEqual([
{
x: 'Name 1',

'Foo.count': 'Some string',
xValues: [
'Name 1'
Expand Down Expand Up @@ -194,7 +491,7 @@ describe('ResultSet', () => {
expect(resultSet.chartPivot()).toEqual([
{
x: 'Name 1',

'Foo.count': 0,
xValues: [
'Name 1'
Expand Down Expand Up @@ -291,7 +588,7 @@ describe('ResultSet', () => {
expect(resultSet.chartPivot()).toEqual([
{
x: 'Name 1',

'Foo.count': 10,
xValues: [
'Name 1'
Expand Down Expand Up @@ -343,7 +640,7 @@ describe('ResultSet', () => {
expect(resultSet.chartPivot()).toEqual([
{
x: 'Name 1',

'Foo.latestRun': new Date('2020-03-11T18:06:09.403Z'),
xValues: [
'Name 1'
Expand Down
2 changes: 2 additions & 0 deletions packages/cubejs-client-core/src/tests/granularity.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* globals describe,test,expect */

import 'jest';
import dayjs from 'dayjs';
import ko from 'dayjs/locale/ko';
Expand Down

0 comments on commit 4d1bf3e

Please sign in to comment.