From e7b25a2bedec003a8652fa5bc6762eeaf51c0742 Mon Sep 17 00:00:00 2001 From: deniz kusefoglu Date: Tue, 12 Sep 2017 15:49:23 -0700 Subject: [PATCH] Improve testing on resultsToCSV --- ui/spec/shared/parsing/resultsToCSVSpec.js | 19 ++++++++++++++----- ui/src/shared/parsing/resultsToCSV.js | 7 ++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ui/spec/shared/parsing/resultsToCSVSpec.js b/ui/spec/shared/parsing/resultsToCSVSpec.js index 508696af81..1963c7cf4a 100644 --- a/ui/spec/shared/parsing/resultsToCSVSpec.js +++ b/ui/spec/shared/parsing/resultsToCSVSpec.js @@ -1,4 +1,4 @@ -import resultsToCSV from 'shared/parsing/resultsToCSV' +import resultsToCSV, {formatDate} from 'shared/parsing/resultsToCSV' describe('resultsToCSV', () => { it('parses results to an object with name and CSVString keys', () => { @@ -8,18 +8,27 @@ describe('resultsToCSV', () => { { name: 'some_name', columns: ['col1', 'col2', 'col3', 'col4'], - values: [[1, 2, 3, 4], [(5, 6, 7, 8)]], + values: [[1000000000, '2', 3, 4], [2000000000, '6', 7, 8]], }, ], }, ] const response = resultsToCSV(results) + const expected = `date,col2,col3,col4\n${formatDate( + 1000000000 + )},2,3,4\n${formatDate(2000000000)},6,7,8` expect(response).to.have.all.keys('name', 'CSVString') expect(response.name).to.be.a('string') - expect('foobar').to.not.include('/') expect(response.CSVString).to.be.a('string') + expect(response.CSVString).to.equal(expected) }) }) -// make sure name does not contain things that would not be allowed in a filename. -// handle edge cases for columns and values. ? +describe('formatDate', () => { + it('converts timestamp to an excel compatible date string', () => { + const timestamp = 1000000000000 + const result = formatDate(timestamp) + expect(result).to.be.a('string') + expect(+new Date(result)).to.equal(timestamp) + }) +}) diff --git a/ui/src/shared/parsing/resultsToCSV.js b/ui/src/shared/parsing/resultsToCSV.js index 77edac44f1..47596b16ec 100644 --- a/ui/src/shared/parsing/resultsToCSV.js +++ b/ui/src/shared/parsing/resultsToCSV.js @@ -1,6 +1,9 @@ import _ from 'lodash' import moment from 'moment' +export const formatDate = timestamp => + moment(timestamp).format('M/D/YYYY h:mm:ss A') + const resultsToCSV = results => { const {name, columns, values} = _.get(results, ['0', 'series', '0'], {}) const [, ...cols] = columns @@ -9,9 +12,7 @@ const resultsToCSV = results => { .concat( values.map(([timestamp, ...measurements]) => // MS Excel format - [moment(timestamp).format('M/D/YYYY h:mm:ss A'), ...measurements].join( - ',' - ) + [formatDate(timestamp), ...measurements].join(',') ) ) .join('\n')