Skip to content

Commit d159286

Browse files
committed
updated tests to ensure its working as expected
1 parent 598f8b1 commit d159286

File tree

7 files changed

+93
-18
lines changed

7 files changed

+93
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ $ npm run coverage
145145

146146
Current Coverage is:
147147
```
148-
Statements : 94.51% ( 155/164 )
149-
Branches : 92.62% ( 113/122 )
148+
Statements : 96.86% ( 154/159 )
149+
Branches : 93.22% ( 110/118 )
150150
Functions : 100% ( 31/31 )
151-
Lines : 96.69% ( 146/151 )
151+
Lines : 97.97% ( 145/148 )
152152
```
153153

154154
## Features

csv2json-test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ var csv = 'header1,header2,header3\n'
88
+ ',"row1 column2, with comma", row1 column3\n'
99
+ 'row2 column1,row2 column2, row2 column3\n';
1010

11-
var csvArrays = '"info.name","coursesTaken","year"\n'
12-
+ '"Mike","[CS2500/CS2510]","Sophomore"\n'
13-
+ '"John","[ANTH1101/POL2312/MATH2142/POL3305/LAW2100]","Senior"\n'
14-
+ '"Joe","[]","Freshman"\n';
11+
var allCasesTest = '"carModel","priceRange.min","priceRange.max"\n'
12+
+ 'Audi,"9000",11000\n'
13+
+ '"BMW",14000,16000\n'
14+
+ '"Mercedes",19000,"21000"\n'
15+
+ '"Porsche","29000",31000\n'
16+
+ ',200,300\n';
1517

16-
converter.csv2json(mycsv, function (err, json) {
18+
19+
converter.csv2json(allCasesTest, function (err, json) {
1720
if (!err) {
1821
return console.log(json);
1922
}

lib/csv-2-json.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ var splitLine = function (line) {
142142
stateVariables.parsingValue = true;
143143
stateVariables.startIndex = index + 1;
144144
}
145-
// If the first field is empty (ie. line starts with a field delimiter)
146-
else if (character === options.DELIMITER.FIELD && index == 0) {
147-
splitLine.push('');
148-
stateVariables.parsingValue = false;
149-
}
145+
150146
// If we reached a wrap delimiter with a field delimiter after it (ie. *",)
151147
else if (character === options.DELIMITER.WRAP && charAfter === options.DELIMITER.FIELD) {
152148
splitLine.push(line.substring(stateVariables.startIndex, index));
@@ -197,11 +193,11 @@ module.exports = {
197193
if (!callback) { throw new Error(constants.Errors.callbackRequired); }
198194

199195
// Shouldn't happen, but just in case
200-
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); return null; }
196+
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); }
201197
options = opts; // Options were passed, set the global options value
202198

203199
// If we don't receive data, report an error
204-
if (!data) { return callback(new Error(constants.Errors.csv2json.cannotCallCsv2JsonOn + data + '.')); return null; }
200+
if (!data) { return callback(new Error(constants.Errors.csv2json.cannotCallCsv2JsonOn + data + '.')); }
205201

206202
// The data provided is not a string
207203
if (!_.isString(data)) {

test/CSV/quoted/nestedSomeFieldsWrappedJson.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Audi,"9000",11000
33
"BMW",14000,16000
44
"Mercedes",19000,"21000"
55
"Porsche","29000",31000
6+
,200,300
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"carModel": "Audi",
4+
"priceRange": {
5+
"min": "9000",
6+
"max": "11000"
7+
}
8+
},
9+
{
10+
"carModel": "BMW",
11+
"priceRange": {
12+
"min": "14000",
13+
"max": "16000"
14+
}
15+
},
16+
{
17+
"carModel": "Mercedes",
18+
"priceRange": {
19+
"min": "19000",
20+
"max": "21000"
21+
}
22+
},
23+
{
24+
"carModel": "Porsche",
25+
"priceRange": {
26+
"min": "29000",
27+
"max": "31000"
28+
}
29+
},
30+
{
31+
"carModel": null,
32+
"priceRange": {
33+
"min": "200",
34+
"max": "300"
35+
}
36+
}
37+
]

test/testCsv2Json.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ var csv2jsonTests = function () {
541541
converter.csv2json(csvTestData.quoted.nestedSomeFieldsWrappedJson, function (err, json) {
542542
if (err) { throw err; }
543543
true.should.equal(_.isEqual(err, null));
544-
var isEqual = _.isEqual(json, jsonTestData.nestedJson);
544+
// Stringify the JSON since these libraries don't say they are equal due to a null field
545+
var isEqual = _.isEqual(JSON.stringify(json), JSON.stringify(jsonTestData.nestedSomeFieldsWrappedJson));
545546
true.should.equal(isEqual);
546547
done();
547548
}, options);
@@ -608,6 +609,23 @@ var csv2jsonTests = function () {
608609
});
609610
});
610611

