2020-09-01 02:00:51 -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.
|
|
|
|
*/
|
2022-09-20 21:41:21 -04:00
|
|
|
import { SqlaFormData } from '@superset-ui/core';
|
2020-09-01 02:00:51 -04:00
|
|
|
import buildQuery from '../../src/Timeseries/buildQuery';
|
|
|
|
|
2020-09-10 17:56:38 -04:00
|
|
|
describe('Timeseries buildQuery', () => {
|
2020-09-01 02:00:51 -04:00
|
|
|
const formData = {
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
2020-09-10 17:56:38 -04:00
|
|
|
metrics: ['bar', 'baz'],
|
2020-09-01 02:00:51 -04:00
|
|
|
viz_type: 'my_chart',
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should build groupby with series in form data', () => {
|
|
|
|
const queryContext = buildQuery(formData);
|
|
|
|
const [query] = queryContext.queries;
|
2020-12-21 00:03:47 -05:00
|
|
|
expect(query.metrics).toEqual(['bar', 'baz']);
|
2020-09-01 02:00:51 -04:00
|
|
|
});
|
2021-06-08 05:34:36 -04:00
|
|
|
|
|
|
|
it('should order by timeseries limit if orderby unspecified', () => {
|
|
|
|
const queryContext = buildQuery({
|
|
|
|
...formData,
|
|
|
|
timeseries_limit_metric: 'bar',
|
|
|
|
order_desc: true,
|
|
|
|
});
|
|
|
|
const [query] = queryContext.queries;
|
|
|
|
expect(query.metrics).toEqual(['bar', 'baz']);
|
|
|
|
expect(query.timeseries_limit_metric).toEqual('bar');
|
|
|
|
expect(query.order_desc).toEqual(true);
|
|
|
|
expect(query.orderby).toEqual([['bar', false]]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not order by timeseries limit if orderby provided', () => {
|
|
|
|
const queryContext = buildQuery({
|
|
|
|
...formData,
|
|
|
|
timeseries_limit_metric: 'bar',
|
|
|
|
order_desc: true,
|
|
|
|
orderby: [['foo', true]],
|
|
|
|
});
|
|
|
|
const [query] = queryContext.queries;
|
|
|
|
expect(query.metrics).toEqual(['bar', 'baz']);
|
|
|
|
expect(query.timeseries_limit_metric).toEqual('bar');
|
|
|
|
expect(query.order_desc).toEqual(true);
|
|
|
|
expect(query.orderby).toEqual([['foo', true]]);
|
|
|
|
});
|
2020-09-01 02:00:51 -04:00
|
|
|
});
|
2022-09-20 21:41:21 -04:00
|
|
|
|
|
|
|
describe('GENERIC_CHART_AXES is enabled', () => {
|
|
|
|
let windowSpy: any;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
windowSpy = jest.spyOn(window, 'window', 'get').mockImplementation(() => ({
|
|
|
|
featureFlags: {
|
|
|
|
GENERIC_CHART_AXES: true,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
windowSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
const formData: SqlaFormData = {
|
|
|
|
datasource: '5__table',
|
|
|
|
viz_type: 'table',
|
|
|
|
granularity_sqla: 'time_column',
|
|
|
|
time_grain_sqla: 'P1Y',
|
|
|
|
time_range: '1 year ago : 2013',
|
|
|
|
groupby: ['col1'],
|
|
|
|
metrics: ['count(*)'],
|
|
|
|
};
|
|
|
|
|
|
|
|
it("shouldn't convert queryObject", () => {
|
|
|
|
const { queries } = buildQuery(formData);
|
|
|
|
expect(queries[0]).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
granularity: 'time_column',
|
|
|
|
time_range: '1 year ago : 2013',
|
|
|
|
extras: { time_grain_sqla: 'P1Y', having: '', where: '' },
|
|
|
|
columns: ['col1'],
|
|
|
|
series_columns: ['col1'],
|
|
|
|
metrics: ['count(*)'],
|
|
|
|
is_timeseries: true,
|
|
|
|
post_processing: [
|
|
|
|
{
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
aggregates: { 'count(*)': { operator: 'mean' } },
|
|
|
|
columns: ['col1'],
|
|
|
|
drop_missing_columns: true,
|
|
|
|
index: ['__timestamp'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ operation: 'flatten' },
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should convert queryObject', () => {
|
|
|
|
const { queries } = buildQuery({ ...formData, x_axis: 'time_column' });
|
|
|
|
expect(queries[0]).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
granularity: 'time_column',
|
|
|
|
time_range: '1 year ago : 2013',
|
|
|
|
extras: { having: '', where: '' },
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
columnType: 'BASE_AXIS',
|
|
|
|
expressionType: 'SQL',
|
|
|
|
label: 'time_column',
|
|
|
|
sqlExpression: 'time_column',
|
|
|
|
timeGrain: 'P1Y',
|
|
|
|
},
|
|
|
|
'col1',
|
|
|
|
],
|
|
|
|
series_columns: ['col1'],
|
|
|
|
metrics: ['count(*)'],
|
|
|
|
post_processing: [
|
|
|
|
{
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
aggregates: { 'count(*)': { operator: 'mean' } },
|
|
|
|
columns: ['col1'],
|
|
|
|
drop_missing_columns: true,
|
|
|
|
index: ['time_column'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ operation: 'flatten' },
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GENERIC_CHART_AXES is disabled', () => {
|
|
|
|
let windowSpy: any;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
windowSpy = jest.spyOn(window, 'window', 'get').mockImplementation(() => ({
|
|
|
|
featureFlags: {
|
|
|
|
GENERIC_CHART_AXES: false,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
windowSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
const formData: SqlaFormData = {
|
|
|
|
datasource: '5__table',
|
|
|
|
viz_type: 'table',
|
|
|
|
granularity_sqla: 'time_column',
|
|
|
|
time_grain_sqla: 'P1Y',
|
|
|
|
time_range: '1 year ago : 2013',
|
|
|
|
groupby: ['col1'],
|
|
|
|
metrics: ['count(*)'],
|
|
|
|
};
|
|
|
|
|
|
|
|
it("shouldn't convert queryObject", () => {
|
|
|
|
const { queries } = buildQuery(formData);
|
|
|
|
expect(queries[0]).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
granularity: 'time_column',
|
|
|
|
time_range: '1 year ago : 2013',
|
|
|
|
extras: { time_grain_sqla: 'P1Y', having: '', where: '' },
|
|
|
|
columns: ['col1'],
|
|
|
|
series_columns: ['col1'],
|
|
|
|
metrics: ['count(*)'],
|
|
|
|
is_timeseries: true,
|
|
|
|
post_processing: [
|
|
|
|
{
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
aggregates: { 'count(*)': { operator: 'mean' } },
|
|
|
|
columns: ['col1'],
|
|
|
|
drop_missing_columns: true,
|
|
|
|
index: ['__timestamp'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ operation: 'flatten' },
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should convert queryObject', () => {
|
|
|
|
const { queries } = buildQuery({ ...formData, x_axis: 'time_column' });
|
|
|
|
expect(queries[0]).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
granularity: 'time_column',
|
|
|
|
time_range: '1 year ago : 2013',
|
|
|
|
extras: { having: '', where: '' },
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
columnType: 'BASE_AXIS',
|
|
|
|
expressionType: 'SQL',
|
|
|
|
label: 'time_column',
|
|
|
|
sqlExpression: 'time_column',
|
|
|
|
timeGrain: 'P1Y',
|
|
|
|
},
|
|
|
|
'col1',
|
|
|
|
],
|
|
|
|
series_columns: ['col1'],
|
|
|
|
metrics: ['count(*)'],
|
|
|
|
post_processing: [
|
|
|
|
{
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
aggregates: { 'count(*)': { operator: 'mean' } },
|
|
|
|
columns: ['col1'],
|
|
|
|
drop_missing_columns: true,
|
|
|
|
index: ['time_column'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ operation: 'flatten' },
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|