Skip to content

Commit

Permalink
Add test cases for combination.
Browse files Browse the repository at this point in the history
Test forEach, forEachOrNull, unionAll, column and select expressions when applied on the same level.
Fix forEach with unionAll.

Add test for unionAll in combination with forEach(OrNull)

forEach(OrNull) combined with unionAll

add test with combination of select and column on the same level

format tests output
  • Loading branch information
mput committed Feb 20, 2024
1 parent 6b9562e commit d11b230
Show file tree
Hide file tree
Showing 17 changed files with 3,433 additions and 2,846 deletions.
8 changes: 6 additions & 2 deletions sof-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function normalize(def) {
def.type = 'forEach'

if (def.unionAll) {
def.select.unshift({unionAll: def.union})
def.select.unshift({unionAll: def.unionAll})
delete def.unionAll
}

Expand All @@ -162,7 +162,7 @@ function normalize(def) {
def.type = 'forEachOrNull'

if (def.unionAll) {
def.select.unshift({unionAll: def.union})
def.select.unshift({unionAll: def.unionAll})
delete def.unionAll
}

Expand Down Expand Up @@ -272,5 +272,9 @@ export function evaluate(def, node) {
}

const normal_def = normalize(structuredClone(def));

// console.log("======= NORM =========")
// console.dir(normal_def, {depth: null})

return node.flatMap(n => do_eval(normal_def, n, def))
}
19 changes: 19 additions & 0 deletions sof-js/tests/1_basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,24 @@ describe("basics", () => {
expect: [{id: 'pt1'}]
})

add_test({
title: 'select & column',
view: {
resource: 'Patient',
select: [
{
column: [{path: 'id', name: 'c_id'}],
select: [{column: [{path: 'id', name: 's_id'}]}]
}

]
},
expect: [
{c_id: 'pt1', s_id: 'pt1'},
{c_id: 'pt2', s_id: 'pt2'},
{c_id: 'pt3', s_id: 'pt3'}
]
});

end_case();
});
205 changes: 203 additions & 2 deletions sof-js/tests/3_foreach.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ let resources = [
{ system: 'phone' }
],
name: {
family: 'FC1.1',
given: ['N1', 'N1`']
}
},
{
telecom: [
{ system: 'email' }
],
name: { given: ['N2'] }
gender: "unknown",
name: {
family: 'FC1.2',
given: ['N2']
}
}
]
},
Expand Down Expand Up @@ -77,7 +82,7 @@ describe('foreach', () => {
})