612+
describe('Custom Options - No Delimiters Specified', function () {
613+
beforeEach(function () {
614+
options = JSON.parse(JSON.stringify(defaultOptions));
615+
});
616+
617+
it('should still work when no delimiters specified', function (done) {
618+
delete options.DELIMITER;
619+
converter.csv2json(csvTestData.unQuoted.regularJson, function (err, json) {
620+
if (err) { throw err; }
621+
true.should.equal(_.isEqual(err, null));
622+
var isEqual = _.isEqual(json, jsonTestData.regularJson);
623+
true.should.equal(isEqual);
624+
done();
625+
}, options);
626+
});
627+
});
628+
611629
describe('Testing other errors', function () {
612630
beforeEach(function () {
613631
options = JSON.parse(JSON.stringify(defaultOptions));
@@ -1148,8 +1166,8 @@ var csv2jsonTests = function () {
11481166
it('should parse a CSV with some fields wrapped and others unwrapped', function (done) {
11491167
converter.csv2jsonAsync(csvTestData.quoted.nestedSomeFieldsWrappedJson, options)
11501168
.then(function (json) {
1151-
var isEqual = _.isEqual(json, jsonTestData.nestedJson);
1152-
true.should.equal(isEqual);
1169+
// Stringify the JSON since these libraries don't say they are equal due to a null field
1170+
var isEqual = _.isEqual(JSON.stringify(json), JSON.stringify(jsonTestData.nestedSomeFieldsWrappedJson)); true.should.equal(isEqual);
11531171
done();
11541172
})
11551173
.catch(function (err) {
@@ -1192,6 +1210,25 @@ var csv2jsonTests = function () {
11921210
});
11931211
});
11941212

1213+
describe('Custom Options - No Delimiters Specified', function () {
1214+
beforeEach(function () {
1215+
options = JSON.parse(JSON.stringify(defaultOptions));
1216+
});
1217+
1218+
it('should still work when no delimiters specified', function (done) {
1219+
delete options.DELIMITER;
1220+
converter.csv2jsonAsync(csvTestData.unQuoted.regularJson, options)
1221+
.then(function (json) {
1222+
var isEqual = _.isEqual(json, jsonTestData.regularJson);
1223+
true.should.equal(isEqual);
1224+
done();
1225+
})
1226+
.catch(function (err) {
1227+
throw err;
1228+
});
1229+
});
1230+
});
1231+
11951232
describe('Testing other errors', function () {
11961233
beforeEach(function () {
11971234
options = JSON.parse(JSON.stringify(defaultOptions));

test/testJsonFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
arrayValue_specificKeys: require('./JSON/arrayValueDocs_specificKeys'),
44
nestedComma: require('./JSON/nestedComma'),
55
nestedJson: require('./JSON/nestedJson'),
6+
nestedSomeFieldsWrappedJson: require('./JSON/nestedSomeFieldsWrappedJson'),
67
nestedJson2: require('./JSON/nestedJson2'),
78
nestedQuotes: require('./JSON/nestedQuotes'),
89
noData: require('./JSON/noData'),

0 commit comments

Comments
 (0)