Skip to content

Commit

Permalink
fix(core/models): SUM function fixed for repeating slide
Browse files Browse the repository at this point in the history
  • Loading branch information
sara-gnucoop authored and robzan8 committed Nov 10, 2022
1 parent cf182f6 commit 863d531
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
49 changes: 44 additions & 5 deletions projects/core/models/src/utils/expression-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import {ALL_VALUES_OF, SUM, MainForm, MEAN} from './expression-utils';
import {ALL_VALUES_OF, SUM, MainForm, MEAN, COUNT_FORMS} from './expression-utils';

const forms: MainForm[] = [
{dog_name: 'dog1', dog_puppies: 3, to_check: true, reps: {'rep_1': [{dog_type: 'dogtype1'}]}},
{
dog_name: 'dog1',
dog_puppies: 3,
to_check: true,
reps: {'rep_1': [{dog_type: 'dogtype1', score: 3}]},
},
{},
{dog_name: 'dog2', to_check: true, dog_puppies: 2, reps: {'rep_1': [{dog_type: 'dogtype1'}]}},
{dog_name: 'dog3', to_check: true, reps: {'rep_1': [{dog_type: 'dogtype1'}]}},
{dog_name: 'dog4', dog_puppies: 1, reps: {'rep_1': [{dog_type: 'dogtype2'}]}},
{
dog_name: 'dog2',
to_check: true,
dog_puppies: 2,
reps: {
'rep_1': [{dog_type: 'dogtype1', score: 6}],
'rep_2': [{dog_type: 'dogtype2', score: 4}],
},
},
{dog_name: 'dog3', to_check: true, reps: {'rep_1': [{dog_type: 'dogtype1', score: 5}]}},
{dog_name: 'dog4', dog_puppies: 1, reps: {'rep_1': [{dog_type: 'dogtype1', score: 1}]}},
];

describe('ALL_VALUES_OF', () => {
Expand All @@ -25,6 +38,32 @@ describe('SUM', () => {
const dog_puppies = 5;
expect(result).toBe(dog_puppies);
});

it('should sum the values of one field in repeating slide', () => {
const result = SUM(forms, 'score', 'to_check === true');
const score = 18;
expect(result).toBe(score);
});

it('should sum the values of one field in repeating slide with condition in rep slide', () => {
const result = SUM(forms, 'score', 'dog_type === "dogtype1"');
const score = 15;
expect(result).toBe(score);
});
});

describe('COUNT_FORMS', () => {
it('should count the forms in dataset with condition', () => {
const result = COUNT_FORMS(forms, 'to_check === true');
const count = 3;
expect(result).toBe(count);
});

it('should count the forms with condition in repeating slide', () => {
const result = COUNT_FORMS(forms, 'dog_type === "dogtype1"');
const count = 4;
expect(result).toBe(count);
});
});

describe('MEAN', () => {
Expand Down
15 changes: 14 additions & 1 deletion projects/core/models/src/utils/expression-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,20 @@ export function SUM(mainForms: (MainForm | Form)[], field: string, condition = '
const mainForm = forms[i];

if (evaluateExpression(condition, mainForm)) {
count += +(mainForm[field] as number) || 0;
if (field in mainForm && mainForm[field] != null) {
count += +(mainForm[field] as number) || 0;
} else {
if (mainForm.reps != null) {
const allreps: Form[] = Object.keys(mainForm.reps)
.map((key: string) => (mainForm.reps as Instances)[key])
.flat();
allreps
.filter(c => c[field] != null)
.forEach((child: Form) => {
count += +(child[field] as number) || 0;
});
}
}
} else {
if (mainForm.reps != null) {
const allreps: Form[] = Object.keys(mainForm.reps)
Expand Down

0 comments on commit 863d531

Please sign in to comment.