Skip to content

Commit aff1aee

Browse files
authored
Additional tests
1 parent b9a7b93 commit aff1aee

8 files changed

+124
-2
lines changed

test/config/testCsvFilesList.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ const fs = require('fs'),
4141
{key: 'emptyLastFieldValueNoEol', file: '../data/csv/emptyLastFieldValueNoEol.csv'},
4242
{key: 'lastCharFieldDelimiter', file: '../data/csv/lastCharFieldDelimiter.csv'},
4343
{key: 'nativeMapMethod', file: '../data/csv/nativeMapMethod.csv'},
44-
{key: 'nestedDotKeys', file: '../data/csv/nestedDotKeys.csv'}
44+
{key: 'nestedDotKeys', file: '../data/csv/nestedDotKeys.csv'},
45+
{key: 'nestedDotKeysWithArray', file: '../data/csv/nestedDotKeysWithArray.csv'},
46+
{key: 'nestedDotKeysWithArrayExpandedUnwound', file: '../data/csv/nestedDotKeysWithArrayExpandedUnwound.csv'}
4547
];
4648

4749
function readCsvFile(filePath) {

test/config/testJsonFilesList.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ module.exports = {
3131
emptyLastFieldValue: require('../data/json/emptyLastFieldValue.json'),
3232
emptyLastFieldValueNoEol: require('../data/json/emptyLastFieldValueNoEol.json'),
3333
nativeMapMethod: require('../data/json/nativeMapMethod.json'),
34-
nestedDotKeys: require('../data/json/nestedDotKeys.json')
34+
nestedDotKeys: require('../data/json/nestedDotKeys.json'),
35+
nestedDotKeysWithArray: require('../data/json/nestedDotKeysWithArray.json'),
36+
nestedDotKeysWithArrayExpandedUnwound: require('../data/json/nestedDotKeysWithArrayExpandedUnwound.json')
3537
};

test/csv2json.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ function runTests(jsonTestData, csvTestData) {
207207
done();
208208
});
209209
});
210+
211+
// Test case for #184
212+
it('should properly convert keys with escaped/nested dots in them even when they appear in an array', (done) => {
213+
converter.csv2json(csvTestData.nestedDotKeysWithArray, (err, json) => {
214+
if (err) done(err);
215+
json.should.deepEqual(jsonTestData.nestedDotKeysWithArray);
216+
done();
217+
});
218+
});
219+
220+
// Test case for #184
221+
it('should properly convert keys with escaped/nested dots in them even when they appear in an expanded, unwound array', (done) => {
222+
converter.csv2json(csvTestData.nestedDotKeysWithArrayExpandedUnwound, (err, json) => {
223+
if (err) done(err);
224+
json.should.deepEqual(jsonTestData.nestedDotKeysWithArrayExpandedUnwound);
225+
done();
226+
});
227+
});
210228
});
211229

212230
describe('Error Handling', () => {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a\.a,a\.b.c\.d,a\.b.c,a\.b.c\.f,a.a,a.b,a.b\.c\.d,a.f.g\.h,a.f.g\.h\.k,a.f.h.l\.m\.n\.o\.p,c
2+
a,4,5,6,1,2,32,3,5,qrstuv,"[{""d.e"":103,""f"":2},{""d.e"":105,""f"":5}]"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a\.a,a\.b.c\.d,a\.b.c,a\.b.c\.f,a.a,a.b,a.b\.c\.d,a.f.g\.h,a.f.g\.h\.k,a.f.h.l\.m\.n\.o\.p,c.d\.e,c.f
2+
a,4,5,6,1,2,32,3,5,qrstuv,103,2
3+
a,4,5,6,1,2,32,3,5,qrstuv,105,5
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"a.a": "a",
4+
"a.b": {
5+
"c.d": 4,
6+
"c": 5,
7+
"c.f": 6
8+
},
9+
"a": {
10+
"a": 1,
11+
"b": 2,
12+
"b.c.d": 32,
13+
"f": {
14+
"g.h": 3,
15+
"g.h.k": 5,
16+
"h": {
17+
"l.m.n.o.p": "qrstuv"
18+
}
19+
}
20+
},
21+
"c": [
22+
{ "d.e" : 103, "f": 2 },
23+
{ "d.e" : 105, "f": 5 }
24+
]
25+
}
26+
]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[
2+
{
3+
"a.a": "a",
4+
"a.b": {
5+
"c.d": 4,
6+
"c": 5,
7+
"c.f": 6
8+
},
9+
"a": {
10+
"a": 1,
11+
"b": 2,
12+
"b.c.d": 32,
13+
"f": {
14+
"g.h": 3,
15+
"g.h.k": 5,
16+
"h": {
17+
"l.m.n.o.p": "qrstuv"
18+
}
19+
}
20+
},
21+
"c": { "d.e" : 103, "f": 2 }
22+
},
23+
{
24+
"a.a": "a",
25+
"a.b": {
26+
"c.d": 4,
27+
"c": 5,
28+
"c.f": 6
29+
},
30+
"a": {
31+
"a": 1,
32+
"b": 2,
33+
"b.c.d": 32,
34+
"f": {
35+
"g.h": 3,
36+
"g.h.k": 5,
37+
"h": {
38+
"l.m.n.o.p": "qrstuv"
39+
}
40+
}
41+
},
42+
"c": { "d.e" : 105, "f": 5 }
43+
}
44+
]

test/json2csv.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ function runTests(jsonTestData, csvTestData) {
154154
done();
155155
});
156156
});
157+
158+
// Test case for #184
159+
it('should properly handle keys with nested dots in them even when they appear in array fields', (done) => {
160+
converter.json2csv(jsonTestData.nestedDotKeysWithArray, (err, csv) => {
161+
if (err) done(err);
162+
csv.should.equal(csvTestData.nestedDotKeysWithArray);
163+
done();
164+
});
165+
});
157166
});
158167

159168
describe('Error Handling', () => {
@@ -646,6 +655,22 @@ function runTests(jsonTestData, csvTestData) {
646655
wrapBooleans: true
647656
});
648657
});
658+
659+
// Test case for #184
660+
it('should handle keys with nested dots when expanding and unwinding arrays', (done) => {
661+
converter.json2csv(jsonTestData.nestedDotKeysWithArrayExpandedUnwound, (err, csv) => {
662+
if (err) done(err);
663+
664+
// Replace raw boolean values with quoted versions
665+
let expectedCsv = csvTestData.nestedDotKeysWithArrayExpandedUnwound;
666+
667+
csv.should.equal(expectedCsv);
668+
done();
669+
}, {
670+
expandArrayObjects: true,
671+
unwindArrays: true
672+
});
673+
});
649674
});
650675
});
651676

0 commit comments

Comments
 (0)