betodealmeida commented on code in PR #32548: URL: https://github.com/apache/superset/pull/32548#discussion_r1985859178
########## superset-frontend/src/explore/components/DatasourcePanel/transformDatasourceFolders.test.ts: ########## @@ -0,0 +1,198 @@ +/** + * 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 { Metric } from '@superset-ui/core'; +import { transformDatasourceWithFolders } from './transformDatasourceFolders'; +import { DatasourceFolder, DatasourcePanelColumn } from './types'; + +const mockMetrics: Metric[] = [ + { metric_name: 'metric1', uuid: 'metric1-uuid', expression: 'SUM(col1)' }, + { metric_name: 'metric2', uuid: 'metric2-uuid', expression: 'AVG(col2)' }, + { metric_name: 'metric3', uuid: 'metric3-uuid', expression: 'COUNT(*)' }, +]; + +const mockColumns: DatasourcePanelColumn[] = [ + { column_name: 'column1', uuid: 'column1-uuid', type: 'STRING' }, + { column_name: 'column2', uuid: 'column2-uuid', type: 'NUMERIC' }, + { column_name: 'column3', uuid: 'column3-uuid', type: 'DATETIME' }, +]; + +test('transforms data into default folders when no folder config is provided', () => { + const result = transformDatasourceWithFolders( + mockMetrics, + mockColumns, + undefined, + ); + + expect(result).toHaveLength(2); + + expect(result[0].id).toBe('metrics-default'); + expect(result[0].name).toBe('Metrics'); + expect(result[0].items).toHaveLength(3); + expect(result[0].items[0].uuid).toBe('metric1-uuid'); + expect(result[0].items[0].type).toBe('metric'); + + expect(result[1].id).toBe('columns-default'); + expect(result[1].name).toBe('Columns'); + expect(result[1].items).toHaveLength(3); + expect(result[1].items[0].uuid).toBe('column1-uuid'); + expect(result[1].items[0].type).toBe('column'); +}); + +test('transforms data according to folder configuration', () => { + const folderConfig: DatasourceFolder[] = [ + { + uuid: 'folder1', + type: 'folder', + name: 'Important Metrics', + description: 'Key metrics folder', + children: [ + { type: 'metric', uuid: 'metric1-uuid', name: 'metric1' }, + { type: 'metric', uuid: 'metric2-uuid', name: 'metric2' }, + ], + }, + { + uuid: 'folder2', + type: 'folder', + name: 'Key Dimensions', + children: [{ type: 'column', uuid: 'column1-uuid', name: 'column1' }], + }, + ]; + + const result = transformDatasourceWithFolders( + mockMetrics, + mockColumns, + folderConfig, + ); + + // We expect 4 folders: + // 1. Important Metrics (from config) + // 2. Key Dimensions (from config) + // 3. Metrics (default for unassigned metrics) + // 4. Columns (default for unassigned columns) + expect(result).toHaveLength(4); + + expect(result[0].id).toBe('folder1'); + expect(result[0].name).toBe('Important Metrics'); + expect(result[0].description).toBe('Key metrics folder'); + expect(result[0].items).toHaveLength(2); + expect(result[0].items[0].uuid).toBe('metric1-uuid'); + + expect(result[1].id).toBe('folder2'); + expect(result[1].name).toBe('Key Dimensions'); + expect(result[1].items).toHaveLength(1); + expect(result[1].items[0].uuid).toBe('column1-uuid'); + + expect(result[2].id).toBe('metrics-default'); + expect(result[2].items).toHaveLength(1); + expect(result[2].items[0].uuid).toBe('metric3-uuid'); + + expect(result[3].id).toBe('columns-default'); + expect(result[3].items).toHaveLength(2); +}); + +test('handles nested folder structures', () => { + const folderConfig: DatasourceFolder[] = [ + { + uuid: 'parent-folder', + type: 'folder', + name: 'Parent Folder', + children: [ + { + uuid: 'child-folder', + type: 'folder', + name: 'Child Folder', + children: [{ type: 'metric', uuid: 'metric1-uuid', name: 'metric1' }], + }, + { type: 'column', uuid: 'column1-uuid', name: 'column1' }, + ], + }, + ]; + + const result = transformDatasourceWithFolders( + mockMetrics, + mockColumns, + folderConfig, + ); + + expect(result[0].id).toBe('parent-folder'); + expect(result[0].name).toBe('Parent Folder'); + expect(result[0].items).toHaveLength(1); + expect(result[0].subFolders).toHaveLength(1); + + const childFolder = result[0].subFolders![0]; + expect(childFolder.id).toBe('child-folder'); + expect(childFolder.name).toBe('Child Folder'); + expect(childFolder.items).toHaveLength(1); + expect(childFolder.parentId).toBe('parent-folder'); +}); + +test('handles empty children arrays', () => { + const folderConfig: DatasourceFolder[] = [ + { + uuid: 'empty-folder', + type: 'folder', + name: 'Empty Folder', + children: [], + }, + ]; + + const result = transformDatasourceWithFolders( + mockMetrics, + mockColumns, + folderConfig, + ); + + expect(result[0].id).toBe('empty-folder'); + expect(result[0].name).toBe('Empty Folder'); + expect(result[0].items).toHaveLength(0); +}); + +test('handles non-existent metric and column UUIDs in folder config', () => { Review Comment: It's great that you added this test! ########## superset-frontend/src/explore/components/DatasourcePanel/transformDatasourceFolders.ts: ########## @@ -0,0 +1,155 @@ +/** + * 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 { Metric, t } from '@superset-ui/core'; +import { + ColumnItem, + DatasourceFolder, + DatasourcePanelColumn, + Folder, + MetricItem, +} from './types'; + +const transformToFolderStructure = ( Review Comment: Nice! ❤️ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
