Skip to content

Commit a1db974

Browse files
committed
Merge pull request #20 from mrodrig/refactor-tests
Refactoring Tests
2 parents 2f06924 + 1e2e3e9 commit a1db974

32 files changed

+2482
-2616
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ $ npm test
137137

138138
_Note_: This requires `mocha`, `should`, `async`, and `underscore`.
139139

140+
To see test coverage, please run:
141+
```bash
142+
$ npm run coverage
143+
```
144+
145+
Current Coverage is:
146+
```
147+
Statements : 93.63% ( 147/157 )
148+
Branches : 87.91% ( 80/91 )
149+
Functions : 100% ( 36/36 )
150+
Lines : 95.86% ( 139/145 )
151+
```
152+
140153
## Features
141154

142155
- Header Generation (per document keys)

lib/constants.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"Errors" : {
3+
4+
"delimitersMustDiffer": "The field and array delimiters must differ.",
5+
"callbackRequired": "A callback is required!",
6+
"optionsRequired": "Options were not passed and are required.",
7+
8+
"json2csv": {
9+
"cannotCallJson2CsvOn": "Cannot call json2csv on ",
10+
"dataNotArrayOfDocuments": "Data provided was not an array of documents.",
11+
"notSameSchema": "Not all documents have the same schema."
12+
},
13+
14+
"csv2json": {
15+
"cannotCallCsv2JsonOn": "Cannot call csv2json on ",
16+
"csvNotString": "CSV is not a string.",
17+
"noDataRetrieveHeading": "No data provided to retrieve heading."
18+
}
19+
20+
},
21+
22+
"DefaultOptions" : {
23+
"DELIMITER" : {
24+
"FIELD" : ",",
25+
"ARRAY" : ";",
26+
"WRAP" : ""
27+
},
28+
"EOL" : "\n",
29+
"PARSE_CSV_NUMBERS" : false,
30+
"KEYS" : null
31+
}
32+
}

lib/converter.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22

33
var json2Csv = require('./json-2-csv'), // Require our json-2-csv code
44
csv2Json = require('./csv-2-json'), // Require our csv-2-json code
5+
constants = require('./constants'), // Require in constants
56
_ = require('underscore'); // Require underscore
67

78
/**
89
* Default options
910
*/
10-
var defaultOptions = {
11-
DELIMITER : {
12-
FIELD : ',',
13-
ARRAY : ';',
14-
WRAP : ''
15-
},
16-
EOL : '\n',
17-
PARSE_CSV_NUMBERS : false,
18-
KEYS : null
19-
};
11+
var defaultOptions = constants.DefaultOptions;
2012

