2021-07-28 08:01:43 -04:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
import { QueryObject, SqlaFormData } from '@superset-ui/core';
|
|
|
|
import { rollingWindowOperator } from '../../../src';
|
|
|
|
|
|
|
|
const formData: SqlaFormData = {
|
2021-11-09 07:42:28 -05:00
|
|
|
metrics: [
|
|
|
|
'count(*)',
|
|
|
|
{ label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
|
|
|
|
],
|
2021-07-28 08:01:43 -04:00
|
|
|
time_range: '2015 : 2016',
|
|
|
|
granularity: 'month',
|
|
|
|
datasource: 'foo',
|
|
|
|
viz_type: 'table',
|
|
|
|
};
|
|
|
|
const queryObject: QueryObject = {
|
2021-11-09 07:42:28 -05:00
|
|
|
metrics: [
|
|
|
|
'count(*)',
|
|
|
|
{ label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
|
|
|
|
],
|
2021-07-28 08:01:43 -04:00
|
|
|
time_range: '2015 : 2016',
|
|
|
|
granularity: 'month',
|
|
|
|
post_processing: [
|
|
|
|
{
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['__timestamp'],
|
|
|
|
columns: ['nation'],
|
|
|
|
aggregates: {
|
|
|
|
'count(*)': {
|
|
|
|
operator: 'sum',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-09-20 09:55:08 -04:00
|
|
|
test('skip transformation', () => {
|
|
|
|
expect(rollingWindowOperator(formData, queryObject)).toEqual(undefined);
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
rollingWindowOperator({ ...formData, rolling_type: 'None' }, queryObject),
|
|
|
|
).toEqual(undefined);
|
|
|
|
expect(
|
|
|
|
rollingWindowOperator({ ...formData, rolling_type: 'foobar' }, queryObject),
|
|
|
|
).toEqual(undefined);
|
2021-07-28 08:01:43 -04:00
|
|
|
|
2021-09-20 09:55:08 -04:00
|
|
|
const formDataWithoutMetrics = { ...formData };
|
|
|
|
delete formDataWithoutMetrics.metrics;
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(rollingWindowOperator(formDataWithoutMetrics, queryObject)).toEqual(
|
|
|
|
undefined,
|
|
|
|
);
|
2021-09-20 09:55:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('rolling_type: cumsum', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
rollingWindowOperator({ ...formData, rolling_type: 'cumsum' }, queryObject),
|
|
|
|
).toEqual({
|
2021-09-20 09:55:08 -04:00
|
|
|
operation: 'cum',
|
|
|
|
options: {
|
|
|
|
operator: 'sum',
|
|
|
|
columns: {
|
|
|
|
'count(*)': 'count(*)',
|
|
|
|
'sum(val)': 'sum(val)',
|
|
|
|
},
|
2021-10-06 03:08:58 -04:00
|
|
|
is_pivot_df: true,
|
2021-09-20 09:55:08 -04:00
|
|
|
},
|
2021-07-28 08:01:43 -04:00
|
|
|
});
|
2021-09-20 09:55:08 -04:00
|
|
|
});
|
2021-07-28 08:01:43 -04:00
|
|
|
|
2021-09-20 09:55:08 -04:00
|
|
|
test('rolling_type: sum/mean/std', () => {
|
|
|
|
const rollingTypes = ['sum', 'mean', 'std'];
|
|
|
|
rollingTypes.forEach(rollingType => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
rollingWindowOperator(
|
|
|
|
{ ...formData, rolling_type: rollingType },
|
|
|
|
queryObject,
|
|
|
|
),
|
|
|
|
).toEqual({
|
2021-09-20 09:55:08 -04:00
|
|
|
operation: 'rolling',
|
2021-07-28 08:01:43 -04:00
|
|
|
options: {
|
2021-09-20 09:55:08 -04:00
|
|
|
rolling_type: rollingType,
|
|
|
|
window: 1,
|
|
|
|
min_periods: 0,
|
2021-07-28 08:01:43 -04:00
|
|
|
columns: {
|
|
|
|
'count(*)': 'count(*)',
|
|
|
|
'sum(val)': 'sum(val)',
|
|
|
|
},
|
2021-10-06 03:08:58 -04:00
|
|
|
is_pivot_df: true,
|
2021-07-28 08:01:43 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2021-09-20 09:55:08 -04:00
|
|
|
});
|
2021-07-28 08:01:43 -04:00
|
|
|
|
2021-09-20 09:55:08 -04:00
|
|
|
test('rolling window and "actual values" in the time compare', () => {
|
|
|
|
expect(
|
|
|
|
rollingWindowOperator(
|
|
|
|
{
|
|
|
|
...formData,
|
|
|
|
rolling_type: 'cumsum',
|
|
|
|
comparison_type: 'values',
|
|
|
|
time_compare: ['1 year ago', '1 year later'],
|
|
|
|
},
|
|
|
|
queryObject,
|
|
|
|
),
|
|
|
|
).toEqual({
|
|
|
|
operation: 'cum',
|
|
|
|
options: {
|
|
|
|
operator: 'sum',
|
|
|
|
columns: {
|
|
|
|
'count(*)': 'count(*)',
|
|
|
|
'count(*)__1 year ago': 'count(*)__1 year ago',
|
|
|
|
'count(*)__1 year later': 'count(*)__1 year later',
|
|
|
|
'sum(val)': 'sum(val)',
|
|
|
|
'sum(val)__1 year ago': 'sum(val)__1 year ago',
|
|
|
|
'sum(val)__1 year later': 'sum(val)__1 year later',
|
|
|
|
},
|
2021-10-06 03:08:58 -04:00
|
|
|
is_pivot_df: true,
|
2021-09-20 09:55:08 -04:00
|
|
|
},
|
2021-07-28 08:01:43 -04:00
|
|
|
});
|
2021-09-20 09:55:08 -04:00
|
|
|
});
|
2021-07-28 08:01:43 -04:00
|
|
|
|
2021-10-04 01:30:50 -04:00
|
|
|
test('rolling window and "difference / percentage / ratio" in the time compare', () => {
|
|
|
|
const comparisionTypes = ['difference', 'percentage', 'ratio'];
|
2021-09-20 09:55:08 -04:00
|
|
|
comparisionTypes.forEach(cType => {
|
2021-07-28 08:01:43 -04:00
|
|
|
expect(
|
|
|
|
rollingWindowOperator(
|
|
|
|
{
|
|
|
|
...formData,
|
|
|
|
rolling_type: 'cumsum',
|
2021-09-20 09:55:08 -04:00
|
|
|
comparison_type: cType,
|
2021-07-28 08:01:43 -04:00
|
|
|
time_compare: ['1 year ago', '1 year later'],
|
|
|
|
},
|
|
|
|
queryObject,
|
|
|
|
),
|
|
|
|
).toEqual({
|
|
|
|
operation: 'cum',
|
|
|
|
options: {
|
|
|
|
operator: 'sum',
|
|
|
|
columns: {
|
2021-09-20 09:55:08 -04:00
|
|
|
[`${cType}__count(*)__count(*)__1 year ago`]: `${cType}__count(*)__count(*)__1 year ago`,
|
|
|
|
[`${cType}__count(*)__count(*)__1 year later`]: `${cType}__count(*)__count(*)__1 year later`,
|
|
|
|
[`${cType}__sum(val)__sum(val)__1 year ago`]: `${cType}__sum(val)__sum(val)__1 year ago`,
|
|
|
|
[`${cType}__sum(val)__sum(val)__1 year later`]: `${cType}__sum(val)__sum(val)__1 year later`,
|
2021-07-28 08:01:43 -04:00
|
|
|
},
|
2021-10-06 03:08:58 -04:00
|
|
|
is_pivot_df: true,
|
2021-07-28 08:01:43 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|