git-csv-diff
Advanced tools
Comparing version 1.3.0 to 1.3.1
{ | ||
"name": "git-csv-diff", | ||
"author": "Valor-Software", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"license": "GPL-3.0", | ||
@@ -26,3 +26,3 @@ "description": "Library generate difference between csv-files based on Git commit hash", | ||
"test:e2e": "./node_modules/.bin/mocha test/e2e/*.js --reporter spec", | ||
"test:unit": "istanbul cover _mocha -- -R spec 'test/unit/*.spec.js'" | ||
"test:unit": "istanbul --include-all-sources cover _mocha -- -R spec 'test/unit/*.spec.js'" | ||
}, | ||
@@ -29,0 +29,0 @@ "dependencies": { |
@@ -20,8 +20,2 @@ 'use strict'; | ||
function diffByFile() { | ||
return { | ||
process: _process | ||
}; | ||
} | ||
// metaData.fileName; | ||
@@ -36,4 +30,11 @@ // metaData.fileModifier; | ||
module.exports = { | ||
process: _process, | ||
_splitPrimaryKeysByPathToOldAndNew: splitPrimaryKeysByPathToOldAndNew | ||
}; | ||
function _process(metaData, dataDiff, streams) { | ||
metaData = Object.assign({}, metaData, splitPrimaryKeysByPathToOldAndNew(metaData)); | ||
const isTranslations = diffHelpers.isLanguageFile(metaData.fileName); | ||
@@ -50,3 +51,3 @@ const baseStream = isTranslations ? streams.lang : streams.diff; | ||
// validate input data | ||
if(!isSchemaExists(modelResponse.metadata) || !isValidFilePath(metaData.fileName)) { | ||
if (!isSchemaExists(modelResponse.metadata) || !isValidFilePath(metaData.fileName)) { | ||
return; | ||
@@ -86,7 +87,7 @@ } | ||
if (modificationType !== diffModifiers.BLANK) { | ||
if(diffStrategy.has(modificationType)){ | ||
if (diffStrategy.has(modificationType)) { | ||
const diffInstance = diffStrategy.get(modificationType); | ||
diffInstance.process(baseStream, metaData, modelResponse, modelDiff, diffResultColumns, rowValue); | ||
} | ||
// if nothing changed | ||
// if nothing changed | ||
} else { | ||
@@ -121,3 +122,3 @@ // Case: new columns were added | ||
let lang = 'default'; | ||
if(diffHelpers.isLanguageFile(fileName)) { | ||
if (diffHelpers.isLanguageFile(fileName)) { | ||
const regexpRes = /lang\/(.+)\//.exec(fileName); | ||
@@ -135,3 +136,3 @@ lang = regexpRes[1] || lang; | ||
// info is not available if file was removed | ||
if(metaData.fileModifier != "D") { | ||
if (metaData.fileModifier != "D") { | ||
const resourcesByPathNew = _.keyBy(metaData.datapackage.new.resources, 'path'); | ||
@@ -183,2 +184,16 @@ file.new = resourcesByPathNew[fileName]; | ||
module.exports = new diffByFile(); | ||
function splitPrimaryKeysByPathToOldAndNew(metadata) { | ||
return { | ||
primaryKeyByPath: { | ||
old: convertResourcesToPrimaryKeyByPath(_.get(metadata, 'datapackage.old.resources')), | ||
new: convertResourcesToPrimaryKeyByPath(_.get(metadata, 'datapackage.new.resources')) | ||
} | ||
}; | ||
} | ||
function convertResourcesToPrimaryKeyByPath(resources) { | ||
return _.reduce(resources, (result, resource) => { | ||
result[resource.path] = resource.schema.primaryKey; | ||
return result; | ||
}, {}); | ||
} |
@@ -25,3 +25,3 @@ 'use strict'; | ||
const isTranslations = diffHelpers.isLanguageFile(metaData.fileName); | ||
const isDataPointsFile = diffHelpers.isDatapointFile(metaData.fileName); | ||
const isDataPointsFile = diffHelpers.isDatapointFile(metaData); | ||
@@ -63,2 +63,2 @@ if(isDataPointsFile && isTranslations) { | ||
module.exports = new diffHeaderRemove(); | ||
module.exports = new diffHeaderRemove(); |
@@ -20,4 +20,11 @@ 'use strict'; | ||
diffHelpers.prototype.isDatapointFile = function (filename) { | ||
return _.includes(filename, "--datapoints--"); | ||
diffHelpers.prototype.isDatapointFile = function (metadata) { | ||
const fileName = this.isLanguageFile(metadata.fileName) ? _.last(_.split(metadata.fileName, '/')) : metadata.fileName; | ||
const primaryKeyByPath = _.get(metadata, 'primaryKeyByPath'); | ||
const newPrimaryKey = _.get(primaryKeyByPath, ['new', fileName]); | ||
const oldPrimaryKey = _.get(primaryKeyByPath, ['old', fileName]); | ||
const primaryKey = newPrimaryKey || oldPrimaryKey; | ||
return _.isArray(primaryKey) && primaryKey.length > 1; | ||
}; | ||
@@ -46,2 +53,2 @@ | ||
module.exports = new diffHelpers(); | ||
module.exports = new diffHelpers(); |
@@ -28,3 +28,3 @@ 'use strict'; | ||
const isDataPointsFile = diffHelpers.isDatapointFile(metaData.fileName); | ||
const isDataPointsFile = diffHelpers.isDatapointFile(metaData); | ||
const isTranslations = diffHelpers.isLanguageFile(metaData.fileName); | ||
@@ -31,0 +31,0 @@ |
@@ -19,3 +19,3 @@ 'use strict'; | ||
diffRowRemove.prototype.process = function (baseStream, metaData, modelResponse, modelDiff, diffResultColumns, rowValue) { | ||
const isDataPointsFile = diffHelpers.isDatapointFile(metaData.fileName); | ||
const isDataPointsFile = diffHelpers.isDatapointFile(metaData); | ||
@@ -22,0 +22,0 @@ const primaryKey = _.first(diffHelpers.getPrimaryKeys(modelResponse.metadata)); |
@@ -34,2 +34,7 @@ const expect = require("chai").expect; | ||
const metaDataDatapoint = { | ||
primaryKeyByPath: { | ||
new: { | ||
'ddf--datapoints--lines.csv': ['bla', 'bla'] | ||
}, | ||
}, | ||
file: {old: {schema: {primaryKey: 'column 1'}}, new: {schema: {primaryKey: 'column 2'}}}, | ||
@@ -103,3 +108,3 @@ fileName: '/lang/nl-nl/ddf--datapoints--lines.csv' | ||
testFile.process(baseStream, metaDataDatapoint, modelResponse, modelDiff, diffResultColumns, rowValue); | ||
expect(diffHelpersStub.calledOnce).to.equal(true); | ||
sinon.assert.calledOnce(diffHelpersStub); | ||
expect(diffHelpersStub.getCall(0).args[1]).to.deep.equal(resultFixture); | ||
@@ -113,2 +118,2 @@ | ||
}); | ||
}); |
@@ -79,25 +79,110 @@ const expect = require("chai").expect; | ||
describe("API::isDatapointFile", function () { | ||
it("should return correct result for datapoint file: taken from new datapackage", function () { | ||
const metadata = { | ||
fileName: 'ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: { | ||
new: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': ['company', 'project', 'anno'] | ||
} | ||
} | ||
}; | ||
it("should return correct result for datapoint file", function (done) { | ||
expect(testFile.isDatapointFile(metadata)).to.equal(true); | ||
}); | ||
const input = 'ddf--datapoints--lines_of_code--by--company--project--anno.csv'; | ||
const resultFixture = true; | ||
it("should return correct result for datapoint file: taken from old datapackage", function () { | ||
const metadata = { | ||
fileName: 'ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: { | ||
old: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': ['company', 'project', 'anno'] | ||
} | ||
} | ||
}; | ||
const result = testFile.isDatapointFile(input); | ||
expect(result).to.deep.equal(resultFixture); | ||
expect(testFile.isDatapointFile(metadata)).to.equal(true); | ||
}); | ||
done(); | ||
it("should return correct result for datapoint file: new datapackage has priority over the old datapackage", function () { | ||
const metadata = { | ||
fileName: 'ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: { | ||
old: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': [] | ||
}, | ||
new: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': ['company', 'project', 'anno'] | ||
}, | ||
} | ||
}; | ||
expect(testFile.isDatapointFile(metadata)).to.equal(true); | ||
}); | ||
it("should return correct result for non-datapoint file", function (done) { | ||
it("should return correct result for datapoint file: datapoints translations are also included", function () { | ||
const metadata = { | ||
fileName: 'lang/path/ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: { | ||
new: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': ['company', 'project', 'anno'] | ||
} | ||
} | ||
}; | ||
const input = 'ddf--concepts.csv'; | ||
const resultFixture = false; | ||
expect(testFile.isDatapointFile(metadata)).to.equal(true); | ||
}); | ||
const result = testFile.isDatapointFile(input); | ||
expect(result).to.deep.equal(resultFixture); | ||
it("should detect when a file is not about datapoints: primaryKey is a single non-array value", function () { | ||
const metadata = { | ||
fileName: 'lang/path/ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: { | ||
new: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': 'company' | ||
} | ||
} | ||
}; | ||
done(); | ||
expect(testFile.isDatapointFile(metadata)).to.deep.equal(false); | ||
}); | ||
it("should detect when a file is not about datapoints: primaryKey is null", function () { | ||
const metadata = { | ||
fileName: 'lang/path/ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: { | ||
new: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': null | ||
} | ||
} | ||
}; | ||
expect(testFile.isDatapointFile(metadata)).to.deep.equal(false); | ||
}); | ||
it("should detect when a file is not about datapoints: primaryKey is an array with a single value", function () { | ||
const metadata = { | ||
fileName: 'lang/path/ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: { | ||
new: { | ||
'ddf--datapoints--lines_of_code--by--company--project--anno.csv': ['bla'] | ||
} | ||
} | ||
}; | ||
expect(testFile.isDatapointFile(metadata)).to.deep.equal(false); | ||
}); | ||
it("should not treat file as a datapoints' one when there is not enough info in metadata to detect this: there are no new and old primary keys by path", function () { | ||
const metadata = { | ||
fileName: 'lang/path/ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
primaryKeyByPath: {} | ||
}; | ||
expect(testFile.isDatapointFile(metadata)).to.deep.equal(false); | ||
}); | ||
it("should not treat file as a datapoints' one when there is not enough info in metadata to detect this: there is no primary keys by path property", function () { | ||
const metadata = { | ||
fileName: 'lang/path/ddf--datapoints--lines_of_code--by--company--project--anno.csv', | ||
}; | ||
expect(testFile.isDatapointFile(metadata)).to.deep.equal(false); | ||
}); | ||
}); | ||
@@ -204,2 +289,2 @@ | ||
}); | ||
}); |
@@ -34,2 +34,7 @@ const expect = require("chai").expect; | ||
const metaDataDatapoint = { | ||
primaryKeyByPath: { | ||
new: { | ||
'ddf--datapoints--lines.csv': ['bla', 'bla'] | ||
} | ||
}, | ||
file: {old: {schema: {primaryKey: 'column 1'}}, new: {schema: {primaryKey: 'column 2'}}}, | ||
@@ -36,0 +41,0 @@ fileName: 'ddf--datapoints--lines.csv' |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
407349
120
5962
0