Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: [TECH-887] improve rules engine unit test coverage #3462

Merged
merged 8 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [];
superskip marked this conversation as resolved.
Show resolved Hide resolved
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]
superskip marked this conversation as resolved.
Show resolved Hide resolved
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
Loading