From 1638e6e9322737cf2111abf1e5cb8f87aefe7982 Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Wed, 7 Apr 2021 01:35:34 -0300 Subject: [PATCH] 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 --- .../exploreUtils/getAnnotationJsonUrl.test.ts | 51 ++++++++++ .../exploreUtils/getChartDataUri.test.ts | 73 +++++++++++++++ .../explore/exploreUtils/getChartKey.test.ts | 23 +++++ .../exploreUtils/getExploreLongUrl.test.ts | 92 +++++++++++++++++++ .../exploreUtils/getExploreUrl.test.ts | 51 ++++++++++ .../explore/exploreUtils/getHostName.test.ts | 52 +++++++++++ .../getLegacyEndpointType.test.ts | 34 +++++++ .../getSimpleSQLExpression.test.ts | 62 +++++++++++++ .../exploreUtils/getURIDirectory.test.ts | 30 ++++++ .../index.js} | 4 +- .../exploreUtils/shouldUseLegacyApi.test.ts | 52 +++++++++++ 11 files changed, 522 insertions(+), 2 deletions(-) create mode 100644 superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getChartDataUri.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getChartKey.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getExploreLongUrl.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getExploreUrl.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getHostName.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getLegacyEndpointType.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getSimpleSQLExpression.test.ts create mode 100644 superset-frontend/src/explore/exploreUtils/getURIDirectory.test.ts rename superset-frontend/src/explore/{exploreUtils.js => exploreUtils/index.js} (98%) create mode 100644 superset-frontend/src/explore/exploreUtils/shouldUseLegacyApi.test.ts diff --git a/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts b/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts new file mode 100644 index 000000000..388c3b9a0 --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts @@ -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', + ); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getChartDataUri.test.ts b/superset-frontend/src/explore/exploreUtils/getChartDataUri.test.ts new file mode 100644 index 000000000..c61612d9c --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getChartDataUri.test.ts @@ -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: '', + }); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getChartKey.test.ts b/superset-frontend/src/explore/exploreUtils/getChartKey.test.ts new file mode 100644 index 000000000..cd2561e3a --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getChartKey.test.ts @@ -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); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getExploreLongUrl.test.ts b/superset-frontend/src/explore/exploreUtils/getExploreLongUrl.test.ts new file mode 100644 index 000000000..6c69e469b --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getExploreLongUrl.test.ts @@ -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', + ); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getExploreUrl.test.ts b/superset-frontend/src/explore/exploreUtils/getExploreUrl.test.ts new file mode 100644 index 000000000..efa4b6ed8 --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getExploreUrl.test.ts @@ -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/'); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getHostName.test.ts b/superset-frontend/src/explore/exploreUtils/getHostName.test.ts new file mode 100644 index 000000000..6b90cc0f5 --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getHostName.test.ts @@ -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'); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getLegacyEndpointType.test.ts b/superset-frontend/src/explore/exploreUtils/getLegacyEndpointType.test.ts new file mode 100644 index 000000000..31655fec5 --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getLegacyEndpointType.test.ts @@ -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'); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getSimpleSQLExpression.test.ts b/superset-frontend/src/explore/exploreUtils/getSimpleSQLExpression.test.ts new file mode 100644 index 000000000..54fc9022f --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getSimpleSQLExpression.test.ts @@ -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'"); +}); diff --git a/superset-frontend/src/explore/exploreUtils/getURIDirectory.test.ts b/superset-frontend/src/explore/exploreUtils/getURIDirectory.test.ts new file mode 100644 index 000000000..c8d43f413 --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/getURIDirectory.test.ts @@ -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/'); +}); diff --git a/superset-frontend/src/explore/exploreUtils.js b/superset-frontend/src/explore/exploreUtils/index.js similarity index 98% rename from superset-frontend/src/explore/exploreUtils.js rename to superset-frontend/src/explore/exploreUtils/index.js index efd67acf3..a09f6b7cc 100644 --- a/superset-frontend/src/explore/exploreUtils.js +++ b/superset-frontend/src/explore/exploreUtils/index.js @@ -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; diff --git a/superset-frontend/src/explore/exploreUtils/shouldUseLegacyApi.test.ts b/superset-frontend/src/explore/exploreUtils/shouldUseLegacyApi.test.ts new file mode 100644 index 000000000..8b4599ce6 --- /dev/null +++ b/superset-frontend/src/explore/exploreUtils/shouldUseLegacyApi.test.ts @@ -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'); +});