|
|
@@ -14,17 +14,64 @@
|
|
|
// See the License for the specific language governing permissions and
|
|
|
// limitations under the License.
|
|
|
|
|
|
-import { ColumnProps } from 'cuix/dist/components/Table';
|
|
|
import { convertToAntdColumns, convertToDataSource, getDefaultTableName } from './utils';
|
|
|
-import { ImporterFileSource, GuessFieldTypesColumn, ImporterTableData } from '../types';
|
|
|
+import { ImporterFileSource, FilePreviewTableColumn, FilePreviewTableData } from '../types';
|
|
|
|
|
|
describe('convertToAntdColumns', () => {
|
|
|
- it('should return an empty array when no input is provided', () => {
|
|
|
- expect(convertToAntdColumns()).toEqual([]);
|
|
|
+ it('should correctly convert FilePreviewTableColumn[] to ColumnProps[]', () => {
|
|
|
+ const input: FilePreviewTableColumn[] = [{ name: 'name' }, { name: 'age' }];
|
|
|
+ const expectedOutput = [
|
|
|
+ { title: 'name', dataIndex: 'name', key: 'name', width: '100px' },
|
|
|
+ { title: 'age', dataIndex: 'age', key: 'age', width: '100px' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ expect(convertToAntdColumns(input)).toEqual(expectedOutput);
|
|
|
});
|
|
|
|
|
|
- it('should correctly convert GuessFieldTypesColumn[] to ColumnProps[]', () => {
|
|
|
- const input: GuessFieldTypesColumn[] = [{ name: 'name' }, { name: 'age' }];
|
|
|
+ it('should handle single column input', () => {
|
|
|
+ const input: FilePreviewTableColumn[] = [{ name: 'singleColumn' }];
|
|
|
+ const expectedOutput = [
|
|
|
+ { title: 'singleColumn', dataIndex: 'singleColumn', key: 'singleColumn', width: '100px' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ expect(convertToAntdColumns(input)).toEqual(expectedOutput);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle columns with special characters', () => {
|
|
|
+ const input: FilePreviewTableColumn[] = [
|
|
|
+ { name: 'first-name' },
|
|
|
+ { name: 'email_address' },
|
|
|
+ { name: 'phone number' }
|
|
|
+ ];
|
|
|
+ const expectedOutput = [
|
|
|
+ { title: 'first-name', dataIndex: 'firstName', key: 'first-name', width: '100px' },
|
|
|
+ { title: 'email_address', dataIndex: 'emailAddress', key: 'email_address', width: '100px' },
|
|
|
+ { title: 'phone number', dataIndex: 'phoneNumber', key: 'phone number', width: '100px' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ expect(convertToAntdColumns(input)).toEqual(expectedOutput);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle columns with numbers and special characters', () => {
|
|
|
+ const input: FilePreviewTableColumn[] = [
|
|
|
+ { name: 'column1' },
|
|
|
+ { name: '2nd_column' },
|
|
|
+ { name: 'column-3' }
|
|
|
+ ];
|
|
|
+ const expectedOutput = [
|
|
|
+ { title: 'column1', dataIndex: 'column1', key: 'column1', width: '100px' },
|
|
|
+ { title: '2nd_column', dataIndex: '2NdColumn', key: '2nd_column', width: '100px' },
|
|
|
+ { title: 'column-3', dataIndex: 'column3', key: 'column-3', width: '100px' }
|
|
|
+ ];
|
|
|
+
|
|
|
+ expect(convertToAntdColumns(input)).toEqual(expectedOutput);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle columns with type property', () => {
|
|
|
+ const input: FilePreviewTableColumn[] = [
|
|
|
+ { name: 'name', type: 'string' },
|
|
|
+ { name: 'age', type: 'number' }
|
|
|
+ ];
|
|
|
const expectedOutput = [
|
|
|
{ title: 'name', dataIndex: 'name', key: 'name', width: '100px' },
|
|
|
{ title: 'age', dataIndex: 'age', key: 'age', width: '100px' }
|
|
|
@@ -35,70 +82,210 @@ describe('convertToAntdColumns', () => {
|
|
|
});
|
|
|
|
|
|
describe('convertToDataSource', () => {
|
|
|
- const columns: ColumnProps<ImporterTableData>[] = [
|
|
|
- { title: 'Name', dataIndex: 'name', key: 'name', width: '100px' },
|
|
|
- { title: 'Age', dataIndex: 'age', key: 'age', width: '100px' }
|
|
|
- ];
|
|
|
-
|
|
|
it('should return an empty array when no apiResponse is provided', () => {
|
|
|
- expect(convertToDataSource(columns)).toEqual([]);
|
|
|
+ expect(convertToDataSource({})).toEqual([]);
|
|
|
});
|
|
|
|
|
|
- it('should correctly convert apiResponse to GuessFieldTypesColumn[]', () => {
|
|
|
- const apiResponse: string[][] = [
|
|
|
- ['Alice', '30'],
|
|
|
- ['Bob', '25']
|
|
|
+ it('should correctly convert apiResponse to FilePreviewTableColumn[]', () => {
|
|
|
+ const apiResponse: FilePreviewTableData = {
|
|
|
+ name: ['Alice', 'Bob'],
|
|
|
+ age: ['30', '25']
|
|
|
+ };
|
|
|
+
|
|
|
+ const expectedOutput = [
|
|
|
+ { importerDataKey: 'importer-row__0', name: 'Alice', age: '30' },
|
|
|
+ { importerDataKey: 'importer-row__1', name: 'Bob', age: '25' }
|
|
|
];
|
|
|
|
|
|
+ expect(convertToDataSource(apiResponse)).toEqual(expectedOutput);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle uneven array lengths with null values', () => {
|
|
|
+ const apiResponse: FilePreviewTableData = {
|
|
|
+ name: ['Alice', 'Bob', 'Charlie'],
|
|
|
+ age: ['30', '25'],
|
|
|
+ city: ['New York']
|
|
|
+ };
|
|
|
+
|
|
|
const expectedOutput = [
|
|
|
- { importerDataKey: 'Alice__0', name: 'Alice', age: '30' },
|
|
|
- { importerDataKey: 'Bob__1', name: 'Bob', age: '25' }
|
|
|
+ { importerDataKey: 'importer-row__0', name: 'Alice', age: '30', city: 'New York' },
|
|
|
+ { importerDataKey: 'importer-row__1', name: 'Bob', age: '25', city: null },
|
|
|
+ { importerDataKey: 'importer-row__2', name: 'Charlie', age: null, city: null }
|
|
|
];
|
|
|
|
|
|
- expect(convertToDataSource(columns, apiResponse)).toEqual(expectedOutput);
|
|
|
+ expect(convertToDataSource(apiResponse)).toEqual(expectedOutput);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle empty arrays in apiResponse', () => {
|
|
|
+ const apiResponse: FilePreviewTableData = {
|
|
|
+ name: [],
|
|
|
+ age: []
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(convertToDataSource(apiResponse)).toEqual([]);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('getDefaultTableName', () => {
|
|
|
- it('should extract file name from LOCALFILE path using pattern', () => {
|
|
|
- const filePath = '/user/data/:myDocument;v1.csv';
|
|
|
- const result = getDefaultTableName(filePath, ImporterFileSource.LOCAL);
|
|
|
+ it('should extract file name from LOCAL file metadata with fileName provided', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/myDocument.csv',
|
|
|
+ fileName: 'myDocument.csv',
|
|
|
+ source: ImporterFileSource.LOCAL
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
expect(result).toBe('myDocument');
|
|
|
});
|
|
|
|
|
|
- it('should return empty string if LOCALFILE pattern does not match', () => {
|
|
|
- const filePath = '/user/data/myDocument.csv';
|
|
|
- const result = getDefaultTableName(filePath, ImporterFileSource.LOCAL);
|
|
|
+ it('should return empty string if LOCAL file has no fileName property', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/myDocument.csv',
|
|
|
+ source: ImporterFileSource.LOCAL
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
expect(result).toBe('');
|
|
|
});
|
|
|
|
|
|
+ it('should handle LOCAL file with complex filename', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/complex-file_name.with.dots.xlsx',
|
|
|
+ fileName: 'complex-file_name.with.dots.xlsx',
|
|
|
+ source: ImporterFileSource.LOCAL
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('complex_file_name_with_dots');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle LOCAL file with special characters in filename', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/file@2023#test.csv',
|
|
|
+ fileName: 'file@2023#test.csv',
|
|
|
+ source: ImporterFileSource.LOCAL
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('file_2023_test');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle LOCAL file with spaces in filename', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/my file name.csv',
|
|
|
+ fileName: 'my file name.csv',
|
|
|
+ source: ImporterFileSource.LOCAL
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('my_file_name');
|
|
|
+ });
|
|
|
+
|
|
|
it('should extract file name from REMOTE path as last part of path', () => {
|
|
|
- const filePath = 'https://demo.gethue.com/hue/test-file.csv';
|
|
|
- const result = getDefaultTableName(filePath, ImporterFileSource.REMOTE);
|
|
|
- expect(result).toBe('test-file');
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/test-file.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('test_file');
|
|
|
});
|
|
|
|
|
|
it('should handle file names with multiple dots correctly', () => {
|
|
|
- const filePath = 'https://demo.gethue.com/hue/test.file.name.csv';
|
|
|
- const result = getDefaultTableName(filePath, ImporterFileSource.REMOTE);
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/test.file.name.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
expect(result).toBe('test_file_name');
|
|
|
});
|
|
|
|
|
|
it('should handle file names with no extension correctly', () => {
|
|
|
- const filePath = 'https://demo.gethue.com/hue/test-file';
|
|
|
- const result = getDefaultTableName(filePath, ImporterFileSource.REMOTE);
|
|
|
- expect(result).toBe('test-file');
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/test-file',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('test_file');
|
|
|
});
|
|
|
|
|
|
it('should handle file names with special characters correctly', () => {
|
|
|
- const filePath = 'https://demo.gethue.com/hue/test-file@2023.csv';
|
|
|
- const result = getDefaultTableName(filePath, ImporterFileSource.REMOTE);
|
|
|
- expect(result).toBe('test-file@2023');
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/test-file@2023.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('test_file_2023');
|
|
|
});
|
|
|
|
|
|
it('should handle file names with spaces correctly', () => {
|
|
|
- const filePath = 'https://demo.gethue.com/hue/test file.csv';
|
|
|
- const result = getDefaultTableName(filePath, ImporterFileSource.REMOTE);
|
|
|
- expect(result).toBe('test file');
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/test file.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('test_file');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle REMOTE files with only extension', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle REMOTE files with multiple extensions', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/file.backup.tar.gz',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('file_backup_tar');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle REMOTE files with numbers and underscores', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/data_file_2023_v1.xlsx',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('data_file_2023_v1');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle S3 URLs', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: 's3://bucket-name/folder/data-file.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('data_file');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle Azure blob URLs', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: 'abfs://container/data.file.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('data_file');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should return empty string for empty file paths', () => {
|
|
|
+ const localFileMetaData = {
|
|
|
+ path: '',
|
|
|
+ fileName: '',
|
|
|
+ source: ImporterFileSource.LOCAL
|
|
|
+ };
|
|
|
+ const remoteFileMetaData = {
|
|
|
+ path: '',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ expect(getDefaultTableName(localFileMetaData)).toBe('');
|
|
|
+ expect(getDefaultTableName(remoteFileMetaData)).toBe('');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should handle single character file names', () => {
|
|
|
+ const fileMetaData = {
|
|
|
+ path: '/user/data/a.csv',
|
|
|
+ source: ImporterFileSource.REMOTE
|
|
|
+ };
|
|
|
+ const result = getDefaultTableName(fileMetaData);
|
|
|
+ expect(result).toBe('a');
|
|
|
});
|
|
|
});
|