| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- /**
- * 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.
- */
- import {
- JsonObject,
- AnnotationLayer,
- AnnotationOpacity,
- AnnotationSourceType,
- AnnotationStyle,
- AnnotationType,
- buildQueryObject,
- QueryObject,
- VizType,
- } from '@superset-ui/core';
- describe('buildQueryObject', () => {
- let query: QueryObject;
- it('should build granularity for sqlalchemy datasources', () => {
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- });
- expect(query.granularity).toEqual('ds');
- });
- it('should build metrics based on default queryFields', () => {
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- metric: 'sum__num',
- secondary_metric: 'avg__num',
- });
- expect(query.metrics).toEqual(['sum__num', 'avg__num']);
- });
- it('should merge original and append filters', () => {
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.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: {
- adhoc_filters: [
- {
- expressionType: 'SQL',
- clause: 'WHERE',
- sqlExpression: '(1 = 1)',
- },
- ],
- },
- });
- expect(query.filters).toEqual([
- { col: 'abc', op: '==', val: 'qwerty' },
- { col: 'foo', op: '!=', val: 'bar' },
- ]);
- expect(query.extras?.where).toEqual('(a = b) AND ((1 = 1))');
- });
- it('should group custom metric control', () => {
- query = buildQueryObject(
- {
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- my_custom_metric_control: 'sum__num',
- },
- { my_custom_metric_control: 'metrics' },
- );
- expect(query.metrics).toEqual(['sum__num']);
- });
- it('should group custom metric control with predefined metrics', () => {
- query = buildQueryObject(
- {
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- metrics: ['sum__num'],
- my_custom_metric_control: 'avg__num',
- },
- { my_custom_metric_control: 'metrics' },
- );
- expect(query.metrics).toEqual(['sum__num', 'avg__num']);
- });
- it('should build series_limit from legacy control', () => {
- const series_limit = 2;
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- limit: series_limit,
- });
- expect(query.series_limit).toEqual(series_limit);
- });
- it('should build series_limit', () => {
- const series_limit = 2;
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- series_limit,
- });
- expect(query.series_limit).toEqual(series_limit);
- });
- it('should build order_desc', () => {
- const orderDesc = false;
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- order_desc: orderDesc,
- });
- expect(query.order_desc).toEqual(orderDesc);
- });
- it('should build series_limit_metric from legacy control', () => {
- const metric = 'country';
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- timeseries_limit_metric: metric,
- });
- expect(query.series_limit_metric).toEqual(metric);
- });
- it('should build series_limit_metric', () => {
- const metric = 'country';
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.PivotTable,
- 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: VizType.PivotTable,
- series_limit_metric: metric,
- });
- expect(query.series_limit_metric).toEqual(undefined);
- });
- it('should handle null and non-numeric row_limit and row_offset', () => {
- const baseQuery = {
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- row_limit: null,
- };
- // undefined
- query = buildQueryObject({ ...baseQuery });
- expect(query.row_limit).toBeUndefined();
- expect(query.row_offset).toBeUndefined();
- // null value
- query = buildQueryObject({
- ...baseQuery,
- row_limit: null,
- row_offset: null,
- });
- expect(query.row_limit).toBeUndefined();
- expect(query.row_offset).toBeUndefined();
- query = buildQueryObject({ ...baseQuery, row_limit: 1000, row_offset: 50 });
- expect(query.row_limit).toStrictEqual(1000);
- expect(query.row_offset).toStrictEqual(50);
- // valid string
- query = buildQueryObject({
- ...baseQuery,
- row_limit: '200',
- row_offset: '100',
- });
- expect(query.row_limit).toStrictEqual(200);
- expect(query.row_offset).toStrictEqual(100);
- // invalid string
- query = buildQueryObject({
- ...baseQuery,
- row_limit: 'two hundred',
- row_offset: 'twenty',
- });
- expect(query.row_limit).toBeUndefined();
- expect(query.row_offset).toBeUndefined();
- });
- it('should populate annotation_layers', () => {
- const annotationLayers: AnnotationLayer[] = [
- {
- annotationType: AnnotationType.Formula,
- color: '#ff7f44',
- name: 'My Formula',
- opacity: AnnotationOpacity.Low,
- show: true,
- showLabel: false,
- style: AnnotationStyle.Solid,
- value: '10*sin(x)',
- width: 1,
- },
- {
- annotationType: AnnotationType.Interval,
- color: null,
- show: false,
- showLabel: false,
- name: 'My Interval',
- sourceType: AnnotationSourceType.Native,
- style: AnnotationStyle.Dashed,
- value: 1,
- width: 100,
- },
- {
- annotationType: AnnotationType.Event,
- color: null,
- descriptionColumns: [],
- name: 'My Interval',
- overrides: {
- granularity: null,
- time_grain_sqla: null,
- time_range: null,
- },
- sourceType: AnnotationSourceType.Table,
- show: false,
- showLabel: false,
- timeColumn: 'ds',
- style: AnnotationStyle.Dashed,
- value: 1,
- width: 100,
- },
- ];
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- annotation_layers: annotationLayers,
- });
- expect(query.annotation_layers).toEqual(annotationLayers);
- });
- it('should populate url_params', () => {
- expect(
- buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- url_params: { abc: '123' },
- }).url_params,
- ).toEqual({ abc: '123' });
- expect(
- buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- // @ts-expect-error
- url_params: null,
- }).url_params,
- ).toBeUndefined();
- });
- it('should populate granularity', () => {
- const granularity = 'ds';
- query = buildQueryObject({
- datasource: '5__table',
- granularity,
- viz_type: VizType.Table,
- });
- expect(query.granularity).toEqual(granularity);
- });
- it('should populate granularity from legacy field', () => {
- const granularity = 'ds';
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: granularity,
- viz_type: VizType.Table,
- });
- expect(query.granularity).toEqual(granularity);
- });
- it('should populate custom_params', () => {
- const customParams: JsonObject = {
- customObject: { id: 137, name: 'C-137' },
- };
- query = buildQueryObject({
- datasource: '5__table',
- granularity_sqla: 'ds',
- viz_type: VizType.Table,
- custom_params: customParams,
- });
- expect(query.custom_params).toEqual(customParams);
- });
- });
|