Skip to content

Commit

Permalink
add more tests for function combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
Yngwarr committed Feb 28, 2024
1 parent 6b9562e commit 2c1cd11
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 19 deletions.
17 changes: 7 additions & 10 deletions sof-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ export function merge(a,b) {
}

export function row_product(parts) {
if(parts.length == 1) {return parts[0]}
if (parts.length == 1) {
return parts[0];
}

let rows = [{}];
let new_rows = null;
parts.forEach((partial_rows) => {

parts.forEach(partial_rows => {
new_rows = [];
partial_rows.forEach((partial_row)=> {
rows.forEach((row)=> {
rows.forEach(row => {
new_rows.push(merge(partial_row,row))
})
})
Expand Down Expand Up @@ -129,10 +133,6 @@ function select(select_expr, node, def) {
)
}

function compile(def) {
throw new Error('not impl');
}

// * foreach / column / [select(..)] -> foreach select[column, ..]
// * foreach / union / [select(..)] -> foreach select[union, ..]
// * foreach / select(..) -> foreach select[..]
Expand Down Expand Up @@ -244,18 +244,15 @@ function collect_columns(acc, def){
return def.select.reduce((acc, s)=> {
return collect_columns(acc, s);
}, acc)
break;
case 'unionAll':
return def.unionAll.reduce((acc, s)=> {
return collect_columns(acc, s);
}, acc)
break;
case 'column':
return def.column.reduce((acc, c)=> {
acc.push(c.name || c.path)
return acc
}, acc)
break;
default:
return acc;
}
Expand Down
99 changes: 90 additions & 9 deletions sof-js/tests/combination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,112 @@ start_case('Combinations', 'TBD', resources)
describe("combinations", () => {

add_test({
title: 'select & column',
title: 'select',
view: {
resource: 'Patient',
status: 'active',
select: [{column: [{path: 'id', name: 'id'}]}]
select: [{
select: [{column: [{path: 'id', name: 'id'}]}]
}]
},
expect: [{id: 'pt1'}, {id: 'pt2'}, {id: 'pt3'}]
});

add_test({
title: 'top level column',
title: 'column + select',
view: {
resource: 'Patient',
status: 'active',
select: [{column: [{path: 'id', name: 'id'}]}]
select: [{
column: [{path: 'id', name: 'column_id'}],
select: [
{
column: [{path: 'id', name: 'select_id'}]
}
]
}]
},
expect: [{id: 'pt1'}, {id: 'pt2'}, {id: 'pt3'}]
expect: [
{ column_id: "pt1", select_id: "pt1" },
{ column_id: "pt1", select_id: "pt2" },
{ column_id: "pt1", select_id: "pt3" },
{ column_id: "pt2", select_id: "pt1" },
{ column_id: "pt2", select_id: "pt2" },
{ column_id: "pt2", select_id: "pt3" },
{ column_id: "pt3", select_id: "pt1" },
{ column_id: "pt3", select_id: "pt2" },
{ column_id: "pt3", select_id: "pt3" },
]
});

add_test({
title: 'sibling select',
view: {
resource: 'Patient',
select: [
{ column: [{path: 'id', name: 'id_1'}] },
{ column: [{path: 'id', name: 'id_2'}] }
]
},
expect: [
{ id_1: "pt1", id_2: "pt1" },
{ id_1: "pt1", id_2: "pt2" },
{ id_1: "pt1", id_2: "pt3" },
{ id_1: "pt2", id_2: "pt1" },
{ id_1: "pt2", id_2: "pt2" },
{ id_1: "pt2", id_2: "pt3" },
{ id_1: "pt3", id_2: "pt1" },
{ id_1: "pt3", id_2: "pt2" },
{ id_1: "pt3", id_2: "pt3" },
]
});

add_test({
title: 'sibling select inside a select',
view: {
resource: 'Patient',
select: [{
select: [
{ column: [{path: 'id', name: 'id_1'}] },
{ column: [{path: 'id', name: 'id_2'}] }
]
}]
},
expect: [
{ id_1: "pt1", id_2: "pt1" },
{ id_1: "pt1", id_2: "pt2" },
{ id_1: "pt1", id_2: "pt3" },
{ id_1: "pt2", id_2: "pt1" },
{ id_1: "pt2", id_2: "pt2" },
{ id_1: "pt2", id_2: "pt3" },
{ id_1: "pt3", id_2: "pt1" },
{ id_1: "pt3", id_2: "pt2" },
{ id_1: "pt3", id_2: "pt3" },
]
});

add_test({
title: 'column + select, with where',
view: {
resource: 'Patient',
select: [{
column: [{path: 'id', name: 'column_id'}],
select: [
{ column: [{path: 'id', name: 'select_id'}] }
]
}],
where: [{ path: "id = 'pt1'" }]
},
expect: [
{ column_id: "pt1", select_id: "pt1" }
]
});

add_test({
title: 'select & select & column',
title: 'unionAll + forEach + column + select',
view: {
resource: 'Patient',
select: [{select: [{column: [{path: 'id', name: 'id'}]}]}]
select: [{
select: [{column: [{path: 'id', name: 'id'}]}]
}]
},
expect: [{id: 'pt1'}, {id: 'pt2'}, {id: 'pt3'}]
});
Expand Down

0 comments on commit 2c1cd11

Please sign in to comment.