Skip to content

Commit

Permalink
SonarQube
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Nov 23, 2024
1 parent 7af035c commit 687a6a2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 31 deletions.
30 changes: 23 additions & 7 deletions src/lib/hierarchy-operations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ const HierarchyOperations = (db, options) => {
});
}

function replaceLineageInReports(reportsCreatedByDescendants, replaceWith, startingFromIdInLineage) {
function replaceLineageInReports(reportsCreatedByDescendants, replaceWith, startingFromId) {
return reportsCreatedByDescendants.reduce((agg, doc) => {
if (lineageManipulation.replaceLineage(doc, 'contact', replaceWith, startingFromIdInLineage, options)) {
const replaceLineageOptions = {
lineageAttribute: 'contact',
replaceWith,
startingFromId,
merge: options.merge,
};

if (lineageManipulation.replaceLineage(doc, replaceLineageOptions)) {
agg.push(doc);
}
return agg;
Expand All @@ -139,18 +146,27 @@ const HierarchyOperations = (db, options) => {
}, []);
}

function replaceLineageInContacts(descendantsAndSelf, replacementLineage, destinationId) {
function replaceLineageInContacts(descendantsAndSelf, replaceWith, destinationId) {
function replaceForSingleContact(doc) {
const docIsDestination = doc._id === destinationId;
const startingFromIdInLineage = options.merge || !docIsDestination ? destinationId : undefined;
const parentWasUpdated = lineageManipulation.replaceLineage(doc, 'parent', replacementLineage, startingFromIdInLineage, options);
const contactWasUpdated = lineageManipulation.replaceLineage(doc, 'contact', replacementLineage, destinationId, options);
const startingFromId = options.merge || !docIsDestination ? destinationId : undefined;
const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith,
startingFromId,
merge: options.merge,
};
const parentWasUpdated = lineageManipulation.replaceLineage(doc, replaceLineageOptions);

replaceLineageOptions.lineageAttribute = 'contact';
replaceLineageOptions.startingFromId = destinationId;
const contactWasUpdated = lineageManipulation.replaceLineage(doc, replaceLineageOptions);
const isUpdated = parentWasUpdated || contactWasUpdated;
if (isUpdated) {
result.push(doc);
}
}

const result = [];
for (const doc of descendantsAndSelf) {
const docIsDestination = doc._id === destinationId;
Expand Down
28 changes: 15 additions & 13 deletions src/lib/hierarchy-operations/lineage-manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@
* Given a doc, replace the lineage information therein with "replaceWith"
*
* @param {Object} doc A CouchDB document containing a hierarchy that needs replacing
* @param {string} lineageAttributeName Name of the attribute which is a lineage in doc (contact or parent)
* @param {Object} replaceWith The new hierarchy { parent: { _id: 'parent', parent: { _id: 'grandparent' } }
* @param {string} [startingFromIdInLineage] Only the part of the lineage "after" this id will be replaced
* @param {Object} options
* @param {boolean} merge When true, startingFromIdInLineage is replaced and when false, startingFromIdInLineage's parent is replaced
* @param {Object} params SonarQube
* @param {string} params.lineageAttribute Name of the attribute which is a lineage in doc (contact or parent)
* @param {Object} params.replaceWith The new hierarchy { parent: { _id: 'parent', parent: { _id: 'grandparent' } }
* @param {string} params.startingFromId Only the part of the lineage "after" this id will be replaced
* @param {boolean} params.merge When true, startingFromId is replaced and when false, startingFromId's parent is replaced
*/
function replaceLineage(doc, lineageAttributeName, replaceWith, startingFromIdInLineage, options={}) {
function replaceLineage(doc, params) {
const { lineageAttribute, replaceWith, startingFromId, merge } = params;

// Replace the full lineage
if (!startingFromIdInLineage) {
return replaceWithinLineage(doc, lineageAttributeName, replaceWith);
if (!startingFromId) {
return replaceWithinLineage(doc, lineageAttribute, replaceWith);
}

function getInitialState() {
if (options.merge) {
if (merge) {
return {
element: doc,
attributeName: lineageAttributeName,
attributeName: lineageAttribute,
};
}

return {
element: doc[lineageAttributeName],
element: doc[lineageAttribute],
attributeName: 'parent',
};
}

function traverseOne() {
const compare = options.merge ? state.element[state.attributeName] : state.element;
if (compare?._id === startingFromIdInLineage) {
const compare = merge ? state.element[state.attributeName] : state.element;
if (compare?._id === startingFromId) {
return replaceWithinLineage(state.element, state.attributeName, replaceWith);
}

Expand Down
66 changes: 55 additions & 11 deletions test/lib/hierarchy-operations/lineage-manipulation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ const log = require('../../../src/lib/log');
log.level = log.LEVEL_TRACE;

const { parentsToLineage } = require('../../mock-hierarchies');
const mergeOption = { merge: true };

describe('lineage manipulation', () => {
describe('kenn replaceLineage', () => {
describe('replaceLineage', () => {
const mockReport = data => Object.assign({ _id: 'r', type: 'data_record', contact: parentsToLineage('parent', 'grandparent') }, data);
const mockContact = data => Object.assign({ _id: 'c', type: 'person', parent: parentsToLineage('parent', 'grandparent') }, data);

it('replace with empty lineage', () => {
const mock = mockReport();
expect(replaceLineage(mock, 'contact', undefined)).to.be.true;
const replaceLineageOptions = {
lineageAttribute: 'contact',
replaceWith: undefined,
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.true;
expect(mock).to.deep.eq({
_id: 'r',
type: 'data_record',
Expand All @@ -23,7 +26,11 @@ describe('lineage manipulation', () => {

it('replace full lineage', () => {
const mock = mockContact();
expect(replaceLineage(mock, 'parent', parentsToLineage('new_parent'))).to.be.true;
const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith: parentsToLineage('new_parent'),
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.true;
expect(mock).to.deep.eq({
_id: 'c',
type: 'person',
Expand All @@ -35,7 +42,11 @@ describe('lineage manipulation', () => {
const mock = mockContact();
delete mock.parent;

expect(replaceLineage(mock, 'parent', parentsToLineage('new_parent'))).to.be.true;
const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith: parentsToLineage('new_parent'),
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.true;
expect(mock).to.deep.eq({
_id: 'c',
type: 'person',
Expand All @@ -46,12 +57,23 @@ describe('lineage manipulation', () => {
it('replace empty with empty', () => {
const mock = mockContact();
delete mock.parent;
expect(replaceLineage(mock, 'parent', undefined)).to.be.false;

const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith: undefined,
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.false;
});

it('replace lineage starting at contact', () => {
const mock = mockContact();
expect(replaceLineage(mock, 'parent', parentsToLineage('new_grandparent'), 'parent')).to.be.true;

const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith: parentsToLineage('new_grandparent'),
startingFromId: 'parent',
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.true;
expect(mock).to.deep.eq({
_id: 'c',
type: 'person',
Expand All @@ -61,7 +83,13 @@ describe('lineage manipulation', () => {

it('merge new parent', () => {
const mock = mockContact();
expect(replaceLineage(mock, 'parent', parentsToLineage('new_parent', 'new_grandparent'), 'parent', mergeOption)).to.be.true;
const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith: parentsToLineage('new_parent', 'new_grandparent'),
startingFromId: 'parent',
merge: true,
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.true;
expect(mock).to.deep.eq({
_id: 'c',
type: 'person',
Expand All @@ -71,7 +99,13 @@ describe('lineage manipulation', () => {

it('merge grandparent of contact', () => {
const mock = mockReport();
expect(replaceLineage(mock, 'contact', parentsToLineage('new_grandparent'), 'grandparent', mergeOption)).to.be.true;
const replaceLineageOptions = {
lineageAttribute: 'contact',
replaceWith: parentsToLineage('new_grandparent'),
startingFromId: 'grandparent',
merge: true,
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.true;
expect(mock).to.deep.eq({
_id: 'r',
type: 'data_record',
Expand All @@ -81,7 +115,12 @@ describe('lineage manipulation', () => {

it('replace empty starting at contact', () => {
const mock = mockContact();
expect(replaceLineage(mock, 'parent', undefined, 'parent')).to.be.true;
const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith: undefined,
startingFromId: 'parent',
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.true;
expect(mock).to.deep.eq({
_id: 'c',
type: 'person',
Expand All @@ -91,7 +130,12 @@ describe('lineage manipulation', () => {

it('replace starting at non-existant contact', () => {
const mock = mockContact();
expect(replaceLineage(mock, 'parent', parentsToLineage('irrelevant'), 'dne')).to.be.false;
const replaceLineageOptions = {
lineageAttribute: 'parent',
replaceWith: parentsToLineage('irrelevant'),
startingFromId: 'dne',
};
expect(replaceLineage(mock, replaceLineageOptions)).to.be.false;
});
});

Expand Down

0 comments on commit 687a6a2

Please sign in to comment.