add_test({
title: 'forEachOrNull: normal',
title: 'forEachOrNull: basic',
view: {
resource: 'Patient',
status: 'active',
Expand Down Expand Up @@ -114,6 +119,52 @@ describe('foreach', () => {
expect: []
})


add_test({
title: 'forEach: two on the same level',
view: {
resource: 'Patient',
status: 'active',
select: [
{
forEach: 'contact',
column: [{ name: 'cont_family', path: 'name.family' }]
},
{
forEach: 'name',
column: [{ name: 'pat_family' , path: 'family' }]
}
]
},
expect: [
{pat_family: 'F1.1', cont_family: 'FC1.1'},
{pat_family: 'F1.1', cont_family: 'FC1.2'},

{pat_family: 'F1.2', cont_family: 'FC1.1'},
{pat_family: 'F1.2', cont_family: 'FC1.2'}
]
})

add_test({
title: 'forEach: two on the same level (empty result)',
view: {
resource: 'Patient',
status: 'active',
select: [
{ column: [{ name: 'id', path: 'id' }] },
{
forEach: 'identifier',
column: [{ name: 'value', path: 'value' }]
},
{
forEach: 'name',
column: [{ name: 'family', path: 'family' }]
}
]
},
expect: []
})

add_test({
title: 'forEachOrNull: null case',
view: {
Expand All @@ -134,6 +185,31 @@ describe('foreach', () => {
]
})

add_test({
title: 'forEach and forEachOrNull on the same level',
view: {
resource: 'Patient',
status: 'active',
select: [
{ column: [{ name: 'id', path: 'id' }] },
{
forEachOrNull: 'identifier',
column: [{ name: 'value', path: 'value' }]
},
{
forEach: 'name',
column: [{ name: 'family', path: 'family' }]
}
]
},
expect: [
{ id: 'pt1', family: 'F1.1', value: null },
{ id: 'pt1', family: 'F1.2', value: null },
{ id: 'pt2', family: 'F2.1', value: null },
{ id: 'pt2', family: 'F2.2', value: null },
]
})

let nested_result = [
{ contact_type: "phone", name: "N1" , id: "pt1" },
{ contact_type: "phone", name: "N1`", id: "pt1" },
Expand Down Expand Up @@ -185,6 +261,131 @@ describe('foreach', () => {
expect: nested_result
});

add_test({
title: 'forEachOrNull & unionAll on the same level',
view: {
resource: 'Patient',
select: [
{
column: [{path: 'id', name: 'id'}],
},
{
forEachOrNull: "contact",
unionAll: [
{column: [{path: 'name.family', name: 'name'}]},
{forEach: "name.given",
column: [{path: '$this', name: 'name'}]}

]
}

]
},
expect: [
{id: 'pt1', name: 'FC1.1'},
{id: 'pt1', name: 'N1'},
{id: 'pt1', name: 'N1`'},
{id: 'pt1', name: 'FC1.2'},
{id: 'pt1', name: 'N2'},
{id: 'pt2', name: null},
{id: 'pt3', name: null},
]
})

add_test({
title: 'forEach & unionAll on the same level',
view: {
resource: 'Patient',
select: [
{
column: [{path: 'id', name: 'id'}],
},
{
forEach: "contact",
unionAll: [
{column: [{path: 'name.family', name: 'name'}]},
{forEach: "name.given",
column: [{path: '$this', name: 'name'}]}

]
}

]
},
expect: [
{id: 'pt1', name: 'FC1.1'},
{id: 'pt1', name: 'N1'},
{id: 'pt1', name: 'N1`'},
{id: 'pt1', name: 'FC1.2'},
{id: 'pt1', name: 'N2'},
]
})

add_test({
title: 'forEach & unionAll & column & select on the same level',
view: {
resource: 'Patient',
select: [
{
column: [{path: 'id', name: 'id'}],
},
{
forEach: "contact",
column: [{path: 'telecom.system', name: 'tel_system'}],
select: [{column: [{path: 'gender', name: 'gender'}]}],
unionAll: [
{column: [{path: 'name.family', name: 'name'}]},
{forEach: "name.given",
column: [{path: '$this', name: 'name'}]}

]
}

]
},
expect: [
{id: 'pt1', name: 'FC1.1', tel_system: "phone", gender: null},
{id: 'pt1', name: 'N1', tel_system: "phone", gender: null},
{id: 'pt1', name: 'N1`', tel_system: "phone", gender: null},
{id: 'pt1', name: 'FC1.2', tel_system: "email", gender: "unknown"},
{id: 'pt1', name: 'N2', tel_system: "email", gender: "unknown"},
]
})

add_test({
title: 'forEachOrNull & unionAll & column & select on the same level',
view: {
resource: 'Patient',
select: [
{
column: [{path: 'id', name: 'id'}],
},
{
forEachOrNull: "contact",
column: [{path: 'telecom.system', name: 'tel_system'}],
select: [{column: [{path: 'gender', name: 'gender'}]}],
unionAll: [
{column: [{path: 'name.family', name: 'name'}]},
{forEach: "name.given",
column: [{path: '$this', name: 'name'}]}

]
}

]
},
expect: [
{id: 'pt1', name: 'FC1.1', tel_system: "phone", gender: null},
{id: 'pt1', name: 'N1', tel_system: "phone", gender: null},
{id: 'pt1', name: 'N1`', tel_system: "phone", gender: null},
{id: 'pt1', name: 'FC1.2', tel_system: "email", gender: "unknown"},
{id: 'pt1', name: 'N2', tel_system: "email", gender: "unknown"},
{id: 'pt2', name: null, tel_system: null, gender: null},
{id: 'pt3', name: null, tel_system: null, gender: null},
]
})


end_case();

})
29 changes: 29 additions & 0 deletions sof-js/tests/4_union.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,35 @@ describe("union", () => {
]
});

add_test({
title: 'forEachOrNull and forEach',
view: {
resource: 'Patient',
status: 'active',
select: [
{
column: [{ name: 'id', path: 'id' }],
unionAll: [
{
forEach: 'name',
column: [{ name: 'given', path: 'given' }]
},
{
forEachOrNull: 'name',
column: [{ name: 'given', path: 'given' }]
}
]
}
]
},
expect: [
{ given: null, id: "pt1" },
{ given: null, id: "pt2" },
{ given: null, id: "pt3" },
{ given: null, id: "pt4" },
]
})

add_test({
title: 'nested',
view: {
Expand Down
47 changes: 0 additions & 47 deletions sof-js/tests/combination.test.js

This file was deleted.

Loading

0 comments on commit d11b230

Please sign in to comment.