Skip to content

Commit 61f4e87

Browse files
committed
sortExtractors now returns an array, tests corrected and expanded
1 parent 2fae8e9 commit 61f4e87

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

src/client/MCODEClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class MCODEClient extends BaseClient {
6666
{ type: 'CSVAdverseEventExtractor', dependencies: ['CSVPatientExtractor'] },
6767
];
6868
// Sort extractors based on order and dependencies
69-
sortExtractors(this.extractorConfig, dependencyInfo);
69+
this.extractorConfig = sortExtractors(this.extractorConfig, dependencyInfo);
7070
// Store webServiceAuthConfig if provided`
7171
this.authConfig = webServiceAuthConfig;
7272
this.commonExtractorArgs = {

src/helpers/dependencyUtils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const logger = require('./logger');
1414
*/
1515
function sortExtractors(extractors, dependencyInfo) {
1616
const missing = {};
17-
// Filter dependency info to onlly extractors in the config
17+
// Filter dependency info to only extractors in the config
1818
dependencyInfo.filter((e) => extractors.map((x) => x.type).includes(e.type)).forEach((extractor) => {
1919
// For each extractor, check if its dependencies are present
2020
extractor.dependencies.forEach((dependency) => {
@@ -35,8 +35,10 @@ function sortExtractors(extractors, dependencyInfo) {
3535
throw new Error('Some extractors are missing dependencies, see above for details.');
3636
}
3737
// If no missing dependencies, sort extractors into correct order
38+
const sortedExtractors = [...extractors];
3839
const order = dependencyInfo.map((x) => x.type);
39-
extractors.sort((a, b) => order.indexOf(a.type) - order.indexOf(b.type));
40+
sortedExtractors.sort((a, b) => order.indexOf(a.type) - order.indexOf(b.type));
41+
return sortedExtractors;
4042
}
4143

4244
module.exports = {

test/cli/mcodeExtraction.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ describe('mcodeExtraction', () => {
9292
filePath: path.join(__dirname, './fixtures/example-clinical-trial-info.csv'),
9393
},
9494
},
95+
{
96+
label: 'patient',
97+
type: 'CSVPatientExtractor',
98+
constructorArgs: {
99+
filePath: path.join(__dirname, './fixtures/example-patient.csv'),
100+
},
101+
},
95102
],
96103
};
97104

@@ -105,5 +112,21 @@ describe('mcodeExtraction', () => {
105112
const flatErrors = flattenErrorValues(totalExtractionErrors);
106113
expect(flatErrors).toHaveLength(3);
107114
});
115+
it('should throw error when initialized with missing dependencies', async () => {
116+
const testConfig = {
117+
extractors: [
118+
// Should fail when this extractor is run without patient data in context
119+
{
120+
label: 'CTI',
121+
type: 'CSVClinicalTrialInformationExtractor',
122+
constructorArgs: {
123+
filePath: path.join(__dirname, './fixtures/example-clinical-trial-info.csv'),
124+
},
125+
},
126+
],
127+
};
128+
129+
expect(() => new MCODEClient(testConfig)).toThrow();
130+
});
108131
});
109132
});

test/helpers/dependencyUtils.test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const _ = require('lodash');
21
const { sortExtractors } = require('../../src/helpers/dependencyUtils.js');
32

43
const WRONG_ORDER = [
@@ -26,15 +25,14 @@ const DEPENDENCY_INFO = [
2625

2726
describe('sortExtractors', () => {
2827
test('should put extractors in the correct order', () => {
29-
sortExtractors(WRONG_ORDER, DEPENDENCY_INFO);
30-
expect(WRONG_ORDER[0].type).toEqual('Extractor1');
31-
expect(WRONG_ORDER[1].type).toEqual('Extractor2');
32-
expect(WRONG_ORDER[2].type).toEqual('Extractor3');
28+
const sorted = sortExtractors(WRONG_ORDER, DEPENDENCY_INFO);
29+
expect(sorted[0].type).toEqual('Extractor1');
30+
expect(sorted[1].type).toEqual('Extractor2');
31+
expect(sorted[2].type).toEqual('Extractor3');
3332
});
3433

3534
test('should change nothing if all extractors are in order with all dependencies', () => {
36-
const unchanged = _.cloneDeep(NO_CHANGE);
37-
sortExtractors(unchanged, DEPENDENCY_INFO);
35+
const unchanged = sortExtractors(NO_CHANGE, DEPENDENCY_INFO);
3836
expect(unchanged).toEqual(NO_CHANGE);
3937
});
4038

0 commit comments

Comments
 (0)