2021-02-01 02:12:55 -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.
|
|
|
|
*/
|
2023-07-20 13:02:52 -04:00
|
|
|
import { QueryMode, TimeGranularity } from '@superset-ui/core';
|
|
|
|
import * as supersetCoreModule from '@superset-ui/core';
|
2021-03-08 14:23:24 -05:00
|
|
|
import buildQuery from '../src/buildQuery';
|
2021-02-01 02:12:55 -05:00
|
|
|
import { TableChartFormData } from '../src/types';
|
|
|
|
|
|
|
|
const basicFormData: TableChartFormData = {
|
|
|
|
viz_type: 'table',
|
|
|
|
datasource: '11__table',
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('plugin-chart-table', () => {
|
|
|
|
describe('buildQuery', () => {
|
2021-03-08 14:23:24 -05:00
|
|
|
it('should add post-processing and ignore duplicate metrics', () => {
|
|
|
|
const query = buildQuery({
|
2021-02-01 02:12:55 -05:00
|
|
|
...basicFormData,
|
|
|
|
query_mode: QueryMode.aggregate,
|
2021-03-08 14:23:24 -05:00
|
|
|
metrics: ['aaa', 'aaa'],
|
|
|
|
percent_metrics: ['bbb', 'bbb'],
|
2021-02-01 02:12:55 -05:00
|
|
|
}).queries[0];
|
2021-03-08 14:23:24 -05:00
|
|
|
expect(query.metrics).toEqual(['aaa', 'bbb']);
|
2021-02-01 02:12:55 -05:00
|
|
|
expect(query.post_processing).toEqual([
|
|
|
|
{
|
|
|
|
operation: 'contribution',
|
|
|
|
options: {
|
2021-03-08 14:23:24 -05:00
|
|
|
columns: ['bbb'],
|
|
|
|
rename_columns: ['%bbb'],
|
2021-02-01 02:12:55 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2021-03-12 17:02:31 -05:00
|
|
|
it('should not add metrics in raw records mode', () => {
|
|
|
|
const query = buildQuery({
|
|
|
|
...basicFormData,
|
|
|
|
query_mode: QueryMode.raw,
|
|
|
|
columns: ['a'],
|
|
|
|
metrics: ['aaa', 'aaa'],
|
|
|
|
percent_metrics: ['bbb', 'bbb'],
|
|
|
|
}).queries[0];
|
|
|
|
expect(query.metrics).toBeUndefined();
|
|
|
|
expect(query.post_processing).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2021-03-08 14:23:24 -05:00
|
|
|
it('should not add post-processing when there is no percent metric', () => {
|
|
|
|
const query = buildQuery({
|
2021-02-01 02:12:55 -05:00
|
|
|
...basicFormData,
|
|
|
|
query_mode: QueryMode.aggregate,
|
|
|
|
metrics: ['aaa'],
|
|
|
|
percent_metrics: [],
|
|
|
|
}).queries[0];
|
|
|
|
expect(query.metrics).toEqual(['aaa']);
|
|
|
|
expect(query.post_processing).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add post-processing in raw records mode', () => {
|
2021-03-08 14:23:24 -05:00
|
|
|
const query = buildQuery({
|
2021-02-01 02:12:55 -05:00
|
|
|
...basicFormData,
|
|
|
|
query_mode: QueryMode.raw,
|
|
|
|
metrics: ['aaa'],
|
|
|
|
columns: ['rawcol'],
|
|
|
|
percent_metrics: ['ccc'],
|
|
|
|
}).queries[0];
|
2021-03-12 17:02:31 -05:00
|
|
|
expect(query.metrics).toBeUndefined();
|
2021-02-01 02:12:55 -05:00
|
|
|
expect(query.columns).toEqual(['rawcol']);
|
|
|
|
expect(query.post_processing).toEqual([]);
|
|
|
|
});
|
2023-07-20 13:02:52 -04:00
|
|
|
it('should prefer extra_form_data.time_grain_sqla over formData.time_grain_sqla', () => {
|
|
|
|
Object.defineProperty(supersetCoreModule, 'hasGenericChartAxes', {
|
|
|
|
value: true,
|
|
|
|
});
|
|
|
|
const query = buildQuery({
|
|
|
|
...basicFormData,
|
|
|
|
groupby: ['col1'],
|
|
|
|
query_mode: QueryMode.aggregate,
|
|
|
|
time_grain_sqla: TimeGranularity.MONTH,
|
|
|
|
extra_form_data: { time_grain_sqla: TimeGranularity.QUARTER },
|
|
|
|
temporal_columns_lookup: { col1: true },
|
|
|
|
}).queries[0];
|
|
|
|
expect(query.columns?.[0]).toEqual({
|
|
|
|
timeGrain: TimeGranularity.QUARTER,
|
|
|
|
columnType: 'BASE_AXIS',
|
|
|
|
sqlExpression: 'col1',
|
|
|
|
label: 'col1',
|
|
|
|
expressionType: 'SQL',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should fallback to formData.time_grain_sqla if extra_form_data.time_grain_sqla is not set', () => {
|
|
|
|
Object.defineProperty(supersetCoreModule, 'hasGenericChartAxes', {
|
|
|
|
value: true,
|
|
|
|
});
|
|
|
|
const query = buildQuery({
|
|
|
|
...basicFormData,
|
|
|
|
time_grain_sqla: TimeGranularity.MONTH,
|
|
|
|
groupby: ['col1'],
|
|
|
|
query_mode: QueryMode.aggregate,
|
|
|
|
temporal_columns_lookup: { col1: true },
|
|
|
|
}).queries[0];
|
|
|
|
expect(query.columns?.[0]).toEqual({
|
|
|
|
timeGrain: TimeGranularity.MONTH,
|
|
|
|
columnType: 'BASE_AXIS',
|
|
|
|
sqlExpression: 'col1',
|
|
|
|
label: 'col1',
|
|
|
|
expressionType: 'SQL',
|
|
|
|
});
|
|
|
|
});
|
2021-02-01 02:12:55 -05:00
|
|
|
});
|
|
|
|
});
|