Skip to content

Commit

Permalink
Refactor existing CSV converter
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4776

As part of the work to implement notifications in the system repo we have found some logic that will need to be duplicated in upcoming work.

This change lifts the convert to csv service into the lib.

It has been renamed to transform to csv,

The existing logic has been updated to be more precise in the transformation it is doing (transforms an array into a CSV row).
  • Loading branch information
jonathangoulding committed Feb 3, 2025
1 parent 882ce82 commit 5afc48d
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 185 deletions.
76 changes: 0 additions & 76 deletions app/services/jobs/export/convert-to-csv.service.js

This file was deleted.

6 changes: 3 additions & 3 deletions app/services/jobs/export/write-table-to-file.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { pipeline, Transform } = require('stream')
const path = require('path')
const util = require('util')

const ConvertToCSVService = require('./convert-to-csv.service.js')
const { TransformArrayToCSVRow } = require('../../../lib/transform-to-csv.lib.js')

/**
* Converts data into CSV format and writes it to a file
Expand All @@ -30,7 +30,7 @@ async function go(headers, rows, schemaFolderPath, tableName) {

const transformDataStream = _transformDataStream()

const convertedHeaders = ConvertToCSVService.go(headers)
const convertedHeaders = TransformArrayToCSVRow(headers)

writeToFileStream.write(convertedHeaders)

Expand All @@ -48,7 +48,7 @@ function _transformDataStream() {
return new Transform({
objectMode: true,
transform: function (row, _encoding, callback) {
const datRow = ConvertToCSVService.go(Object.values(row))
const datRow = TransformArrayToCSVRow(Object.values(row))

callback(null, datRow)
}
Expand Down
158 changes: 158 additions & 0 deletions test/lib/transform-to-csv.lib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = (exports.lab = Lab.script())
const { expect } = Code

// Thing under test
const { TransformArrayToCSVRow } = require('../../app/lib/transform-to-csv.lib.js')

describe('Transform to csv', () => {
describe('#TransformArrayToCSVRow', () => {
let testArray

beforeEach(() => {
testArray = _complexArray()
})

it('correctly transforms all data types to csv', () => {
const result = TransformArrayToCSVRow(testArray)

expect(result).to.equal(
'"20146cdc-9b40-4769-aa78-b51c17080d56",' +
'"4.1.1",' +
'9700,' +
'"Low loss tidal abstraction of water up to and ""including"" 25,002 megalitres, known as ML/yr a year where no model applies",' +
'2022-12-14T18:39:45.000Z,' +
'true,' +
',' +
',' +
'false,' +
',' +
'25002,' +
'"{""message"": ""a json object""}"' +
'\n'
)
})

describe('when the data type is', () => {
describe('an object', () => {
it('correctly formats the object to a string', () => {
const result = TransformArrayToCSVRow([{ message: 'a json object' }])

expect(result).to.equal('"{""message"": ""a json object""}"\n')
})
})

describe('a UUID', () => {
it('correctly formats the UUID to a string', () => {
const result = TransformArrayToCSVRow(['20146cdc-9b40-4769-aa78-b51c17080d56'])

expect(result).to.equal('"20146cdc-9b40-4769-aa78-b51c17080d56"\n')
})
})

describe('a boolean', () => {
it('correctly formats the boolean to a string', () => {
const result = TransformArrayToCSVRow([true])

expect(result).to.equal('true\n')
})
})

describe('a number', () => {
it('correctly formats the boolean to a string', () => {
const result = TransformArrayToCSVRow([100])

expect(result).to.equal('100\n')
})
})

describe('a string containing', () => {
describe('a comma', () => {
it('correctly formats the string', () => {
const result = TransformArrayToCSVRow(['I am a, comma seperated sentence.'])

expect(result).to.equal('"I am a, comma seperated sentence."\n')
})
})

describe('a single double quote', () => {
it('correctly formats the string', () => {
const result = TransformArrayToCSVRow(['I am a " double quote sentence.'])

expect(result).to.equal('"I am a "" double quote sentence."\n')
})
})

describe('a double double quote', () => {
it('correctly formats the string', () => {
const result = TransformArrayToCSVRow(['I am a "" double quote sentence.'])

expect(result).to.equal('"I am a """" double quote sentence."\n')
})
})

describe('a back slash', () => {
it('correctly formats the string', () => {
const result = TransformArrayToCSVRow(['I am a "\\" back slash sentence.'])

expect(result).to.equal('"I am a ""\\"" back slash sentence."\n')
})
})
})

describe('a date', () => {
it('correctly formats the date to an iso string', () => {
const result = TransformArrayToCSVRow([new Date('2021-02-01')])

expect(result).to.equal('2021-02-01T00:00:00.000Z\n')
})
})
})

describe('when an array of strings us provided', () => {
beforeEach(() => {
testArray = _arrayOfStrings()
})

it('converts the data to a CSV format', () => {
const result = TransformArrayToCSVRow(testArray)

expect(result).to.equal('"name","age"\n')
})
})

describe('when no array is provided', () => {
it('returns undefined', () => {
const result = TransformArrayToCSVRow()

expect(result).to.equal(undefined)
})
})
})
})

function _arrayOfStrings() {
return ['name', 'age']
}

function _complexArray() {
return [
'20146cdc-9b40-4769-aa78-b51c17080d56',
'4.1.1',
9700,
'Low loss tidal abstraction of water up to and "including" 25,002 megalitres, known as ML/yr a year where no model applies',
new Date(2022, 11, 14, 18, 39, 45),
true,
null,
undefined,
false,
'',
25002,
{ message: 'a json object' }
]
}
106 changes: 0 additions & 106 deletions test/services/jobs/export/convert-to-csv.service.test.js

This file was deleted.

0 comments on commit 5afc48d

Please sign in to comment.