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

fix: superset-ui/core coverage #20324

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion superset-frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = {
'tmp/',
'dist/',
],
coverageReporters: ['lcov', 'json-summary', 'html'],
coverageReporters: ['lcov', 'json-summary', 'html', 'text'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
snapshotSerializers: ['@emotion/jest/enzyme-serializer'],
globals: {
Expand Down
22 changes: 19 additions & 3 deletions superset-frontend/packages/superset-ui-chart-controls/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { ReactNode, ReactText, ReactElement } from 'react';
import React, { ReactElement, ReactNode, ReactText } from 'react';
import type {
AdhocColumn,
Column,
DatasourceType,
JsonValue,
Metric,
QueryFormColumn,
QueryFormData,
QueryResponse,
QueryFormMetric,
QueryFormColumn,
QueryResponse,
} from '@superset-ui/core';
import { sharedControls } from './shared-controls';
import sharedControlComponents from './shared-controls/components';
Expand Down Expand Up @@ -437,3 +437,19 @@ export function isControlPanelSectionConfig(
): section is ControlPanelSectionConfig {
return section !== null;
}

export function isDataset(
datasource: Dataset | QueryResponse | null | undefined,
): datasource is Dataset {
return !!datasource && 'columns' in datasource;
}

export function isQueryResponse(
datasource: Dataset | QueryResponse | null | undefined,
): datasource is QueryResponse {
return (
!!datasource &&
('results' in datasource ||
datasource?.type === ('query' as DatasourceType.Query))
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,33 @@
* specific language governing permissions and limitations
* under the License.
*/
import { QueryResponse } from '@superset-ui/core';
import { Dataset } from '../types';
import { ensureIsArray, QueryResponse } from '@superset-ui/core';
import { Dataset, isColumnMeta, isDataset, isQueryResponse } from '../types';

/**
* Convert Datasource columns to column choices
*/
export default function columnChoices(
datasource?: Dataset | QueryResponse | null,
): [string, string][] {
if (datasource?.columns[0]?.hasOwnProperty('column_name')) {
return (
(datasource as Dataset)?.columns
.map((col): [string, string] => [
col.column_name,
col.verbose_name || col.column_name,
])
.sort((opt1, opt2) =>
opt1[1].toLowerCase() > opt2[1].toLowerCase() ? 1 : -1,
) || []
);
if (isDataset(datasource) && isColumnMeta(datasource.columns[0])) {
return datasource.columns
.map((col): [string, string] => [
col.column_name,
col.verbose_name || col.column_name,
])
.sort((opt1, opt2) =>
opt1[1].toLowerCase() > opt2[1].toLowerCase() ? 1 : -1,
);
}
return (
(datasource as QueryResponse)?.columns

if (isQueryResponse(datasource)) {
return ensureIsArray(datasource.columns)
.map((col): [string, string] => [col.name, col.name])
.sort((opt1, opt2) =>
opt1[1].toLowerCase() > opt2[1].toLowerCase() ? 1 : -1,
) || []
);
);
}

return [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,31 @@ import { defineSavedMetrics } from '@superset-ui/chart-controls';

describe('defineSavedMetrics', () => {
it('defines saved metrics if source is a Dataset', () => {
expect(
defineSavedMetrics({
id: 1,
metrics: [
{
metric_name: 'COUNT(*) non-default-dataset-metric',
expression: 'COUNT(*) non-default-dataset-metric',
},
],
type: DatasourceType.Table,
main_dttm_col: 'test',
time_grain_sqla: 'P1D',
columns: [],
verbose_map: {},
column_format: {},
datasource_name: 'my_datasource',
description: 'this is my datasource',
}),
).toEqual([
const dataset = {
id: 1,
metrics: [
{
metric_name: 'COUNT(*) non-default-dataset-metric',
expression: 'COUNT(*) non-default-dataset-metric',
},
],
type: DatasourceType.Table,
main_dttm_col: 'test',
time_grain_sqla: 'P1D',
columns: [],
verbose_map: {},
column_format: {},
datasource_name: 'my_datasource',
description: 'this is my datasource',
};
expect(defineSavedMetrics(dataset)).toEqual([
{
metric_name: 'COUNT(*) non-default-dataset-metric',
expression: 'COUNT(*) non-default-dataset-metric',
},
]);
// @ts-ignore
expect(defineSavedMetrics({ ...dataset, metrics: undefined })).toEqual([]);
});

it('returns default saved metrics if souce is a Query', () => {
Expand Down
1 change: 0 additions & 1 deletion superset-frontend/packages/superset-ui-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export * from './utils';
export * from './types';
export * from './translation';
export * from './connection';
export * from './dashboard';
export * from './dynamic-plugins';
export * from './query';
export * from './number-format';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ export type DashboardComponentMetadata = {
nativeFilters: NativeFiltersState;
dataMask: DataMaskStateWithId;
};

export default {};
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,14 @@ export const testQuery: Query = {
is_dttm: false,
},
{
name: 'Column 2',
name: 'Column 3',
type: DatasourceType.Query,
is_dttm: true,
is_dttm: false,
},
{
name: 'Column 3',
name: 'Column 2',
type: DatasourceType.Query,
is_dttm: false,
is_dttm: true,
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from './QueryResponse';
export * from './Time';
export * from './AdvancedAnalytics';
export * from './PostProcessing';
export * from './Dashboard';

export { default as __hack_reexport_Datasource } from './Datasource';
export { default as __hack_reexport_Column } from './Column';
Expand All @@ -36,5 +37,6 @@ export { default as __hack_reexport_QueryResponse } from './QueryResponse';
export { default as __hack_reexport_QueryFormData } from './QueryFormData';
export { default as __hack_reexport_Time } from './Time';
export { default as __hack_reexport_AdvancedAnalytics } from './AdvancedAnalytics';
export { default as __hack_reexport_Dashboard } from './Dashboard';

export default {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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 {
isNativeFilter,
isFilterDivider,
Filter,
NativeFilterType,
} from '@superset-ui/core';

test('should do native filter type guard', () => {
const dummyFilter: Filter = {
cascadeParentIds: [],
defaultDataMask: {},
id: 'dummyID',
name: 'dummyName',
scope: { rootPath: [], excluded: [] },
filterType: 'dummyType',
targets: [{}],
controlValues: {},
type: NativeFilterType.NATIVE_FILTER,
description: 'dummyDesc',
};
expect(isNativeFilter(dummyFilter)).toBeTruthy();
expect(
isFilterDivider({
...dummyFilter,
type: NativeFilterType.DIVIDER,
title: 'dummyTitle',
}),
).toBeTruthy();
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import { getUiOverrideRegistry } from '@superset-ui/core';

export * from './types/Base';
test('should get instance of getUiOverrideRegistry', () => {
expect(getUiOverrideRegistry().name).toBe('UiOverrideRegistry');
});