-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
882ce82
commit 5afc48d
Showing
4 changed files
with
161 additions
and
185 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
test/services/jobs/export/convert-to-csv.service.test.js
This file was deleted.
Oops, something went wrong.