2020-12-02 01:20:00 -05: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.
|
|
|
|
*/
|
2020-12-21 00:03:47 -05:00
|
|
|
import {
|
2022-01-07 20:16:24 -05:00
|
|
|
JsonObject,
|
2020-12-21 00:03:47 -05:00
|
|
|
AnnotationLayer,
|
|
|
|
AnnotationOpacity,
|
|
|
|
AnnotationSourceType,
|
|
|
|
AnnotationStyle,
|
|
|
|
AnnotationType,
|
|
|
|
buildQueryObject,
|
|
|
|
QueryObject,
|
2022-01-07 20:16:24 -05:00
|
|
|
} from '@superset-ui/core';
|
2018-12-04 16:21:49 -05:00
|
|
|
|
2020-04-27 15:47:32 -04:00
|
|
|
describe('buildQueryObject', () => {
|
2018-12-04 16:21:49 -05:00
|
|
|
let query: QueryObject;
|
|
|
|
|
2020-05-20 14:13:11 -04:00
|
|
|
it('should build granularity for sqlalchemy datasources', () => {
|
2018-12-12 16:18:27 -05:00
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
});
|
2018-12-04 16:21:49 -05:00
|
|
|
expect(query.granularity).toEqual('ds');
|
|
|
|
});
|
|
|
|
|
2020-05-20 14:13:11 -04:00
|
|
|
it('should build granularity for druid datasources', () => {
|
2018-12-12 16:18:27 -05:00
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__druid',
|
|
|
|
granularity: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
});
|
2018-12-04 16:21:49 -05:00
|
|
|
expect(query.granularity).toEqual('ds');
|
|
|
|
});
|
|
|
|
|
2020-05-20 14:13:11 -04:00
|
|
|
it('should build metrics based on default queryFields', () => {
|
2018-12-11 14:58:02 -05:00
|
|
|
query = buildQueryObject({
|
2018-12-04 16:21:49 -05:00
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
2018-12-12 16:18:27 -05:00
|
|
|
viz_type: 'table',
|
2018-12-04 16:21:49 -05:00
|
|
|
metric: 'sum__num',
|
2020-05-20 14:13:11 -04:00
|
|
|
secondary_metric: 'avg__num',
|
|
|
|
});
|
2020-12-21 00:03:47 -05:00
|
|
|
expect(query.metrics).toEqual(['sum__num', 'avg__num']);
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
|
2021-03-08 14:28:43 -05:00
|
|
|
it('should merge original and append filters', () => {
|
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
extra_filters: [{ col: 'abc', op: '==', val: 'qwerty' }],
|
|
|
|
adhoc_filters: [
|
|
|
|
{
|
|
|
|
expressionType: 'SIMPLE',
|
|
|
|
clause: 'WHERE',
|
|
|
|
subject: 'foo',
|
|
|
|
operator: '!=',
|
|
|
|
comparator: 'bar',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
where: 'a = b',
|
|
|
|
extra_form_data: {
|
2021-04-14 07:30:43 -04:00
|
|
|
adhoc_filters: [
|
|
|
|
{
|
|
|
|
expressionType: 'SQL',
|
|
|
|
clause: 'WHERE',
|
|
|
|
sqlExpression: '(1 = 1)',
|
|
|
|
},
|
|
|
|
],
|
2021-03-08 14:28:43 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(query.filters).toEqual([
|
|
|
|
{ col: 'abc', op: '==', val: 'qwerty' },
|
|
|
|
{ col: 'foo', op: '!=', val: 'bar' },
|
|
|
|
]);
|
|
|
|
expect(query.extras?.where).toEqual('(a = b) AND ((1 = 1))');
|
|
|
|
});
|
|
|
|
|
2020-05-20 14:13:11 -04:00
|
|
|
it('should group custom metric control', () => {
|
2020-12-21 00:03:47 -05:00
|
|
|
query = buildQueryObject(
|
|
|
|
{
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
my_custom_metric_control: 'sum__num',
|
|
|
|
},
|
|
|
|
{ my_custom_metric_control: 'metrics' },
|
|
|
|
);
|
|
|
|
expect(query.metrics).toEqual(['sum__num']);
|
2018-12-04 16:21:49 -05:00
|
|
|
});
|
2019-03-12 19:55:50 -04:00
|
|
|
|
2020-05-20 14:13:11 -04:00
|
|
|
it('should group custom metric control with predefined metrics', () => {
|
2020-12-21 00:03:47 -05:00
|
|
|
query = buildQueryObject(
|
|
|
|
{
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
metrics: ['sum__num'],
|
|
|
|
my_custom_metric_control: 'avg__num',
|
|
|
|
},
|
|
|
|
{ my_custom_metric_control: 'metrics' },
|
|
|
|
);
|
|
|
|
expect(query.metrics).toEqual(['sum__num', 'avg__num']);
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
|
2019-03-12 19:55:50 -04:00
|
|
|
it('should build limit', () => {
|
|
|
|
const limit = 2;
|
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
limit,
|
|
|
|
});
|
|
|
|
expect(query.timeseries_limit).toEqual(limit);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should build order_desc', () => {
|
|
|
|
const orderDesc = false;
|
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
order_desc: orderDesc,
|
|
|
|
});
|
|
|
|
expect(query.order_desc).toEqual(orderDesc);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should build timeseries_limit_metric', () => {
|
|
|
|
const metric = 'country';
|
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
timeseries_limit_metric: metric,
|
|
|
|
});
|
2020-12-21 00:03:47 -05:00
|
|
|
expect(query.timeseries_limit_metric).toEqual(metric);
|
2019-03-12 19:55:50 -04:00
|
|
|
});
|
2020-04-27 15:47:32 -04:00
|
|
|
|
2022-02-28 11:56:13 -05:00
|
|
|
it('should build series_limit_metric', () => {
|
|
|
|
const metric = 'country';
|
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'pivot_table_v2',
|
|
|
|
series_limit_metric: metric,
|
|
|
|
});
|
|
|
|
expect(query.series_limit_metric).toEqual(metric);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should build series_limit_metric as undefined when empty array', () => {
|
|
|
|
const metric: any = [];
|
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'pivot_table_v2',
|
|
|
|
series_limit_metric: metric,
|
|
|
|
});
|
|
|
|
expect(query.series_limit_metric).toEqual(undefined);
|
|
|
|
});
|
|
|
|
|
2020-06-05 14:57:47 -04:00
|
|
|
it('should handle null and non-numeric row_limit and row_offset', () => {
|
2020-04-27 15:47:32 -04:00
|
|
|
const baseQuery = {
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
row_limit: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
// undefined
|
|
|
|
query = buildQueryObject({ ...baseQuery });
|
|
|
|
expect(query.row_limit).toBeUndefined();
|
2020-06-05 14:57:47 -04:00
|
|
|
expect(query.row_offset).toBeUndefined();
|
2020-04-27 15:47:32 -04:00
|
|
|
|
|
|
|
// null value
|
2021-11-09 07:42:28 -05:00
|
|
|
query = buildQueryObject({
|
|
|
|
...baseQuery,
|
|
|
|
row_limit: null,
|
|
|
|
row_offset: null,
|
|
|
|
});
|
2020-04-27 15:47:32 -04:00
|
|
|
expect(query.row_limit).toBeUndefined();
|
2020-06-05 14:57:47 -04:00
|
|
|
expect(query.row_offset).toBeUndefined();
|
2020-04-27 15:47:32 -04:00
|
|
|
|
2020-06-05 14:57:47 -04:00
|
|
|
query = buildQueryObject({ ...baseQuery, row_limit: 1000, row_offset: 50 });
|
2020-04-27 15:47:32 -04:00
|
|
|
expect(query.row_limit).toStrictEqual(1000);
|
2020-06-05 14:57:47 -04:00
|
|
|
expect(query.row_offset).toStrictEqual(50);
|
2020-04-27 15:47:32 -04:00
|
|
|
|
|
|
|
// valid string
|
2021-11-09 07:42:28 -05:00
|
|
|
query = buildQueryObject({
|
|
|
|
...baseQuery,
|
|
|
|
row_limit: '200',
|
|
|
|
row_offset: '100',
|
|
|
|
});
|
2020-04-27 15:47:32 -04:00
|
|
|
expect(query.row_limit).toStrictEqual(200);
|
2020-06-05 14:57:47 -04:00
|
|
|
expect(query.row_offset).toStrictEqual(100);
|
2020-04-27 15:47:32 -04:00
|
|
|
|
|
|
|
// invalid string
|
2021-11-09 07:42:28 -05:00
|
|
|
query = buildQueryObject({
|
|
|
|
...baseQuery,
|
|
|
|
row_limit: 'two hundred',
|
|
|
|
row_offset: 'twenty',
|
|
|
|
});
|
2020-04-27 15:47:32 -04:00
|
|
|
expect(query.row_limit).toBeUndefined();
|
2020-06-05 14:57:47 -04:00
|
|
|
expect(query.row_offset).toBeUndefined();
|
2020-04-27 15:47:32 -04:00
|
|
|
});
|
2020-10-27 12:37:22 -04:00
|
|
|
|
|
|
|
it('should populate annotation_layers', () => {
|
|
|
|
const annotationLayers: AnnotationLayer[] = [
|
|
|
|
{
|
2020-12-21 00:03:47 -05:00
|
|
|
annotationType: AnnotationType.Formula,
|
2020-10-27 12:37:22 -04:00
|
|
|
color: '#ff7f44',
|
|
|
|
name: 'My Formula',
|
2020-12-21 00:03:47 -05:00
|
|
|
opacity: AnnotationOpacity.Low,
|
2020-10-27 12:37:22 -04:00
|
|
|
show: true,
|
2022-03-23 06:57:35 -04:00
|
|
|
showLabel: false,
|
2020-12-21 00:03:47 -05:00
|
|
|
style: AnnotationStyle.Solid,
|
2020-10-27 12:37:22 -04:00
|
|
|
value: '10*sin(x)',
|
|
|
|
width: 1,
|
|
|
|
},
|
|
|
|
{
|
2020-12-21 00:03:47 -05:00
|
|
|
annotationType: AnnotationType.Interval,
|
2020-10-27 12:37:22 -04:00
|
|
|
color: null,
|
2020-12-21 00:03:47 -05:00
|
|
|
show: false,
|
2022-03-23 06:57:35 -04:00
|
|
|
showLabel: false,
|
2020-10-27 12:37:22 -04:00
|
|
|
name: 'My Interval',
|
2020-12-21 00:03:47 -05:00
|
|
|
sourceType: AnnotationSourceType.Native,
|
|
|
|
style: AnnotationStyle.Dashed,
|
2020-10-27 12:37:22 -04:00
|
|
|
value: 1,
|
|
|
|
width: 100,
|
|
|
|
},
|
|
|
|
{
|
2020-12-21 00:03:47 -05:00
|
|
|
annotationType: AnnotationType.Event,
|
2020-10-27 12:37:22 -04:00
|
|
|
color: null,
|
|
|
|
descriptionColumns: [],
|
|
|
|
name: 'My Interval',
|
|
|
|
overrides: {
|
|
|
|
granularity: null,
|
|
|
|
time_grain_sqla: null,
|
|
|
|
time_range: null,
|
|
|
|
},
|
2020-12-21 00:03:47 -05:00
|
|
|
sourceType: AnnotationSourceType.Table,
|
|
|
|
show: false,
|
2022-03-23 06:57:35 -04:00
|
|
|
showLabel: false,
|
2020-10-27 12:37:22 -04:00
|
|
|
timeColumn: 'ds',
|
2020-12-21 00:03:47 -05:00
|
|
|
style: AnnotationStyle.Dashed,
|
|
|
|
value: 1,
|
2020-10-27 12:37:22 -04:00
|
|
|
width: 100,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
annotation_layers: annotationLayers,
|
|
|
|
});
|
|
|
|
expect(query.annotation_layers).toEqual(annotationLayers);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should populate url_params', () => {
|
2021-02-04 02:48:53 -05:00
|
|
|
expect(
|
|
|
|
buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
url_params: { abc: '123' },
|
|
|
|
}).url_params,
|
|
|
|
).toEqual({ abc: '123' });
|
|
|
|
expect(
|
|
|
|
buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
2022-04-08 05:32:29 -04:00
|
|
|
// @ts-expect-error
|
|
|
|
url_params: null,
|
2021-02-04 02:48:53 -05:00
|
|
|
}).url_params,
|
|
|
|
).toBeUndefined();
|
2020-10-27 12:37:22 -04:00
|
|
|
});
|
2021-02-11 12:35:54 -05:00
|
|
|
|
|
|
|
it('should populate custom_params', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
const customParams: JsonObject = {
|
|
|
|
customObject: { id: 137, name: 'C-137' },
|
|
|
|
};
|
2021-02-11 12:35:54 -05:00
|
|
|
query = buildQueryObject({
|
|
|
|
datasource: '5__table',
|
|
|
|
granularity_sqla: 'ds',
|
|
|
|
viz_type: 'table',
|
|
|
|
custom_params: customParams,
|
|
|
|
});
|
|
|
|
expect(query.custom_params).toEqual(customParams);
|
|
|
|
});
|
2018-12-04 16:21:49 -05:00
|
|
|
});
|