2113
/**
2214
* Build the options to be passed to the appropriate function
@@ -28,9 +20,9 @@ var buildOptions = function (opts, cb) {
2820
// Note: _.defaults does a shallow default, we need to deep copy the DELIMITER object
2921
opts.DELIMITER = _.defaults(opts.DELIMITER || {}, defaultOptions.DELIMITER);
3022
// If the delimiter fields are the same, report an error to the caller
31-
if (opts.DELIMITER.FIELD === opts.DELIMITER.ARRAY) { return cb(new Error('The field and array delimiters must differ.')); }
23+
if (opts.DELIMITER.FIELD === opts.DELIMITER.ARRAY) { return cb(new Error(constants.Errors.delimitersMustDiffer)); }
3224
// Otherwise, send the options back
33-
else { return cb(null, opts); }
25+
return cb(null, opts);
3426
};
3527

3628
// Export the following functions that will be client accessible

lib/csv-2-json.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

3-
var _ = require('underscore');
3+
var _ = require('underscore'),
4+
constants = require('./constants');
45

56
var options = {}; // Initialize the options - this will be populated when the csv2json function is called.
67

78
// Generate the JSON heading from the CSV
89
var retrieveHeading = function (lines, callback) {
910
if (!lines.length) { // If there are no lines passed in, then throw an error
10-
return callback(new Error("No data provided to retrieve heading.")); // Pass an error back to the user
11+
return callback(new Error(constants.Errors.csv2json.noDataRetrieveHeading)); // Pass an error back to the user
1112
}
1213
var heading = lines[0].split(options.DELIMITER.FIELD); // Grab the top line (header line) and split by the field delimiter
1314
heading = _.map(heading, function (headerKey, index) {
@@ -97,12 +98,12 @@ module.exports = {
9798
// Function to export internally
9899
// Takes options as a document, data as a CSV string, and a callback that will be used to report the results
99100
csv2json: function (opts, data, callback) {
100-
if (!callback) { throw new Error('A callback is required!'); } // If a callback wasn't provided, throw an error
101-
if (!opts) { callback(new Error('Options were not passed and are required.')); return null; } // Shouldn't happen, but just in case
101+
if (!callback) { throw new Error(constants.Errors.callbackRequired); } // If a callback wasn't provided, throw an error
102+
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); return null; } // Shouldn't happen, but just in case
102103
else { options = opts; } // Options were passed, set the global options value
103-
if (!data) { callback(new Error('Cannot call csv2json on ' + data + '.')); return null; } // If we don't receive data, report an error
104+
if (!data) { return callback(new Error(constants.Errors.csv2json.cannotCallCsv2JsonOn + data + '.')); return null; } // If we don't receive data, report an error
104105
if (!_.isString(data)) { // The data is not a string
105-
callback(new Error("CSV is not a string.")); // Report an error back to the caller
106+
return callback(new Error(constants.Errors.csv2json.csvNotString)); // Report an error back to the caller
106107
}
107108
var lines = data.split(options.EOL); // Split the CSV into lines using the specified EOL option
108109
var json = convertCSV(lines, callback); // Retrieve the JSON document array

lib/json-2-csv.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var _ = require('underscore'),
4+
constants = require('./constants'),
45
Promise = require('bluebird');
56

67
var options = {}; // Initialize the options - this will be populated when the json2csv function is called.
@@ -28,7 +29,7 @@ var generateHeading = function(data) {
2829
// If there is a difference between the schemas, throw the inconsistent schema error
2930
var diff = _.difference(firstDocSchema, _.flatten(keyList));
3031
if (!_.isEqual(diff, [])) {
31-
return reject(new Error('Not all documents have the same schema.'));
32+
return reject(new Error(constants.Errors.json2csv.notSameSchema));
3233
}
3334
});
3435
return resolve(_.flatten(keys[0]));
@@ -98,15 +99,15 @@ module.exports = {
9899
// Function to export internally
99100
// Takes options as a document, data as a JSON document array, and a callback that will be used to report the results
100101
json2csv: function (opts, data, callback) {
101-
if (!callback) { throw new Error('A callback is required!'); } // If a callback wasn't provided, throw an error
102+
if (!callback) { throw new Error(constants.Errors.callbackRequired); } // If a callback wasn't provided, throw an error
102103

103-
if (!opts) { return callback(new Error('Options were not passed and are required.')); } // Shouldn't happen, but just in case
104+
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); } // Shouldn't happen, but just in case
104105
else { options = opts; } // Options were passed, set the global options value
105106

106-
if (!data) { return callback(new Error('Cannot call json2csv on ' + data + '.')); } // If we don't receive data, report an error
107+
if (!data) { return callback(new Error(constants.Errors.json2csv.cannotCallJson2CsvOn + data + '.')); } // If we don't receive data, report an error
107108

108109
if (!_.isObject(data)) { // If the data was not a single document or an array of documents
109-
return callback(new Error('Data provided was not an array of documents.')); // Report the error back to the caller
110+
return callback(new Error(constants.Errors.json2csv.dataNotArrayOfDocuments)); // Report the error back to the caller
110111
} else if (_.isObject(data) && !data.length) { // Single document, not an array
111112
data = [data]; // Convert to an array of the given document
112113
}
@@ -128,4 +129,4 @@ module.exports = {
128129
});
129130
}
130131

131-
};
132+
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"main": "./lib/converter.js",
1111
"scripts": {
12-
"test": "./node_modules/.bin/mocha --reporter spec"
12+
"test": "./node_modules/.bin/mocha test/tests.js",
13+
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec"
1314
},
1415
"keywords": [
1516
"json",
@@ -28,6 +29,7 @@
2829
},
2930
"devDependencies": {
3031
"mocha": "~2.2.4",
32+
"istanbul": "~0.3.13",
3133
"should": "~5.2.0",
3234
"async": "~0.9.0"
3335
},

test/CSV/arrayValueDocs.csv

Lines changed: 0 additions & 4 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)