-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): UNEXPECTED_DATA rule providing
Closes #67
- Loading branch information
Showing
20 changed files
with
505 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# UNEXPECTED_DATA | ||
|
||
## Rule test folders | ||
|
||
`test/fixtures/rules-cases/unexpected-data/indexed` | ||
`test/fixtures/rules-cases/unexpected-data/indexless` | ||
|
||
## Description | ||
|
||
An issue according to this rule will be fired when filename and header are good but content isn't: content | ||
|
||
## Examples of correct data | ||
|
||
``` | ||
ddf--concepts.csv | ||
concept,concept_type,domain,name | ||
name,string,, | ||
geo,entity_domain,, | ||
region,entity_set,geo,Region | ||
country,entity_set,geo,Country | ||
capital,entity_set,geo,Capital | ||
pop,measure,geo,Population | ||
year,time,,year | ||
``` | ||
|
||
## Examples of incorrect data | ||
|
||
``` | ||
ddf--concepts.csv | ||
concept,concept_type,domain,name | ||
foo | ||
geo,entity_domain,, | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const fs = require('fs'); | ||
const CsvParser = require('babyparse'); | ||
|
||
const getErrors = parsedCsv => parsedCsv.errors | ||
.filter(error => error.row >= 0) | ||
.map(error => ({ | ||
message: error.message, | ||
row: error.row, | ||
type: `${error.type}/${error.code}`, | ||
data: parsedCsv.data[error.row] | ||
})); | ||
|
||
class CsvChecker { | ||
constructor(filePath) { | ||
this.filePath = filePath; | ||
this.error = null; | ||
this.errors = []; | ||
} | ||
|
||
check(onChecked) { | ||
fs.readFile(this.filePath, 'utf-8', (err, fileContent) => { | ||
if (err) { | ||
onChecked(); | ||
return; | ||
} | ||
|
||
CsvParser.parse(fileContent, { | ||
header: true, | ||
delimiter: ',', | ||
skipEmptyLines: true, | ||
complete: parsedCsv => { | ||
this.errors = getErrors(parsedCsv); | ||
onChecked(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
isCorrect() { | ||
return _.isEmpty(this.errors); | ||
} | ||
} | ||
|
||
module.exports = CsvChecker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const registry = require('../registry'); | ||
const Issue = require('../issue'); | ||
|
||
module.exports = { | ||
rule: ddfDataSet => { | ||
const result = []; | ||
|
||
ddfDataSet.ddfRoot.directoryDescriptors.forEach(directoryDescriptor => { | ||
if (!directoryDescriptor.ddfIndex.csvChecker.isCorrect()) { | ||
result.push( | ||
new Issue(registry.UNEXPECTED_DATA) | ||
.setPath(directoryDescriptor.ddfIndex.indexPath) | ||
.setData(directoryDescriptor.ddfIndex.csvChecker.errors) | ||
); | ||
} | ||
|
||
directoryDescriptor.fileDescriptors.forEach(fileDescriptor => { | ||
if (!fileDescriptor.csvChecker.isCorrect()) { | ||
result.push( | ||
new Issue(registry.UNEXPECTED_DATA) | ||
.setPath(fileDescriptor.fullPath) | ||
.setData(fileDescriptor.csvChecker.errors) | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
const _ = require('lodash'); | ||
const chai = require('chai'); | ||
const sinonChai = require('sinon-chai'); | ||
const expect = chai.expect; | ||
|
||
const CsvChecker = require('../lib/data/csv-checker'); | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('csv checker', () => { | ||
it('should be no errors for correct csv file', done => { | ||
const csvChecker = new CsvChecker('./test/fixtures/good-folder/ddf--concepts.csv'); | ||
|
||
csvChecker.check(() => { | ||
expect(csvChecker.isCorrect()).to.be.true; | ||
expect(_.isEmpty(csvChecker.errors)).to.be.true; | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should be no errors for correct csv file', done => { | ||
const csvChecker = new CsvChecker('./test/fixtures/csv/bad.csv'); | ||
const expectedErrors = [{ | ||
message: 'Too few fields: expected 3 fields but parsed 1', | ||
row: 1, | ||
type: 'FieldMismatch/TooFewFields', | ||
data: {aaa: '444'} | ||
}, | ||
{ | ||
message: 'Too few fields: expected 3 fields but parsed 2', | ||
row: 2, | ||
type: 'FieldMismatch/TooFewFields', | ||
data: {aaa: '555', bbb: '777'} | ||
}, | ||
{ | ||
message: 'Too few fields: expected 3 fields but parsed 1', | ||
row: 3, | ||
type: 'FieldMismatch/TooFewFields', | ||
data: {aaa: 'foo'} | ||
}]; | ||
|
||
csvChecker.check(() => { | ||
expect(csvChecker.isCorrect()).to.be.false; | ||
expect(_.isEqual(csvChecker.errors, expectedErrors)).to.be.true; | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.