Skip to content

Commit

Permalink
test: improve rules engine coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru committed Nov 10, 2023
1 parent 1a127c2 commit 296c1be
Show file tree
Hide file tree
Showing 13 changed files with 5,222 additions and 5,427 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"@babel/preset-react": "^7.16.7",
"@badeball/cypress-cucumber-preprocessor": "17.2.1",
"@cypress/webpack-preprocessor": "^6.0.0",
"@dhis2/cli-app-scripts": "^9.0.1",
"@dhis2/cli-app-scripts": "^10.3.10",
"@dhis2/cli-helpers-engine": "^3.2.1",
"@dhis2/cli-style": "^10.4.1",
"@dhis2/cli-utils-cypress": "^9.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/rules-engine/src/RulesEngine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import log from 'loglevel';
import { VariableService } from './services/VariableService/VariableService';
import { VariableService } from './services/VariableService';
import { ValueProcessor } from './processors/ValueProcessor';
import { executeExpression } from './services/expressionService';
import { getD2Functions } from './d2Functions';
Expand Down
2 changes: 1 addition & 1 deletion packages/rules-engine/src/helpers/previousValueCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const getOutputEffectsWithPreviousValueCheck = ({
onProcessValue: (value: any, type: $Values<typeof typeKeys>) => any,
}) =>
outputEffects.reduce((acc, outputEffect) => {
if (formValues && outputEffect.targetDataType) {
if (formValues && Object.keys(formValues).length !== 0 && outputEffect.targetDataType) {
const formValue = formValues[outputEffect.id];
const rawValue = mapByTargetDataTypes[outputEffect.targetDataType]({
dataElementId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,26 +296,23 @@ export function getRulesEffectsProcessor(
formValues,
onProcessValue,
}: {
effects: ?Array<ProgramRuleEffect>,
effects: Array<ProgramRuleEffect>,
dataElements: ?DataElements,
trackedEntityAttributes: ?TrackedEntityAttributes,
formValues?: ?{ [key: string]: any },
onProcessValue: (value: any, type: $Values<typeof typeKeys>) => any,
}): OutputEffects {
if (effects) {
return effects
.filter(({ action }) => mapActionsToProcessor[action])
.flatMap(effect => mapActionsToProcessor[effect.action](
effect,
dataElements,
trackedEntityAttributes,
formValues,
onProcessValue,
))
// when mapActionsToProcessor function returns `null` we filter those value out.
.filter(keepTruthyValues => keepTruthyValues);
}
return [];
return effects
.filter(({ action }) => mapActionsToProcessor[action])
.flatMap(effect => mapActionsToProcessor[effect.action](
effect,
dataElements,
trackedEntityAttributes,
formValues,
onProcessValue,
))
// when mapActionsToProcessor function returns `null` we filter those value out.
.filter(keepTruthyValues => keepTruthyValues);
}

return processRulesEffects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const getStructureEvents = (compareDates: CompareDates) => {
event.eventId !== currentEvent.eventId,
);

const events = [...otherEventsFiltered, currentEvent]
const events = [...otherEventsFiltered, ...(Object.keys(currentEvent).length !== 0 ? [currentEvent] : [])]
.sort(compareEvents);

return createEventsContainer(events);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Object {
"trackedEntityAttributes": Object {
"lZGmxYbs96q": Object {
"id": "lZGmxYbs96q",
"optionSetId": undefined,
"optionSetId": "optionSet",
"valueType": "DATE",
},
"lZGmxYbs97q": Object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { variableSourceTypes } from '@dhis2/rules-engine-javascript';
import {
TrackerProgram,
ProgramStage,
Expand Down Expand Up @@ -122,6 +123,7 @@ describe('getApplicableRuleEffectsForTrackerProgram', () => {
element3.id = 'lZGmxYbs96q';
element3.name = 'SomeDate';
element3.type = dataElementTypes.DATE;
element3.optionSet = { id: 'optionSet', name: 'optionSet', code: 'optionSet' };
}),
new DataElement((element4) => {
element4.id = 'w75KJ2mc4zz';
Expand All @@ -136,23 +138,23 @@ describe('getApplicableRuleEffectsForTrackerProgram', () => {
displayName: 'Test',
id: 'PUQZWgmQ0jx',
programId: 'IpHINAT79UW',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
dataElementId: 'H6uSAMO5WLD',
displayName: 'apgarcomment',
id: 'aKpfPKSRQnv',
programId: 'IpHINAT79UW',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
dataElementId: 'a3kGcGDCuk6',
displayName: 'apgarscore',
id: 'g2GooOydipB',
programId: 'IpHINAT79UW',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
];
Expand Down Expand Up @@ -236,4 +238,36 @@ describe('getApplicableRuleEffectsForTrackerProgram', () => {

expect(Array.isArray(effects)).toBe(true);
});

test('RulesEngine called without programRules', () => {
const effects = getApplicableRuleEffectsForTrackerProgram({
program: new TrackerProgram((initProgram) => {
initProgram.programRules = [];
}),
stage: new ProgramStage((stage) => {
stage.programRules = [];
}),
orgUnit,
currentEvent,
otherEvents,
attributeValues,
enrollmentData,
});

expect(effects).toStrictEqual([]);
});

test('currentEvent without a programStageId', () => {
const effects = getApplicableRuleEffectsForTrackerProgram({
program,
stage: programStage,
orgUnit,
currentEvent: {},
otherEvents,
attributeValues,
enrollmentData,
});

expect(effects.DISPLAYTEXT).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { variableSourceTypes } from '@dhis2/rules-engine-javascript';
import {
EventProgram,
ProgramStage,
Expand Down Expand Up @@ -78,23 +79,23 @@ describe('getApplicableRuleEffectsForEventProgram', () => {
displayName: 'Test',
id: 'PUQZWgmQ0jx',
programId: 'IpHINAT79UW',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
dataElementId: 'H6uSAMO5WLD',
displayName: 'apgarcomment',
id: 'aKpfPKSRQnv',
programId: 'IpHINAT79UW',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
dataElementId: 'a3kGcGDCuk6',
displayName: 'apgarscore',
id: 'g2GooOydipB',
programId: 'IpHINAT79UW',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
];
Expand Down Expand Up @@ -146,4 +147,16 @@ describe('getApplicableRuleEffectsForEventProgram', () => {

expect(effects.DISPLAYTEXT).toBeDefined();
});

test('RulesEngine called without programRules', () => {
const effects = getApplicableRuleEffectsForEventProgram({
program: new EventProgram((initProgram) => {
initProgram.programRules = [];
}),
orgUnit,
currentEvent,
});

expect(effects).toStrictEqual([]);
});
});
Loading

0 comments on commit 296c1be

Please sign in to comment.