Improve testing on resultsToCSV

pull/10616/head
deniz kusefoglu 2017-09-12 15:49:23 -07:00
parent c94a69366b
commit e7b25a2bed
2 changed files with 18 additions and 8 deletions

View File

@ -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)
})
})

View File

@ -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')