Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: exploreUtils #13719

Merged
merged 8 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions superset-frontend/src/explore/exploreUtils/exploreChart.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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 { exploreChart } from '.';

/**
* I'm leaving this comment here to show one of the approaches I tried and it didn't work.
*
* The same problem occurred with: "exploreChart"
*/
// import * as index from './index';
// const spy = jest.spyOn(index, 'postForm');

const params = {
formData: { data: 'same-string' },
resultFormat: 'json',
resultType: 'full',
force: false,
};

/**
* For some reason the spy is not working properly and I also did not find a way to mock only the "postForm"
* I believe that if this file is divided the problem will be solved.
*/
test.skip('Should call postForm correctly', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exploreChart is just calling getExploreUrl and postForm. I don't think is necessary to create a test for exploreChart. Just testing getExploreUrl and postForm is enough.

exploreChart(params);
// expect(spy).toBeCalledTimes(1);
});
Original file line number Diff line number Diff line change
@@ -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',
);
});
73 changes: 73 additions & 0 deletions superset-frontend/src/explore/exploreUtils/getChartDataUri.test.ts
Original file line number Diff line number Diff line change
@@ -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: '',
});
});
37 changes: 37 additions & 0 deletions superset-frontend/src/explore/exploreUtils/getChartKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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 '.';

// This validation has not been implemented
test.skip('Should return 0 when called without arguments', () => {
expect(getChartKey()).toBe(0);
});

test('should return 0 when called with empty object', () => {
expect(getChartKey({})).toBe(0);
});

// The function does not yet have this behavior
test.skip('should return 0 when called with incomplete object', () => {
expect(getChartKey({ slice: {} })).toBe(0);
});

test('should return 0 when called with complete object', () => {
expect(getChartKey({ slice: { slice_id: 100 } })).toBe(100);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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 '.';

let params = {
formData: {
datasource: 'datasource',
viz_type: 'viz_type',
},
endpointType: 'standalone',
allowOverflow: true,
extraSearch: { same: 'any-string' },
};

beforeEach(() => {
params = {
formData: {
datasource: 'datasource',
viz_type: 'viz_type',
},
endpointType: 'endpointType',
allowOverflow: true,
extraSearch: { same: 'any-string' },
};
});

test('Should return null if formData.datasource is falsy', () => {
expect(
getExploreLongUrl(
{},
params.endpointType,
params.allowOverflow,
params.extraSearch,
),
).toBeNull();
});

test('Get url when endpointType:standalone', () => {
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', () => {
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', () => {
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', () => {
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',
);
});
63 changes: 63 additions & 0 deletions superset-frontend/src/explore/exploreUtils/getExploreUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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 '.';

let params = {
formData: {
datasource: 'datasource',
viz_type: 'viz_type',
},
endpointType: 'base',
force: false,
curUrl: null,
requestParams: {},
allowDomainSharding: false,
method: 'POST',
};

beforeEach(() => {
params = {
formData: {
datasource: 'datasource',
viz_type: 'viz_type',
},
endpointType: 'base',
force: false,
curUrl: null,
requestParams: {},
allowDomainSharding: false,
method: 'POST',
};
});

test('Get ExploreUrl whit default params', () => {
expect(getExploreUrl(params)).toBe('http:///superset/explore/');
});

test('Get ExploreUrl whit endpointType:full', () => {
expect(getExploreUrl({ ...params, endpointType: 'full' })).toBe(
'http:///superset/explore_json/',
);
});

test('Get ExploreUrl whit endpointType:full and method:GET', () => {
expect(
getExploreUrl({ ...params, endpointType: 'full', method: 'GET' }),
).toBe('http:///superset/explore_json/');
});
Loading