test: exploreUtils (#13719)

* Tests for exploreUtils

* removing eslint-disable

* applying factory

* Removing skip tests and code comments.

* Improving test name

* remove comments

* Improving "shouldUseLegacyApi" tests

* fixing typo
This commit is contained in:
Bruno Motta 2021-04-07 01:35:34 -03:00 committed by GitHub
parent 4602ead10c
commit 1638e6e932
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 522 additions and 2 deletions

View File

@ -0,0 +1,51 @@
/**
* 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 { getAnnotationJsonUrl } from '.';
let windowLocation: any;
beforeAll(() => {
windowLocation = window.location;
// @ts-expect-error
delete window.location;
});
beforeEach(() => {
window.location = {
search: '?testA=0&testB=1',
} as any;
});
afterAll(() => {
window.location = windowLocation;
});
test('get correct annotation when isNative:true', () => {
const response = getAnnotationJsonUrl('slice_id', 'form_data', true);
expect(response).toBe(
'/superset/annotation_json/slice_id?form_data=%22form_data%22',
);
});
test('get correct annotation when isNative:false', () => {
const response = getAnnotationJsonUrl('slice_id', { json: 'my-data' }, false);
expect(response).toBe(
'/superset/slice_json/slice_id?form_data=%7B%22json%22%3A%22my-data%22%7D',
);
});

View File

@ -0,0 +1,73 @@
/**
* 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 { getChartDataUri } from '.';
test('Get ChartUri when allowDomainSharding:false', () => {
expect(
getChartDataUri({
path: '/path',
qs: 'same-string',
allowDomainSharding: false,
}),
).toEqual({
_deferred_build: true,
_parts: {
duplicateQueryParameters: false,
escapeQuerySpace: true,
fragment: null,
hostname: undefined,
password: null,
path: '/path',
port: '',
preventInvalidHostname: false,
protocol: 'http',
query: 'same-string',
urn: null,
username: null,
},
_string: '',
});
});
test('Get ChartUri when allowDomainSharding:true', () => {
expect(
getChartDataUri({
path: '/path-allowDomainSharding-true',
qs: 'same-string-allowDomainSharding-true',
allowDomainSharding: true,
}),
).toEqual({
_deferred_build: true,
_parts: {
duplicateQueryParameters: false,
escapeQuerySpace: true,
fragment: null,
hostname: undefined,
password: null,
path: '/path-allowDomainSharding-true',
port: '',
preventInvalidHostname: false,
protocol: 'http',
query: 'same-string-allowDomainSharding-true',
urn: null,
username: null,
},
_string: '',
});
});

View File

@ -0,0 +1,23 @@
/**
* 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 { getChartKey } from '.';
test('should return "slice_id" when called with an object that has "slice.slice_id"', () => {
expect(getChartKey({ slice: { slice_id: 100 } })).toBe(100);
});

View File

@ -0,0 +1,92 @@
/**
* 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 { getExploreLongUrl } from '.';
const createParams = () => ({
formData: {
datasource: 'datasource',
viz_type: 'viz_type',
},
endpointType: 'endpointType',
allowOverflow: true,
extraSearch: { same: 'any-string' },
});
test('Should return null if formData.datasource is falsy', () => {
const params = createParams();
expect(
getExploreLongUrl(
{},
params.endpointType,
params.allowOverflow,
params.extraSearch,
),
).toBeNull();
});
test('Get url when endpointType:standalone', () => {
const params = createParams();
expect(
getExploreLongUrl(
params.formData,
params.endpointType,
params.allowOverflow,
params.extraSearch,
),
).toBe(
'/superset/explore/?same=any-string&form_data=%7B%22datasource%22%3A%22datasource%22%2C%22viz_type%22%3A%22viz_type%22%7D',
);
});
test('Get url when endpointType:standalone and allowOverflow:false', () => {
const params = createParams();
expect(
getExploreLongUrl(
params.formData,
params.endpointType,
false,
params.extraSearch,
),
).toBe(
'/superset/explore/?same=any-string&form_data=%7B%22datasource%22%3A%22datasource%22%2C%22viz_type%22%3A%22viz_type%22%7D',
);
});
test('Get url when endpointType:results', () => {
const params = createParams();
expect(
getExploreLongUrl(
params.formData,
'results',
params.allowOverflow,
params.extraSearch,
),
).toBe(
'/superset/explore_json/?same=any-string&form_data=%7B%22datasource%22%3A%22datasource%22%2C%22viz_type%22%3A%22viz_type%22%7D',
);
});
test('Get url when endpointType:results and allowOverflow:false', () => {
const params = createParams();
expect(
getExploreLongUrl(params.formData, 'results', false, params.extraSearch),
).toBe(
'/superset/explore_json/?same=any-string&form_data=%7B%22datasource%22%3A%22datasource%22%2C%22viz_type%22%3A%22viz_type%22%7D',
);
});

View File

@ -0,0 +1,51 @@
/**
* 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 { getExploreUrl } from '.';
const createParams = () => ({
formData: {
datasource: 'datasource',
viz_type: 'viz_type',
},
endpointType: 'base',
force: false,
curUrl: null,
requestParams: {},
allowDomainSharding: false,
method: 'POST',
});
test('Get ExploreUrl with default params', () => {
const params = createParams();
expect(getExploreUrl(params)).toBe('http:///superset/explore/');
});
test('Get ExploreUrl with endpointType:full', () => {
const params = createParams();
expect(getExploreUrl({ ...params, endpointType: 'full' })).toBe(
'http:///superset/explore_json/',
);
});
test('Get ExploreUrl with endpointType:full and method:GET', () => {
const params = createParams();
expect(
getExploreUrl({ ...params, endpointType: 'full', method: 'GET' }),
).toBe('http:///superset/explore_json/');
});

View File

@ -0,0 +1,52 @@
/**
* 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 { getHostName } from '.';
jest.mock('src/utils/hostNamesConfig', () => ({
availableDomains: [
'domain-a',
'domain-b',
'domain-c',
'domain-d',
'domain-e',
'domain-f',
'domain-g',
],
}));
test('Should get next diferent domain on a loop, never the first on the list', () => {
for (let loop = 3; loop > 0; loop -= 1) {
expect(getHostName(true)).toBe('domain-b');
expect(getHostName(true)).toBe('domain-c');
expect(getHostName(true)).toBe('domain-d');
expect(getHostName(true)).toBe('domain-e');
expect(getHostName(true)).toBe('domain-f');
expect(getHostName(true)).toBe('domain-g');
}
});
test('Should always return the same domain, the first on the list', () => {
expect(getHostName(false)).toBe('domain-a');
expect(getHostName(false)).toBe('domain-a');
});
test('Should always return the same domain, the first on the list - no args', () => {
expect(getHostName()).toBe('domain-a');
expect(getHostName()).toBe('domain-a');
});

View File

@ -0,0 +1,34 @@
/**
* 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 { getLegacyEndpointType } from '.';
const createParams = () => ({
resultType: 'resultType',
resultFormat: 'resultFormat',
});
test('Should return resultType when resultFormat!=csv', () => {
expect(getLegacyEndpointType(createParams())).toBe('resultType');
});
test('Should return resultFormat when resultFormat:csv', () => {
expect(
getLegacyEndpointType({ ...createParams(), resultFormat: 'csv' }),
).toBe('csv');
});

View File

@ -0,0 +1,62 @@
/**
* 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 { getSimpleSQLExpression } from '.';
const params = {
subject: 'subject',
operator: 'operator',
comparator: 'comparator',
};
test('Should return "" if subject is falsy', () => {
expect(getSimpleSQLExpression('', params.operator, params.comparator)).toBe(
'',
);
expect(getSimpleSQLExpression(null, params.operator, params.comparator)).toBe(
'',
);
expect(
getSimpleSQLExpression(undefined, params.operator, params.comparator),
).toBe('');
});
test('Should return subject if operator is falsy', () => {
expect(getSimpleSQLExpression(params.subject, '', params.comparator)).toBe(
params.subject,
);
expect(getSimpleSQLExpression(params.subject, null, params.comparator)).toBe(
params.subject,
);
expect(
getSimpleSQLExpression(params.subject, undefined, params.comparator),
).toBe(params.subject);
});
test('Should return correct string when subject and operator are valid values', () => {
expect(
getSimpleSQLExpression(params.subject, params.operator, params.comparator),
).toBe("subject operator 'comparator'");
expect(
getSimpleSQLExpression(params.subject, params.operator, [
params.comparator,
'comparator-2',
]),
).toBe("subject operator 'comparator', 'comparator-2'");
});

View File

@ -0,0 +1,30 @@
/**
* 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 { getURIDirectory } from '.';
test('Cases in which the "explore_json" will be returned', () => {
['full', 'json', 'csv', 'query', 'results', 'samples'].forEach(name => {
expect(getURIDirectory(name)).toBe('/superset/explore_json/');
});
});
test('Cases in which the "explore" will be returned', () => {
expect(getURIDirectory('any-string')).toBe('/superset/explore/');
expect(getURIDirectory()).toBe('/superset/explore/');
});

View File

@ -29,8 +29,8 @@ import {
import { availableDomains } from 'src/utils/hostNamesConfig';
import { safeStringify } from 'src/utils/safeStringify';
import { URL_PARAMS } from 'src/constants';
import { MULTI_OPERATORS } from './constants';
import { DashboardStandaloneMode } from '../dashboard/util/constants';
import { MULTI_OPERATORS } from 'src/explore/constants';
import { DashboardStandaloneMode } from 'src/dashboard/util/constants';
const MAX_URL_LENGTH = 8000;

View File

@ -0,0 +1,52 @@
/**
* 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 * as Core from '@superset-ui/core';
import { shouldUseLegacyApi } from '.';
test('Should return false', () => {
const spy = jest.spyOn(Core, 'getChartMetadataRegistry');
const get = jest.fn();
spy.mockReturnValue({ get } as any);
expect(get).toBeCalledTimes(0);
expect(shouldUseLegacyApi({ viz_type: 'name_test' })).toBe(false);
expect(get).toBeCalledTimes(1);
expect(get).toBeCalledWith('name_test');
});
test('Should return true', () => {
const spy = jest.spyOn(Core, 'getChartMetadataRegistry');
const get = jest.fn();
get.mockReturnValue({ useLegacyApi: true });
spy.mockReturnValue({ get } as any);
expect(get).toBeCalledTimes(0);
expect(shouldUseLegacyApi({ viz_type: 'name_test' })).toBe(true);
expect(get).toBeCalledTimes(1);
expect(get).toBeCalledWith('name_test');
});
test('Should return false when useLegacyApi:false', () => {
const spy = jest.spyOn(Core, 'getChartMetadataRegistry');
const get = jest.fn();
get.mockReturnValue({ useLegacyApi: false });
spy.mockReturnValue({ get } as any);
expect(get).toBeCalledTimes(0);
expect(shouldUseLegacyApi({ viz_type: 'name_test' })).toBe(false);
expect(get).toBeCalledTimes(1);
expect(get).toBeCalledWith('name_test');
});