Skip to content

Commit

Permalink
add tests for generate json array from csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
iuccio committed Jun 10, 2017
1 parent a832cd3 commit e8a620f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 89 deletions.
11 changes: 0 additions & 11 deletions input.csv

This file was deleted.

72 changes: 0 additions & 72 deletions output.json

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"homepage": "https://github.com/iuccio/CSVtoJSON#readme",
"devDependencies": {
"chai": "^4.0.2",
"chai-fs": "^1.0.0",
"gulp": "^3.9.1",
"gulp-mocha": "^4.3.1",
"gulp-util": "^3.0.8",
Expand Down
14 changes: 8 additions & 6 deletions src/csvToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ class CsvToJson {
let jsonResult = [];
for (let i = 1; i < lines.length; i++) {
let currentLine = lines[i].split(fieldDelimiter);
let jsonObject = {};
for (let j = 0; j < headers.length; j++) {
let propertyName = stringUtils.trimPropertyName(headers[j]);
let value = stringUtils.getValueFormatByType(currentLine[j]);
jsonObject[propertyName] = value;
if (stringUtils.hasContent(currentLine)) {
let jsonObject = {};
for (let j = 0; j < headers.length; j++) {
let propertyName = stringUtils.trimPropertyName(headers[j]);
let value = stringUtils.getValueFormatByType(currentLine[j]);
jsonObject[propertyName] = value;
}
jsonResult.push(jsonObject);
}
jsonResult.push(jsonObject);
}
return jsonResult;
}
Expand Down
11 changes: 11 additions & 0 deletions src/util/stringUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@ class StringUtils {
}
return String(value);
}

hasContent(values){
if(values.length >0){
for(let i=0; i<values.length;i++){
if(values[i]){
return true;
}
}
}
return false;
}
}
module.exports = new StringUtils();
51 changes: 51 additions & 0 deletions test/csvToJson.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

let expect = require('chai').expect;
let assert = require('chai').assert;
let csvToJson = require('../src/csvToJson');

let expectedJson = [{
firstName: 'Constantin',
lastName: 'Langsdon',
email: '[email protected]',
gender: 'Male',
age: 96,
birth: '10.02.1965'
}, {
firstName: 'Norah',
lastName: 'Raison',
email: '[email protected]',
gender: 'Female',
age: 32,
birth: '10.05.2000'
}];

let fileInputName = 'test/resource/input.csv';

describe('CsvToJson class testing', function () {

it('should return json array', function () {
//given

//when
let result = csvToJson.getJsonFromCsv(fileInputName);

//then
expect(result.length).to.equal(expectedJson.length);
expect(result).to.deep.equal(expectedJson);

});

it('should return json array that contains the same property of the csv header', function () {
//given
let headers = ['firstName', 'lastName', 'email', 'gender', 'age', 'birth'];

//when
let result = csvToJson.getJsonFromCsv(fileInputName);

//then
assert.isDefined(result);
assert.hasAllKeys(result[0],headers);
});

});
3 changes: 3 additions & 0 deletions test/resource/input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
firstName;lastName;email;gender;age;birth
Constantin;Langsdon;[email protected];Male;96;10.02.1965
Norah;Raison;[email protected];Female;32;10.05.2000
11 changes: 11 additions & 0 deletions test/resource/input_example.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
firstName;lastName;email;gender;age;birth
Constantin;Langsdon;[email protected];Male;96;10.02.1965
Norah;Raison;[email protected];Female;32;10.05.2000
Noll;Taborre;[email protected];Male;52;05.12.1920
Karlen;Nolot;[email protected];Female;2;11.07.1986
Melva;Trengrouse;[email protected];Female;64;19.11.1978
Stanislaus;Singers;[email protected];Male;17;13.02.1963
Justin;Gerholz;[email protected];Male;77;19.12.1990
Brantley;Degue;[email protected];Male;25;07.05.1974
Emery;Lannin;[email protected];Male;32;10.02.1985
Krystalle;Hilbourne;[email protected];Female;67;10.10.1910

0 comments on commit e8a620f

Please sign in to comment.