Skip to content

Commit

Permalink
unit test around csv utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish8689 committed Sep 27, 2024
1 parent 7a4500b commit ce47405
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 0 deletions.
109 changes: 109 additions & 0 deletions openmetadata-ui/src/main/resources/ui/src/utils/CSV/CSV.utils.test.tsx
Original file line number Diff line number Diff line change
@@ -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"`
);
});
});
});
Original file line number Diff line number Diff line change
@@ -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(<p>UserTeamSelectableList</p>),
})
);

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();
});
});
});

0 comments on commit ce47405

Please sign in to comment.