feat: generate consistent QueryObject whether GenericAxis is enabled or disabled (#21519)

This commit is contained in:
Yongjie Zhao
2022-09-21 09:41:21 +08:00
committed by GitHub
parent 6644a84f79
commit 4d12e3709e
22 changed files with 474 additions and 65 deletions

View File

@@ -267,7 +267,22 @@ test('should compile AA in query B', () => {
});
});
test('should compile query objects with x-axis', () => {
test('should convert a queryObject with x-axis although FF is disabled', () => {
let windowSpy: any;
beforeAll(() => {
// @ts-ignore
windowSpy = jest.spyOn(window, 'window', 'get').mockImplementation(() => ({
featureFlags: {
GENERIC_CHART_AXES: false,
},
}));
});
afterAll(() => {
windowSpy.mockRestore();
});
const { queries } = buildQuery({
...formDataMixedChart,
x_axis: 'my_index',
@@ -280,11 +295,19 @@ test('should compile query objects with x-axis', () => {
filters: [],
extras: {
having: '',
time_grain_sqla: 'P1W',
where: "(foo in ('a', 'b'))",
},
applied_time_extras: {},
columns: ['my_index', 'foo'],
columns: [
{
columnType: 'BASE_AXIS',
expressionType: 'SQL',
label: 'my_index',
sqlExpression: 'my_index',
timeGrain: 'P1W',
},
'foo',
],
metrics: ['sum(sales)'],
annotation_layers: [],
row_limit: 10,
@@ -296,7 +319,6 @@ test('should compile query objects with x-axis', () => {
url_params: {},
custom_params: {},
custom_form_data: {},
is_timeseries: false,
time_offsets: [],
post_processing: [
{
@@ -332,8 +354,16 @@ test('should compile query objects with x-axis', () => {
// check the main props on the second query
expect(queries[1]).toEqual(
expect.objectContaining({
is_timeseries: false,
columns: ['my_index'],
columns: [
{
columnType: 'BASE_AXIS',
expressionType: 'SQL',
label: 'my_index',
sqlExpression: 'my_index',
timeGrain: 'P1W',
},
],
granularity: 'ds',
series_columns: [],
metrics: ['count'],
post_processing: [
@@ -357,3 +387,90 @@ test('should compile query objects with x-axis', () => {
}),
);
});
test("shouldn't convert a queryObject with axis although FF is enabled", () => {
let windowSpy: any;
beforeAll(() => {
// @ts-ignore
windowSpy = jest.spyOn(window, 'window', 'get').mockImplementation(() => ({
featureFlags: {
GENERIC_CHART_AXES: true,
},
}));
});
afterAll(() => {
windowSpy.mockRestore();
});
const { queries } = buildQuery(formDataMixedChart);
expect(queries[0]).toEqual(
expect.objectContaining({
granularity: 'ds',
columns: ['foo'],
series_columns: ['foo'],
metrics: ['sum(sales)'],
is_timeseries: true,
extras: {
time_grain_sqla: 'P1W',
having: '',
where: "(foo in ('a', 'b'))",
},
post_processing: [
{
operation: 'pivot',
options: {
aggregates: {
'sum(sales)': {
operator: 'mean',
},
},
columns: ['foo'],
drop_missing_columns: false,
index: ['__timestamp'],
},
},
{
operation: 'rename',
options: { columns: { 'sum(sales)': null }, inplace: true, level: 0 },
},
{
operation: 'flatten',
},
],
}),
);
expect(queries[1]).toEqual(
expect.objectContaining({
granularity: 'ds',
columns: [],
series_columns: [],
metrics: ['count'],
is_timeseries: true,
extras: {
time_grain_sqla: 'P1W',
having: '',
where: "(name in ('c', 'd'))",
},
post_processing: [
{
operation: 'pivot',
options: {
aggregates: {
count: {
operator: 'mean',
},
},
columns: [],
drop_missing_columns: false,
index: ['__timestamp'],
},
},
{
operation: 'flatten',
},
],
}),
);
});

View File

@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { SqlaFormData } from '@superset-ui/core';
import buildQuery from '../../src/Timeseries/buildQuery';
describe('Timeseries buildQuery', () => {
@@ -59,3 +60,181 @@ describe('Timeseries buildQuery', () => {
expect(query.orderby).toEqual([['foo', true]]);
});
});
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' },
],
}),
);
});
});