From ce474051b0a62c0c64e261afe9123f806d22814f Mon Sep 17 00:00:00 2001 From: Ashish Gupta Date: Fri, 27 Sep 2024 18:07:48 +0530 Subject: [PATCH] unit test around csv utils --- .../ui/src/utils/CSV/CSV.utils.test.tsx | 109 ++++++++++++++ .../src/utils/CSV/CSVUtilsClassBase.test.tsx | 133 ++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSV.utils.test.tsx create mode 100644 openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSVUtilsClassBase.test.tsx diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSV.utils.test.tsx b/openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSV.utils.test.tsx new file mode 100644 index 000000000000..0c014da2b2f2 --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSV.utils.test.tsx @@ -0,0 +1,109 @@ +/* + * Copyright 2024 Collate. + * Licensed 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 { EntityType } from '../../enums/entity.enum'; +import { + getColumnConfig, + getCSVStringFromColumnsAndDataSource, + getEntityColumnsAndDataSourceFromCSV, +} from './CSV.utils'; + +describe('CSVUtils', () => { + describe('getColumnConfig', () => { + it('should return the column configuration object', () => { + const column = 'description'; + const columnConfig = getColumnConfig(column, EntityType.GLOSSARY); + + expect(columnConfig).toBeDefined(); + expect(columnConfig.name).toBe(column); + }); + }); + + describe('getEntityColumnsAndDataSourceFromCSV', () => { + it('should return the columns and data source from the CSV', () => { + const csv = [ + ['col1', 'col2'], + ['value1', 'value2'], + ]; + const { columns, dataSource } = getEntityColumnsAndDataSourceFromCSV( + csv, + EntityType.GLOSSARY + ); + + expect(columns).toHaveLength(2); + expect(dataSource).toHaveLength(1); + }); + }); + + describe('getCSVStringFromColumnsAndDataSource', () => { + it('should return the CSV string from the columns and data source for non-quoted columns', () => { + const columns = [{ name: 'col1' }, { name: 'col2' }]; + const dataSource = [{ col1: 'value1', col2: 'value2' }]; + const csvString = getCSVStringFromColumnsAndDataSource( + columns, + dataSource + ); + + expect(csvString).toBe('col1,col2\nvalue1,value2'); + }); + + it('should return the CSV string from the columns and data source with quoted columns', () => { + const columns = [ + { name: 'tags' }, + { name: 'glossaryTerms' }, + { name: 'description' }, + { name: 'domain' }, + ]; + const dataSource = [ + { + tags: 'value1', + glossaryTerms: 'value2', + description: 'something new', + domain: 'domain1', + }, + ]; + const csvString = getCSVStringFromColumnsAndDataSource( + columns, + dataSource + ); + + expect(csvString).toBe( + 'tags,glossaryTerms,description,domain\n"value1","value2",something new,"domain1"' + ); + }); + + it('should return quoted value if data contains comma', () => { + const columns = [ + { name: 'tags' }, + { name: 'glossaryTerms' }, + { name: 'description' }, + { name: 'domain' }, + ]; + const dataSource = [ + { + tags: 'value,1', + glossaryTerms: 'value_2', + description: 'something#new', + domain: 'domain,1', + }, + ]; + const csvString = getCSVStringFromColumnsAndDataSource( + columns, + dataSource + ); + + expect(csvString).toBe( + `tags,glossaryTerms,description,domain\n"value,1","value_2",something#new,"domain,1"` + ); + }); + }); +}); diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSVUtilsClassBase.test.tsx b/openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSVUtilsClassBase.test.tsx new file mode 100644 index 000000000000..4c8719f75fd6 --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSVUtilsClassBase.test.tsx @@ -0,0 +1,133 @@ +/* + * Copyright 2024 Collate. + * Licensed 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 React from 'react'; +import { EntityType } from '../../enums/entity.enum'; +import csvUtilsClassBase from './CSVUtilsClassBase'; + +jest.mock( + '../../components/common/AsyncSelectList/TreeAsyncSelectList', + () => ({ + __esModule: true, + default: jest.fn(), + }) +); + +jest.mock( + '../../components/common/DomainSelectableList/DomainSelectableList.component', + () => ({ + __esModule: true, + default: jest.fn(), + }) +); + +jest.mock('../../components/common/InlineEdit/InlineEdit.component', () => ({ + __esModule: true, + default: jest.fn(), +})); + +jest.mock('../../components/common/TierCard/TierCard', () => ({ + __esModule: true, + default: jest.fn(), +})); + +jest.mock( + '../../components/common/UserTeamSelectableList/UserTeamSelectableList.component', + () => ({ + __esModule: true, + UserTeamSelectableList: jest + .fn() + .mockReturnValue(

UserTeamSelectableList

), + }) +); + +jest.mock( + '../../components/Modals/ModalWithMarkdownEditor/ModalWithMarkdownEditor', + () => ({ + __esModule: true, + ModalWithMarkdownEditor: jest.fn(), + }) +); +jest.mock( + '../../components/Modals/ModalWithCustomProperty/ModalWithCustomPropertyEditor.component', + () => ({ + __esModule: true, + ModalWithCustomPropertyEditor: jest.fn(), + }) +); + +describe('CSV utils ClassBase', () => { + describe('getEditor', () => { + it('should return the editor component for the specified column', () => { + const column = 'owner'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + + it('should return undefined for unknown columns', () => { + const column = 'unknown'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeUndefined(); + }); + + it('should return the editor component for the "description" column', () => { + const column = 'description'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + + it('should return the editor component for the "tags" column', () => { + const column = 'tags'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + + it('should return the editor component for the "glossaryTerms" column', () => { + const column = 'glossaryTerms'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + + it('should return the editor component for the "tiers" column', () => { + const column = 'tiers'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + + it('should return the editor component for the "extension" column', () => { + const column = 'extension'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + + it('should return the editor component for the "reviewers" column', () => { + const column = 'reviewers'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + + it('should return the editor component for the "domain" column', () => { + const column = 'domain'; + const editor = csvUtilsClassBase.getEditor(column, EntityType.GLOSSARY); + + expect(editor).toBeDefined(); + }); + }); +});