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 8, 2023
1 parent 08d90aa commit bcff5ea
Show file tree
Hide file tree
Showing 10 changed files with 3,622 additions and 5,452 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 @@ -38,7 +38,7 @@ export const getStructureEvents = (compareDates: CompareDates) => {
event.eventId !== currentEvent.eventId,
);

const events = [...otherEventsFiltered, ...(Object.keys(currentEvent).length !== 0 ? [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
@@ -1,3 +1,4 @@
import { variableSourceTypes } from '@dhis2/rules-engine-javascript';
import {
TrackerProgram,
ProgramStage,
Expand Down Expand Up @@ -136,23 +137,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
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rulesEngineEffectTargetDataTypes } from '@dhis2/rules-engine-javascript';
import { rulesEngineEffectTargetDataTypes, variableSourceTypes } from '@dhis2/rules-engine-javascript';
import { rulesEngine } from '../rulesEngine';

describe('Event Event rules engine', () => {
Expand Down Expand Up @@ -62,15 +62,15 @@ describe('Event Event rules engine', () => {
dataElementId: 'sWoqcoByYmD',
displayName: 'womanSmoking',
programId: 'lxAQ7Zs9VYR',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
id: 'omrL0gtPpDL',
dataElementId: 'vANAXwtLwcT',
displayName: 'hemoglobin',
programId: 'lxAQ7Zs9VYR',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
];
Expand Down Expand Up @@ -222,35 +222,35 @@ describe('Event rules engine', () => {
dataElementId: 'qrur9Dvnyt5',
displayName: 'age',
programId: 'eBAyeGv0exc',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
id: 'zINGRka3g9N',
dataElementId: 'oZg33kd9taw',
displayName: 'gender',
programId: 'eBAyeGv0exc',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
id: 'Zj7UnCAulEk.vV9UWAZohSf',
displayName: 'Zj7UnCAulEk.vV9UWAZohSf',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
dataElementId: 'vV9UWAZohSf',
programId: 'eBAyeGv0exc',
},
{
id: 'Zj7UnCAulEk.GieVkTxp4HH',
displayName: 'Zj7UnCAulEk.GieVkTxp4HH',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
dataElementId: 'GieVkTxp4HH',
programId: 'eBAyeGv0exc',
},
{
id: 'Zj7UnCAulEk.GieVkTxp4HH',
displayName: 'Zj7UnCAulEk.GieVkTxp4HH',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
dataElementId: 'GieVkTxp4HH',
programId: 'eBAyeGv0exc',
},
Expand All @@ -262,10 +262,7 @@ describe('Event rules engine', () => {
displayName: 'Gender',
version: 0,
valueType: 'TEXT',
options: [
{ id: 'rBvjJYbMCVx', displayName: 'Male', code: 'Male', translations: [] },
{ id: 'Mnp3oXrpAbK', displayName: 'Female', code: 'Female', translations: [] },
],
options: undefined,
},
};

Expand Down Expand Up @@ -706,30 +703,30 @@ describe('Event rules engine', () => {
dataElementId: 'f8j4XDEozvj',
displayName: 'INFECTION_SOURCE',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: false,
},
{
id: 'EnpvdmYrwLb',
dataElementId: 'TzqawmlPkI5',
displayName: 'TRAVEL_HISTORY',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'JPIyrAmJapV',
dataElementId: 'CUbZcLm9LyN',
displayName: 'HOSPITALISED',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'LAaPMTz69L7',
displayName: 'CASE_CLASSIFICATION',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'CALCULATED_VALUE',
programRuleVariableSourceType: variableSourceTypes.CALCULATED_VALUE,
useNameForOptionSet: true,
valueType: 'TEXT',
},
Expand All @@ -738,39 +735,39 @@ describe('Event rules engine', () => {
dataElementId: 'ovY6E8BSdto',
displayName: 'TEST_RESULT',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'XcPYCpTOPwB',
dataElementId: 'QQLXTXVidW2',
displayName: 'LAB_TEST',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'cZSslcAEupI',
dataElementId: 's3eoonJ8OJb',
displayName: 'ONSET_DATE',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'eSq3nc1t2F6',
dataElementId: 'dyfYIsTFTjG',
displayName: 'PATIENT_ID',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: true,
},
{
id: 'lY0yJGU1D4e',
dataElementId: 'JGnHr6WI3AY',
displayName: 'SYMPTOMS',
programId: 'PNClHaZARtz',
programRuleVariableSourceType: 'DATAELEMENT_CURRENT_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_CURRENT_EVENT,
useNameForOptionSet: false,
},
];
Expand Down Expand Up @@ -812,7 +809,6 @@ describe('Event rules engine', () => {
},
{
id: 'pqxvAQU1z9W',
displayName: 'Unknown',
code: 'Unknown',
translations: [
{ property: 'NAME', locale: 'es', value: 'DEsconocido' },
Expand All @@ -834,7 +830,6 @@ describe('Event rules engine', () => {
options: [
{
id: 'B44lkxTWbGO',
displayName: 'Inconclusive',
code: 'Inconclusive',
translations: [
{ property: 'NAME', locale: 'fr', value: 'Non concluant' },
Expand Down Expand Up @@ -1808,28 +1803,28 @@ describe('Event rules engine effects with functions and effects', () => {
dataElementId: 'qrur9Dvnyt5',
displayName: 'age',
programId: 'eBAyeGv0exc',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
id: 'zINGRka3g9N',
dataElementId: 'oZg33kd9taw',
displayName: 'gender',
programId: 'eBAyeGv0exc',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
{
id: 'Zj7UnCAulEk.vV9UWAZohSf',
displayName: 'Zj7UnCAulEk.vV9UWAZohSf',
programRuleVariableSourceType: 'DATAELEMENT_PREVIOUS_EVENT',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_PREVIOUS_EVENT,
dataElementId: 'vV9UWAZohSf',
programId: 'eBAyeGv0exc',
},
{
id: 'Zj7UnCAulEk.GieVkTxp4HH',
displayName: 'Zj7UnCAulEk.GieVkTxp4HH',
programRuleVariableSourceType: 'TEI_ATTRIBUTE',
programRuleVariableSourceType: variableSourceTypes.TEI_ATTRIBUTE,
dataElementId: 'GieVkTxp4HH',
programId: 'eBAyeGv0exc',
},
Expand Down Expand Up @@ -2622,7 +2617,7 @@ describe('Assign effects', () => {
dataElementId: 'qrur9Dvnyt5',
displayName: 'age',
programId: 'eBAyeGv0exc',
programRuleVariableSourceType: 'DATAELEMENT_NEWEST_EVENT_PROGRAM',
programRuleVariableSourceType: variableSourceTypes.DATAELEMENT_NEWEST_EVENT_PROGRAM,
useNameForOptionSet: true,
},
];
Expand Down
Loading

0 comments on commit bcff5ea

Please sign in to comment.