Skip to content

Commit

Permalink
[#198] Assert the result of 'where.path' to be boolean.
Browse files Browse the repository at this point in the history
Note: An empty collection returned from the 'where.path' expression is also considered an error.
  • Loading branch information
mput committed Feb 20, 2024
1 parent d11b230 commit 1f21bf1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
4 changes: 3 additions & 1 deletion sof-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ function select(select_expr, node, def) {
assert(select_expr.select, 'select')
if(select_expr.where) {
let include = select_expr.where.every((w)=>{
return fhirpath_evaluate(node, w.path, def.constant)[0]
const val = fhirpath_evaluate(node, w.path, def.constant)[0]
assert(typeof val === "boolean", "'where' expression path should return 'boolean'")
return val
})
if(!include) { return []}
}
Expand Down
12 changes: 6 additions & 6 deletions sof-js/tests/1_basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe("basics", () => {
resource: 'Patient',
status: 'active',
select: [{column: [{name: 'id', path: 'id'}]}],
where: [{path: 'active = true'}]
where: [{path: 'active.exists() and active = true'}]
},
expect: [{id: 'pt1'}]
})
Expand All @@ -164,13 +164,13 @@ describe("basics", () => {
resource: 'Patient',
status: 'active',
select: [{column: [{name: 'id', path: 'id'}]}],
where: [{path: 'active = false'}]
where: [{path: 'active.exists() and active = false'}]
},
expect: [{id: 'pt2'}]
})

add_test({
title: 'where as element',
add_throwing_test({
title: 'where returns non-boolean for some cases',
view: {
resource: 'Patient',
status: 'active',
Expand All @@ -186,7 +186,7 @@ describe("basics", () => {
resource: 'Patient',
status: 'active',
select: [{column: [{name: 'id', path: 'id'}]}],
where: [{path: "name.family = 'F2'"}]
where: [{path: "name.family.exists() and name.family = 'F2'"}]
},
expect: [{id: 'pt2'}]
})
Expand All @@ -197,7 +197,7 @@ describe("basics", () => {
resource: 'Patient',
status: 'active',
select: [{column: [{name: 'id', path: 'id'}]}],
where: [{path: "name.family = 'F1'"}]
where: [{path: "name.family.exists() and name.family = 'F1'"}]
},
expect: [{id: 'pt1'}]
})
Expand Down
4 changes: 2 additions & 2 deletions sof-js/tests/5_constants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("constant", () => {
column: [{ name: "id", path: "id" }]
}
],
where: [{ path: "name.where(use = %name_use)" }]
where: [{ path: "name.where(use = %name_use).exists()" }]
},
expect: [{ id: "pt1" }]
});
Expand Down Expand Up @@ -168,7 +168,7 @@ describe("constant", () => {
column: [{ name: "id", path: "id" }]
}
],
where: [{ path: "deceased.ofType(boolean) = %is_deceased" }]
where: [{ path: "deceased.ofType(boolean).exists() and deceased.ofType(boolean) = %is_deceased" }]
},
expect: [{ id: "pt2" }]
});
Expand Down
5 changes: 4 additions & 1 deletion sof-js/tests/validation.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { describe } from "bun:test";
import { start_case, end_case, invalid_view, debug } from './test_helpers.js'
import { start_case, end_case, invalid_view, debug, add_throwing_test } from './test_helpers.js'


let resources = [
{
resourceType: 'Patient',
name: [
{ family: 'F1.1' },
],
id: 'pt1'
},
{
Expand Down
10 changes: 5 additions & 5 deletions tests/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
],
"where": [
{
"path": "active = true"
"path": "active.exists() and active = true"
}
]
},
Expand All @@ -209,7 +209,7 @@
],
"where": [
{
"path": "active = false"
"path": "active.exists() and active = false"
}
]
},
Expand All @@ -220,7 +220,7 @@
]
},
{
"title": "where as element",
"title": "where returns non-boolean for some cases",
"view": {
"resource": "Patient",
"status": "active",
Expand Down Expand Up @@ -263,7 +263,7 @@
],
"where": [
{
"path": "name.family = 'F2'"
"path": "name.family.exists() and name.family = 'F2'"
}
]
},
Expand All @@ -290,7 +290,7 @@
],
"where": [
{
"path": "name.family = 'F1'"
"path": "name.family.exists() and name.family = 'F1'"
}
]
},
Expand Down
4 changes: 2 additions & 2 deletions tests/constant.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
],
"where": [
{
"path": "name.where(use = %name_use)"
"path": "name.where(use = %name_use).exists()"
}
]
},
Expand Down Expand Up @@ -244,7 +244,7 @@
],
"where": [
{
"path": "deceased.ofType(boolean) = %is_deceased"
"path": "deceased.ofType(boolean).exists() and deceased.ofType(boolean) = %is_deceased"
}
]
},
Expand Down
36 changes: 32 additions & 4 deletions tests/validate.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"resources": [
{
"resourceType": "Patient",
"name": [
{
"family": "F1.1"
}
],
"id": "pt1"
},
{
Expand All @@ -15,7 +20,7 @@
{
"title": "empty",
"view": {},
"error": "structure"
"expectError": true
},
{
"title": "wrong fhirpath",
Expand All @@ -28,10 +33,10 @@
}
]
},
"error": "fhirpath"
"expectError": true
},
{
"title": "wrong type",
"title": "wrong type in forEach",
"view": {
"resource": "Patient",
"status": "active",
Expand All @@ -41,7 +46,30 @@
}
]
},
"error": "structure"
"expectError": true
},
{
"title": "where with path resolving to not boolean",
"view": {
"resource": "Patient",
"status": "active",
"select": [
{
"column": [
{
"name": "id",
"path": "id"
}
]
}
],
"where": [
{
"path": "name.family"
}
]
},
"expectError": true
}
]
}

0 comments on commit 1f21bf1

Please sign in to comment.