Skip to content

Commit

Permalink
writ eoptions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Oct 31, 2016
1 parent 79eba7b commit e43ac2e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
14 changes: 7 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1548,22 +1548,22 @@ var writers = {}
* @param {Number} [options.indent] Used for JSON and YAML formats. Specifies indent level. Default for YAML is `2`, `0` for JSON.
* @param {String} [options.writeMethod='safeDump'] Used for YAML formats. Can also be `"dump"` to allow writing of RegExes and functions. The `options` object will also pass anything onto `js-yaml`. See its docs for other options. Example shown below with `sortKeys`. https://github.com/nodeca/js-yaml#safedump-object---options-
* @param {Array} [options.columns] Used for tabular formats. Optionally specify a list of column names to use. Otherwise they are detected from the data. See `d3-dsv` for more detail: https://github.com/d3/d3-dsv/blob/master/README.md#dsv_format
* @param {Function} callback callback of `(err, data)` where `err` is any error and `data` is the data that was written out
* @param {Function} callback callback of `(err, dataString)` where `err` is any error and `dataString` is the data that was written out as a string
*
* @example
* io.writeData('path/to/data.json', jsonData, function (err) {
* io.writeData('path/to/data.json', jsonData, function (err, dataString) {
* console.log(err)
* })
*
* io.writeData('path/to/create/to/data.csv', flatJsonData, {makeDirectories: true}, function (err) {
* io.writeData('path/to/create/to/data.csv', flatJsonData, {makeDirectories: true}, function (err, dataString) {
* console.log(err)
* })
*
* io.writeData('path/to/to/data.yaml', jsonData, {writeMehod: "dump", sortKeys: true}, function (err) {
* io.writeData('path/to/to/data.yaml', jsonData, {writeMehod: "dump", sortKeys: true}, function (err, dataString) {
* console.log(err)
* })
*
* io.writeData('path/to/to/data.json', jsonData, {indent: 4}, function (err) {
* io.writeData('path/to/to/data.json', jsonData, {indent: 4}, function (err, dataString) {
* console.log(err)
* })
*
Expand All @@ -1576,14 +1576,14 @@ var writers = {}
* }
* return value
* }
* }, function (err) {
* }, function (err, dataString) {
* console.log(err)
* })
*
* io.writeData('path/to/to/data.json', jsonData, {
* indent: 4,
* replacer: ['name', 'occupation'] // Only keep "name" and "occupation" values
* }, function (err) {
* }, function (err, dataString) {
* console.log(err)
* })
*/
Expand Down
92 changes: 78 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ function testDataPath (name) {
return path.join(__dirname, 'data', name)
}

function readAssertBasicValid (path) {
function readAssertBasicValid (path, columns) {
var strFormats = ['json', 'geojson', 'topojson', 'yml', 'yaml']
var strings = strFormats.indexOf(io.discernFormat(path)) === -1
var json = io.readDataSync(path)
assertBasicValid(json, strings)
assertBasicValid(json, strings, columns)
}

function readAssertBasicValidObject (path, row) {
Expand All @@ -32,13 +32,15 @@ function readAssertBasicValidObject (path, row) {
assertBasicValidObject(json, strings, row)
}

function assertBasicValid (json, strings) {
function assertBasicValid (json, strings, columns) {
var values = strings ? ['70', '63'] : [70, 63]

columns = columns || ['name', 'occupation', 'height']

assert.lengthOf(json, 2)
assert.typeOf(json[0], 'object')
assert(_.isEqual(_.keys(json[0]), ['name', 'occupation', 'height']), 'headers match keys')
assert(_.isEqual(_.keys(json[1]), ['name', 'occupation', 'height']), 'headers match keys')
assert(_.isEqual(_.keys(json[0]), columns), 'headers match keys')
assert(_.isEqual(_.keys(json[1]), columns), 'headers match keys')
assert(_.isEqual(_.values(json[0]), ['jim', 'land surveyor', values[0]]), 'data values match values')
assert(_.isEqual(_.values(json[1]), ['francis', 'conductor', values[1]]), 'data values match values')
}
Expand Down Expand Up @@ -2499,7 +2501,7 @@ describe('writers', function () {
})

it('should write with json replacer fn', function (done) {
var filePath = ['test', 'tmp-write-data-json-replacer', 'data.json']
var filePath = ['test', 'tmp-write-data-json-replacer-fn', 'data.json']
io.writeData(filePath.join(path.sep), testData, {
makeDirectories: true,
replacer: function (key, value) {
Expand All @@ -2521,14 +2523,14 @@ describe('writers', function () {
})

it('should write with json replacer array', function (done) {
var filePath = ['test', 'tmp-write-data-json-replacer', 'data.json']
var filePath = ['test', 'tmp-write-data-json-replacer-array', 'data.json']
io.writeData(filePath.join(path.sep), testData, {
makeDirectories: true,
replacer: ['height']
}, function (err, json) {
}, function (err, dataString) {
assert.equal(err, null)
assert.equal(json, '[{"height":70},{"height":63}]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), json)
assert.equal(dataString, '[{"height":70},{"height":63}]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
Expand All @@ -2537,14 +2539,14 @@ describe('writers', function () {
})

it('should write with json indent', function (done) {
var filePath = ['test', 'tmp-write-data-json-replacer', 'data.json']
var filePath = ['test', 'tmp-write-data-json-indent', 'data.json']
io.writeData(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 2
}, function (err, json) {
}, function (err, dataString) {
assert.equal(err, null)
assert.equal(json, '[\n {\n "name": "jim",\n "occupation": "land surveyor",\n "height": 70\n },\n {\n "name": "francis",\n "occupation": "conductor",\n "height": 63\n }\n]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), json)
assert.equal(dataString, '[\n {\n "name": "jim",\n "occupation": "land surveyor",\n "height": 70\n },\n {\n "name": "francis",\n "occupation": "conductor",\n "height": 63\n }\n]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
Expand Down Expand Up @@ -2595,6 +2597,42 @@ describe('writers', function () {
})
})

describe('geojson', function () {
it('should write as geojson with indent', function (done) {
var filePath = ['test', 'tmp-write-data-geojson-indent', 'data.geojson']
io.writeData(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 2
}, function (err, dataString) {
assert.equal(err, null)
assert.equal(dataString, '[\n {\n "name": "jim",\n "occupation": "land surveyor",\n "height": 70\n },\n {\n "name": "francis",\n "occupation": "conductor",\n "height": 63\n }\n]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})
})

describe('topojson', function () {
it('should write as topojson with indent', function (done) {
var filePath = ['test', 'tmp-write-data-topojson-indent', 'data.topojson']
io.writeData(filePath.join(path.sep), testData, {
makeDirectories: true,
indent: 2
}, function (err, dataString) {
assert.equal(err, null)
assert.equal(dataString, '[\n {\n "name": "jim",\n "occupation": "land surveyor",\n "height": 70\n },\n {\n "name": "francis",\n "occupation": "conductor",\n "height": 63\n }\n]')
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), dataString)
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})
})

describe('csv', function () {
it('should write as csv', function (done) {
var filePath = ['test', 'tmp-write-data-csv', 'data.csv']
Expand Down Expand Up @@ -2649,6 +2687,19 @@ describe('writers', function () {
})
})
})

it('should write as yaml with indent', function (done) {
var filePath = ['test', 'tmp-write-data-yaml-indent', 'data.yaml']
io.writeData(filePath.join(path.sep), testData, {makeDirectories: true, indent: 4}, function (err, dataString) {
assert.equal(err, null)
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
assert.equal(dataString, '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})
})

describe('yml', function () {
Expand All @@ -2663,6 +2714,19 @@ describe('writers', function () {
})
})
})

it('should write as yml with indent', function (done) {
var filePath = ['test', 'tmp-write-data-yml-indent', 'data.yml']
io.writeData(filePath.join(path.sep), testData, {makeDirectories: true, indent: 4}, function (err, dataString) {
assert.equal(err, null)
assert.equal(fs.readFileSync(filePath.join(path.sep), 'utf-8'), '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
assert.equal(dataString, '- \n name: jim\n occupation: land surveyor\n height: 70\n- \n name: francis\n occupation: conductor\n height: 63\n')
rimraf(filePath.slice(0, 2).join(path.sep), {glob: false}, function (err) {
assert.equal(err, null)
done()
})
})
})
})
})

Expand Down

0 comments on commit e43ac2e

Please sign in to comment.