From 283800dfa61742263044856633ade6e77d103e72 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Tue, 28 Jun 2022 10:17:22 +0300 Subject: [PATCH] fix(special-missings): fixed formats table sent as part of sasjs_tables --- src/file/generateTableUploadForm.ts | 5 +++-- src/utils/convertToCsv.spec.ts | 18 ++++++++++++++++-- src/utils/convertToCsv.ts | 12 +++++++++++- src/utils/formatDataForRequest.ts | 7 ++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/file/generateTableUploadForm.ts b/src/file/generateTableUploadForm.ts index f2e8b08..b37efed 100644 --- a/src/file/generateTableUploadForm.ts +++ b/src/file/generateTableUploadForm.ts @@ -1,5 +1,5 @@ import * as NodeFormData from 'form-data' -import { convertToCSV } from '../utils/convertToCsv' +import { convertToCSV, isFormatsTable } from '../utils/convertToCsv' import { splitChunks } from '../utils/splitChunks' export const generateTableUploadForm = ( @@ -13,7 +13,8 @@ export const generateTableUploadForm = ( for (const tableName in data) { tableCounter++ - sasjsTables.push(tableName) + // Formats table should not be sent as part of 'sasjs_tables' + if (!isFormatsTable(tableName)) sasjsTables.push(tableName) const csv = convertToCSV(data, tableName) diff --git a/src/utils/convertToCsv.spec.ts b/src/utils/convertToCsv.spec.ts index 7c5f927..ddd2c40 100644 --- a/src/utils/convertToCsv.spec.ts +++ b/src/utils/convertToCsv.spec.ts @@ -1,4 +1,4 @@ -import { convertToCSV } from './convertToCsv' +import { convertToCSV, isFormatsTable } from './convertToCsv' describe('convertToCsv', () => { const tableName = 'testTable' @@ -216,7 +216,9 @@ describe('convertToCsv', () => { const data = { [tableName]: [{ var1: 'string' }] } expect(() => convertToCSV(data, 'wrongTableName')).toThrow( - new Error('No table provided to be converted to CSV') + new Error( + 'Error while converting to CSV. No table provided to be converted to CSV.' + ) ) }) @@ -226,3 +228,15 @@ describe('convertToCsv', () => { expect(convertToCSV(data, tableName)).toEqual('') }) }) + +describe('isFormatsTable', () => { + const tableName = 'sometable' + + it('should return true if table name match pattern of formats table', () => { + expect(isFormatsTable(`$${tableName}`)).toEqual(true) + }) + + it('should return false if table name does not match pattern of formats table', () => { + expect(isFormatsTable(tableName)).toEqual(false) + }) +}) diff --git a/src/utils/convertToCsv.ts b/src/utils/convertToCsv.ts index 62cf613..e970185 100644 --- a/src/utils/convertToCsv.ts +++ b/src/utils/convertToCsv.ts @@ -1,4 +1,5 @@ import { isSpecialMissing } from '@sasjs/utils/input/validators' +import { prefixMessage } from '@sasjs/utils/error' /** * Converts the given JSON object array to a CSV string. @@ -9,7 +10,10 @@ export const convertToCSV = ( tableName: string ) => { if (!data[tableName]) { - throw new Error('No table provided to be converted to CSV') + throw prefixMessage( + 'No table provided to be converted to CSV.', + 'Error while converting to CSV. ' + ) } const table = data[tableName] @@ -170,6 +174,12 @@ export const convertToCSV = ( return finalCSV } +/** + * Checks if table is table of formats (table name should start from '$' character). + * @param tableName - table name. + */ +export const isFormatsTable = (tableName: string) => /^\$.*/.test(tableName) + const getByteSize = (str: string) => { let byteSize = str.length for (let i = str.length - 1; i >= 0; i--) { diff --git a/src/utils/formatDataForRequest.ts b/src/utils/formatDataForRequest.ts index bf73457..16b17e7 100644 --- a/src/utils/formatDataForRequest.ts +++ b/src/utils/formatDataForRequest.ts @@ -1,4 +1,4 @@ -import { convertToCSV } from './convertToCsv' +import { convertToCSV, isFormatsTable } from './convertToCsv' import { splitChunks } from './splitChunks' export const formatDataForRequest = (data: any) => { @@ -8,7 +8,7 @@ export const formatDataForRequest = (data: any) => { for (const tableName in data) { if ( - tableName.match(/^\$.*/) && + isFormatsTable(tableName) && Object.keys(data).includes(tableName.replace(/^\$/, '')) ) { continue @@ -16,7 +16,8 @@ export const formatDataForRequest = (data: any) => { tableCounter++ - sasjsTables.push(tableName) + // Formats table should not be sent as part of 'sasjs_tables' + if (!isFormatsTable(tableName)) sasjsTables.push(tableName) const csv = convertToCSV(data, tableName)