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.
|
|
|
|
*/
|
2022-01-07 20:16:24 -05:00
|
|
|
import { configure, QueryMode, DTTM_ALIAS } from '@superset-ui/core';
|
|
|
|
import extractQueryFields from '../../src/query/extractQueryFields';
|
2021-03-09 21:54:22 -05:00
|
|
|
import { NUM_METRIC } from '../fixtures';
|
2020-05-20 14:13:11 -04:00
|
|
|
|
2021-01-29 04:46:33 -05:00
|
|
|
configure();
|
|
|
|
|
2020-05-20 14:13:11 -04:00
|
|
|
describe('extractQueryFields', () => {
|
|
|
|
it('should return default object', () => {
|
|
|
|
expect(extractQueryFields({})).toEqual({
|
|
|
|
columns: [],
|
|
|
|
metrics: [],
|
2021-03-09 21:54:22 -05:00
|
|
|
orderby: undefined,
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-29 04:46:33 -05:00
|
|
|
it('should group single value to arrays', () => {
|
|
|
|
expect(
|
2021-11-09 07:42:28 -05:00
|
|
|
extractQueryFields({
|
|
|
|
metric: 'my_metric',
|
|
|
|
columns: 'abc',
|
|
|
|
orderby: '["ccc",true]',
|
|
|
|
}),
|
2021-01-29 04:46:33 -05:00
|
|
|
).toEqual({
|
|
|
|
metrics: ['my_metric'],
|
|
|
|
columns: ['abc'],
|
|
|
|
orderby: [['ccc', true]],
|
|
|
|
});
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
|
2021-01-29 04:46:33 -05:00
|
|
|
it('should combine field aliases', () => {
|
2020-05-20 14:13:11 -04:00
|
|
|
expect(
|
|
|
|
extractQueryFields(
|
2021-11-09 07:42:28 -05:00
|
|
|
{
|
|
|
|
metric: 'metric_1',
|
|
|
|
metric_2: 'metric_2',
|
|
|
|
my_custom_metric: 'my_custom_metric',
|
|
|
|
},
|
2020-05-20 14:13:11 -04:00
|
|
|
{ my_custom_metric: 'metrics' },
|
|
|
|
).metrics,
|
2021-01-29 04:46:33 -05:00
|
|
|
).toEqual(['metric_1', 'metric_2', 'my_custom_metric']);
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract columns', () => {
|
|
|
|
expect(extractQueryFields({ columns: 'col_1' })).toEqual({
|
|
|
|
columns: ['col_1'],
|
|
|
|
metrics: [],
|
2021-03-09 21:54:22 -05:00
|
|
|
orderby: undefined,
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-09 21:54:22 -05:00
|
|
|
it('should extract groupby as columns and set empty metrics', () => {
|
2020-05-20 14:13:11 -04:00
|
|
|
expect(extractQueryFields({ groupby: 'col_1' })).toEqual({
|
2021-01-26 06:00:59 -05:00
|
|
|
columns: ['col_1'],
|
2020-05-20 14:13:11 -04:00
|
|
|
metrics: [],
|
2021-03-09 21:54:22 -05:00
|
|
|
orderby: undefined,
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-09 21:54:22 -05:00
|
|
|
it('should remove duplicate metrics', () => {
|
|
|
|
expect(
|
2021-11-09 07:42:28 -05:00
|
|
|
extractQueryFields({
|
|
|
|
metrics: ['col_1', { ...NUM_METRIC }, { ...NUM_METRIC }],
|
|
|
|
}),
|
2021-03-09 21:54:22 -05:00
|
|
|
).toEqual({
|
|
|
|
columns: [],
|
|
|
|
metrics: ['col_1', NUM_METRIC],
|
|
|
|
orderby: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract custom columns fields', () => {
|
2020-05-20 14:13:11 -04:00
|
|
|
expect(
|
2021-11-09 07:42:28 -05:00
|
|
|
extractQueryFields(
|
|
|
|
{ series: 'col_1', metric: 'metric_1' },
|
|
|
|
{ series: 'groupby' },
|
|
|
|
),
|
2020-05-20 14:13:11 -04:00
|
|
|
).toEqual({
|
2021-01-26 06:00:59 -05:00
|
|
|
columns: ['col_1'],
|
2020-05-20 14:13:11 -04:00
|
|
|
metrics: ['metric_1'],
|
2021-03-09 21:54:22 -05:00
|
|
|
orderby: undefined,
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-29 04:46:33 -05:00
|
|
|
it('should merge custom groupby into columns', () => {
|
2020-05-20 14:13:11 -04:00
|
|
|
expect(
|
|
|
|
extractQueryFields(
|
|
|
|
{ groupby: 'col_1', series: 'col_2', metric: 'metric_1' },
|
|
|
|
{ series: 'groupby' },
|
|
|
|
),
|
|
|
|
).toEqual({
|
2021-01-26 06:00:59 -05:00
|
|
|
columns: ['col_1', 'col_2'],
|
2020-05-20 14:13:11 -04:00
|
|
|
metrics: ['metric_1'],
|
2021-03-09 21:54:22 -05:00
|
|
|
orderby: undefined,
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|
|
|
|
});
|
2020-12-21 00:03:47 -05:00
|
|
|
|
|
|
|
it('should include time', () => {
|
|
|
|
expect(
|
2021-11-09 07:42:28 -05:00
|
|
|
extractQueryFields({ groupby: 'col_1', include_time: true }).columns,
|
|
|
|
).toEqual([DTTM_ALIAS, 'col_1']);
|
|
|
|
expect(
|
|
|
|
extractQueryFields({
|
|
|
|
groupby: ['col_1', DTTM_ALIAS, ''],
|
|
|
|
include_time: true,
|
|
|
|
}).columns,
|
2020-12-21 00:03:47 -05:00
|
|
|
).toEqual(['col_1', DTTM_ALIAS]);
|
|
|
|
});
|
2021-01-26 06:00:59 -05:00
|
|
|
|
|
|
|
it('should ignore null values', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
extractQueryFields({ series: ['a'], columns: null }).columns,
|
|
|
|
).toEqual(['a']);
|
2021-01-26 06:00:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore groupby and metrics when in raw QueryMode', () => {
|
|
|
|
expect(
|
|
|
|
extractQueryFields({
|
|
|
|
columns: ['a'],
|
|
|
|
groupby: ['b'],
|
|
|
|
metric: ['m'],
|
|
|
|
query_mode: QueryMode.raw,
|
|
|
|
}),
|
|
|
|
).toEqual({
|
|
|
|
columns: ['a'],
|
2021-03-09 21:54:22 -05:00
|
|
|
metrics: undefined,
|
|
|
|
orderby: undefined,
|
2021-01-26 06:00:59 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore columns when in aggregate QueryMode', () => {
|
2021-03-09 21:54:22 -05:00
|
|
|
expect(
|
|
|
|
extractQueryFields({
|
|
|
|
columns: ['a'],
|
|
|
|
groupby: [],
|
|
|
|
metric: ['m'],
|
|
|
|
query_mode: QueryMode.aggregate,
|
|
|
|
}),
|
|
|
|
).toEqual({
|
|
|
|
metrics: ['m'],
|
|
|
|
columns: [],
|
|
|
|
orderby: undefined,
|
|
|
|
});
|
2021-01-26 06:00:59 -05:00
|
|
|
expect(
|
|
|
|
extractQueryFields({
|
|
|
|
columns: ['a'],
|
|
|
|
groupby: ['b'],
|
|
|
|
metric: ['m'],
|
|
|
|
query_mode: QueryMode.aggregate,
|
|
|
|
}),
|
|
|
|
).toEqual({
|
|
|
|
metrics: ['m'],
|
|
|
|
columns: ['b'],
|
2021-03-09 21:54:22 -05:00
|
|
|
orderby: undefined,
|
2021-01-29 04:46:33 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse orderby if needed', () => {
|
|
|
|
expect(
|
|
|
|
extractQueryFields({
|
|
|
|
columns: ['a'],
|
|
|
|
order_by_cols: ['["foo",false]', '["bar",true]'],
|
|
|
|
orderby: [['abc', true]],
|
|
|
|
}),
|
|
|
|
).toEqual({
|
|
|
|
columns: ['a'],
|
|
|
|
metrics: [],
|
|
|
|
orderby: [
|
|
|
|
['foo', false],
|
|
|
|
['bar', true],
|
|
|
|
['abc', true],
|
|
|
|
],
|
2021-01-26 06:00:59 -05:00
|
|
|
});
|
|
|
|
});
|
2021-03-09 21:54:22 -05:00
|
|
|
|
2021-01-29 04:46:33 -05:00
|
|
|
it('should throw error if parse orderby failed', () => {
|
|
|
|
expect(() => {
|
|
|
|
extractQueryFields({
|
|
|
|
orderby: ['ccc'],
|
|
|
|
});
|
|
|
|
}).toThrow('invalid orderby');
|
|
|
|
});
|
2020-05-20 14:13:11 -04:00
|
|
|
});
|