From be3134b1ac458d9e8ceeb0d85b80abb40baefd57 Mon Sep 17 00:00:00 2001 From: Joshen Lim Date: Thu, 28 Apr 2022 11:16:59 +0700 Subject: [PATCH] Resolve https://sentry.io/organizations/supabase/issues/2956926689conflicts --- .../SpreadsheetImport.utils.ts | 8 ++-- .../Editor/SpreadsheetImport.utils.test.js | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 studio/tests/components/Editor/SpreadsheetImport.utils.test.js diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/SpreadsheetImport/SpreadsheetImport.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/SpreadsheetImport/SpreadsheetImport.utils.ts index d914ac247a4..4ba1a2c91b3 100644 --- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/SpreadsheetImport/SpreadsheetImport.utils.ts +++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/SpreadsheetImport/SpreadsheetImport.utils.ts @@ -93,7 +93,7 @@ export const revertSpreadsheet = (headers: string[], rows: any[]) => { return Papa.unparse(rows, { columns: headers }) } -const inferColumnType = (column: string, rows: object[]) => { +export const inferColumnType = (column: string, rows: object[]) => { // General strategy is to check the first row first, before checking across all the rows // to ensure uniformity in data type. Thinking we do this as an optimization instead of // checking all the rows up front. @@ -105,7 +105,7 @@ const inferColumnType = (column: string, rows: object[]) => { const columnDataAcrossRows = rows.map((row: object) => (row as any)[column]) // Unable to infer any type as there's no data, default to text - if (!columnData) { + if (columnData === undefined || columnData === null) { return 'text' } @@ -121,9 +121,9 @@ const inferColumnType = (column: string, rows: object[]) => { } // Infer boolean type - if (includes(['true', 'false'], columnData.toLowerCase())) { + if (includes(['true', 'false'], columnData.toString().toLowerCase())) { const isAllBoolean = columnDataAcrossRows.every((item: any) => - includes(['true', 'false'], item.toLowerCase()) + includes(['true', 'false'], item.toString().toLowerCase()) ) if (isAllBoolean) { return 'boolean' diff --git a/studio/tests/components/Editor/SpreadsheetImport.utils.test.js b/studio/tests/components/Editor/SpreadsheetImport.utils.test.js new file mode 100644 index 00000000000..0a86c93411f --- /dev/null +++ b/studio/tests/components/Editor/SpreadsheetImport.utils.test.js @@ -0,0 +1,43 @@ +import { inferColumnType } from 'components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/SpreadsheetImport/SpreadsheetImport.utils' + +describe('SpreadsheedImport.utils: inferColumnType', () => { + test('should default column type to text if no rows to infer from', () => { + const mockData = [] + const type = inferColumnType('id', mockData) + expect(type).toBe('text') + }) + test('should default column type to text if the first row has no data to infer from', () => { + const mockData = [{ name: 'bob', age: '42' }] + const type = inferColumnType('id', mockData) + expect(type).toBe('text') + }) + test('should default column type to text if the first row data value is null', () => { + const mockData = [{ id: null, name: 'bob', age: '42' }] + const type = inferColumnType('id', mockData) + expect(type).toBe('text') + }) + test('should infer integer types correctly', () => { + const mockData = [{ name: 'bob', age: '42' }] + const type = inferColumnType('age', mockData) + expect(type).toBe('int8') + }) + test('should infer float types correctly', () => { + const mockData = [{ name: 'bob', height: '161.72' }] + const type = inferColumnType('height', mockData) + expect(type).toBe('float8') + }) + test('should infer boolean types correctly', () => { + const mockData1 = [{ name: 'bob', height: '161.72', isWorking: 'true' }] + const type1 = inferColumnType('isWorking', mockData1) + expect(type1).toBe('boolean') + + const mockData2 = [{ name: 'bob', height: '161.72', isRetired: 'false' }] + const type2 = inferColumnType('isRetired', mockData2) + expect(type2).toBe('boolean') + }) + test('should infer objects as jsonb types correctly', () => { + const mockData = [{ name: 'bob', metadata: '{}' }] + const type = inferColumnType('metadata', mockData) + expect(type).toBe('jsonb') + }) +})