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 2, 2023
1 parent c86d002 commit 23b1d3e
Show file tree
Hide file tree
Showing 5 changed files with 1,039 additions and 27 deletions.
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 @@ -1822,21 +1822,21 @@ describe('Event rules engine effects with functions and effects', () => {
{
id: 'Zj7UnCAulEk.vV9UWAZohSf',
displayName: 'Zj7UnCAulEk.vV9UWAZohSf',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: 'DATAELEMENT_PREVIOUS_EVENT',
dataElementId: 'vV9UWAZohSf',
programId: 'eBAyeGv0exc',
},
{
id: 'Zj7UnCAulEk.GieVkTxp4HH',
displayName: 'Zj7UnCAulEk.GieVkTxp4HH',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: 'TEI_ATTRIBUTE',
dataElementId: 'GieVkTxp4HH',
programId: 'eBAyeGv0exc',
},
{
id: 'Zj7UnCAulEk.GieVkTxp4HH',
displayName: 'Zj7UnCAulEk.GieVkTxp4HH',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: 'UNKNOWN',
dataElementId: 'GieVkTxp4HH',
programId: 'eBAyeGv0exc',
},
Expand Down Expand Up @@ -2536,3 +2536,108 @@ describe('Event rules engine', () => {
});
});
});

describe('Assign effects', () => {
// these variables are shared between each test
const constants = [];
const dataElementsInProgram = {
qrur9Dvnyt5: { id: 'qrur9Dvnyt5', valueType: 'NUMBER' },
oZg33kd9taw: { id: 'oZg33kd9taw', valueType: 'BOOLEAN' },
oZ3fhkd9taw: { id: 'oZ3fhkd9taw', valueType: 'UNKNOWN' },
};
const orgUnit = { id: 'DiszpKrYNg8', name: 'Ngelehun CHC' };
const optionSets = {};
const currentEvent = {};

test('Assign effect corner cases', () => {
const programRules = [
{
id: 'cq1dwUY4lVU',
condition: 'true',
displayName: 'testing assign actions',
programId: 'eBAyeGv0exc',
programRuleActions: [
{
id: 'lJOYxhjupx2',
data: "'string'",
dataElementId: 'qrur9Dvnyt5',
programRuleActionType: 'ASSIGN',
},
{ id: 'lJOYxkjupx2', data: "'10'", dataElementId: 'qrur9Dvnyt5', programRuleActionType: 'ASSIGN' },
{
id: 'lJhYxhjupxz',
data: "'string'",
dataElementId: 'oZg33kd9taw',
programRuleActionType: 'ASSIGN',
},
{
id: 'lJfYxhjupxz',
data: "'string'",
dataElementId: 'oZ3fhkd9taw',
programRuleActionType: 'ASSIGN',
},
],
},
];
const programRuleVariables = [];
// when
const rulesEffects = rulesEngine.getProgramRuleEffects({
programRulesContainer: { programRuleVariables, programRules, constants },
currentEvent,
dataElements: dataElementsInProgram,
selectedOrgUnit: orgUnit,
optionSets,
});

// then
expect(rulesEffects).toEqual([
{ type: 'ASSIGN', id: 'qrur9Dvnyt5', value: null, targetDataType: 'dataElement' },
{ type: 'ASSIGN', id: 'qrur9Dvnyt5', value: '10', targetDataType: 'dataElement' },
{ type: 'ASSIGN', id: 'oZg33kd9taw', value: 'false', targetDataType: 'dataElement' },
{ type: 'ASSIGN', id: 'oZ3fhkd9taw', value: '', targetDataType: 'dataElement' },
]);
});

test('Assign effect with the program rule variable id found in the content key', () => {
const programRules = [
{
id: 'cq1dwUY4lVU',
condition: 'true',
displayName: 'testing assign actions',
programId: 'eBAyeGv0exc',
programRuleActions: [
{
id: 'lJOYxhjupx2',
data: 'rowExpresion',
dataElementId: 'qrur9Dvnyt5',
programRuleActionType: 'ASSIGN',
content: 'Hemoglobin value lower than normal RycV5uDi66i',
},
],
},
];
const programRuleVariables = [
{
id: 'RycV5uDi66i',
dataElementId: 'qrur9Dvnyt5',
displayName: 'age',
programId: 'eBAyeGv0exc',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
useNameForOptionSet: true,
},
];
// when
const rulesEffects = rulesEngine.getProgramRuleEffects({
programRulesContainer: { programRuleVariables, programRules, constants },
currentEvent,
dataElements: dataElementsInProgram,
selectedOrgUnit: orgUnit,
optionSets,
});

// then
expect(rulesEffects).toEqual([
{ type: 'ASSIGN', id: 'qrur9Dvnyt5', value: 'false', targetDataType: 'dataElement' },
]);
});
});
Loading

0 comments on commit 23b1d3e

Please sign in to comment.