diff --git a/sof-js/tests/1_basic.test.js b/sof-js/tests/1_basic.test.js index 1a0933c..a4643ab 100644 --- a/sof-js/tests/1_basic.test.js +++ b/sof-js/tests/1_basic.test.js @@ -1,94 +1,37 @@ -import { expect, test , describe, beforeAll, afterAll} from "bun:test"; -import { evaluate, get_columns, row_product } from '../src/index.js' -import { start_case, end_case, add_test, debug, should_fail, add_throwing_test } from './test_helpers.js' - -test("row_product", () => { - expect(row_product([[{a: 1}, {a: 2}], [{b: 1}, {b: 2}]])) - .toEqual([{b: 1, a: 1}, {b: 1, a: 2}, {b: 2, a: 1}, {b: 2, a: 2}]) - - expect(row_product([[{b: 1}, {b: 2}] , [{a: 1}, {a: 2}]])) - .toEqual([ - {a: 1, b: 1}, - {a: 1, b: 2}, - {a: 2, b: 1}, - {a: 2, b: 2} - ]) - - expect(row_product([[{a: 1}, {a: 2}], []])) - .toEqual([]); - - expect(row_product([[{a: 1}, {a: 2}], [{}]])) - .toEqual([{a: 1}, {a: 2}]) - - expect(row_product([[{a: 1}, {a: 2}]])) - .toEqual([{a: 1}, {a: 2}]) - -}); - -test('columns', ()=>{ - - expect(get_columns( - { - select: [ - {column: [{name: 'id', path: 'id'}]}, - {forEach: 'contact', - column: [{name: 'contact_type', path: 'type'}], - select: [ - {forEach: 'person', column: [{name: 'name', path: 'name'}]} - ]} - ] - } - )).toEqual(['id', 'contact_type', 'name']) - - expect(get_columns( - {select: [ - {column: [ - {path: 'id'}, - {path: 'birthDate'}]}, - {forEach: 'name', - column:[ - {path: "family", name: 'last_name'}, - {path: "given.join(' ')", name: 'first_name'}]} - ]})).toEqual(['id', 'birthDate', 'last_name', 'first_name']) - -}) +import { describe } from 'bun:test' +import { start_case, end_case, add_test } from './test_helpers.js' let resources = [ { resourceType: 'Patient', id: 'pt1', - name: [ { family: 'F1' }], - active: true + name: [{ family: 'F1' }], + active: true, }, { resourceType: 'Patient', id: 'pt2', - name: [ { family: 'F2' }], - active: false + name: [{ family: 'F2' }], + active: false, }, { resourceType: 'Patient', - id: 'pt3' + id: 'pt3', }, ] start_case('basic', 'basic view definition', resources) -describe("basics", () => { +describe('basics', () => { add_test({ title: 'basic attribute', view: { resource: 'Patient', status: 'active', - select: [ - { column: [{ name: 'id', path: 'id' }]} - ] + select: [{ column: [{ name: 'id', path: 'id' }] }], }, - expect: [ - {id: 'pt1'}, - {id: 'pt2'}, - {id: 'pt3'} - ]}); + expect: [{ id: 'pt1' }, { id: 'pt2' }, { id: 'pt3' }], + }) add_test({ title: 'boolean attribute with false', @@ -98,23 +41,23 @@ describe("basics", () => { select: [ { column: [ - {name: 'id', path: 'id'}, - {name: 'active', path: 'active'} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'active', path: 'active' }, + ], + }, + ], }, expect: [ - {id: 'pt1', active: true}, - {id: 'pt2', active: false}, - {id: 'pt3', active: null} - ] + { id: 'pt1', active: true }, + { id: 'pt2', active: false }, + { id: 'pt3', active: null }, + ], }) let expected = [ - {id: 'pt1', last_name: 'F1'}, - {id: 'pt2', last_name: 'F2'}, - {id: 'pt3', last_name: null} + { id: 'pt1', last_name: 'F1' }, + { id: 'pt2', last_name: 'F2' }, + { id: 'pt3', last_name: null }, ] add_test({ @@ -125,13 +68,13 @@ describe("basics", () => { select: [ { column: [ - {name: 'id', path: 'id'}, - {name: 'last_name', path: 'name.family.first()'} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'last_name', path: 'name.family.first()' }, + ], + }, + ], }, - expect: expected + expect: expected, }) add_test({ @@ -140,11 +83,11 @@ describe("basics", () => { resource: 'Patient', status: 'active', select: [ - {column: [{name: 'id', path: 'id'}]}, - {column: [{name: 'last_name', path: 'name.family.first()'}]} - ] + { column: [{ name: 'id', path: 'id' }] }, + { column: [{ name: 'last_name', path: 'name.family.first()' }] }, + ], }, - expect: expected + expect: expected, }) add_test({ @@ -152,10 +95,10 @@ describe("basics", () => { view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'id', path: 'id'}]}], - where: [{path: 'active.exists() and active = true'}] + select: [{ column: [{ name: 'id', path: 'id' }] }], + where: [{ path: 'active.exists() and active = true' }], }, - expect: [{id: 'pt1'}] + expect: [{ id: 'pt1' }], }) add_test({ @@ -163,10 +106,10 @@ describe("basics", () => { view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'id', path: 'id'}]}], - where: [{path: 'active.exists() and active = false'}] + select: [{ column: [{ name: 'id', path: 'id' }] }], + where: [{ path: 'active.exists() and active = false' }], }, - expect: [{id: 'pt2'}] + expect: [{ id: 'pt2' }], }) add_test({ @@ -174,10 +117,10 @@ describe("basics", () => { view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'id', path: 'id'}]}], - where: [{path: 'active'}] + select: [{ column: [{ name: 'id', path: 'id' }] }], + where: [{ path: 'active' }], }, - expect: [{id: 'pt1'}] + expect: [{ id: 'pt1' }], }) add_test({ @@ -185,10 +128,10 @@ describe("basics", () => { view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'id', path: 'id'}]}], - where: [{path: "name.family.exists() and name.family = 'F2'"}] + select: [{ column: [{ name: 'id', path: 'id' }] }], + where: [{ path: "name.family.exists() and name.family = 'F2'" }], }, - expect: [{id: 'pt2'}] + expect: [{ id: 'pt2' }], }) add_test({ @@ -196,10 +139,10 @@ describe("basics", () => { view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'id', path: 'id'}]}], - where: [{path: "name.family.exists() and name.family = 'F1'"}] + select: [{ column: [{ name: 'id', path: 'id' }] }], + where: [{ path: "name.family.exists() and name.family = 'F1'" }], }, - expect: [{id: 'pt1'}] + expect: [{ id: 'pt1' }], }) add_test({ @@ -208,18 +151,17 @@ describe("basics", () => { resource: 'Patient', select: [ { - column: [{path: 'id', name: 'c_id'}], - select: [{column: [{path: 'id', name: 's_id'}]}] - } - - ] + 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'} - ] - }); + { c_id: 'pt1', s_id: 'pt1' }, + { c_id: 'pt2', s_id: 'pt2' }, + { c_id: 'pt3', s_id: 'pt3' }, + ], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/2_fhirpath.test.js b/sof-js/tests/2_fhirpath.test.js index 1085461..81011b6 100644 --- a/sof-js/tests/2_fhirpath.test.js +++ b/sof-js/tests/2_fhirpath.test.js @@ -1,8 +1,5 @@ -import { expect, test, describe } from "bun:test"; -import { evaluate, row_product } from '../src/index.js' -import { start_case, end_case, add_test, should_fail } from './test_helpers.js' - -let l = console.log +import { describe } from 'bun:test' +import { start_case, end_case, add_test } from './test_helpers.js' let resources = [ { @@ -13,14 +10,14 @@ let resources = [ { family: 'f1.1', use: 'official', - given: ['g1.1.1', 'g1.1.2'] + given: ['g1.1.1', 'g1.1.2'], }, { family: 'f1.2', - given: ['g1.2.1'] - } + given: ['g1.2.1'], + }, ], - active: true + active: true, }, { resourceType: 'Patient', @@ -30,29 +27,28 @@ let resources = [ { family: 'f2.1' }, { family: 'f2.2', - use: 'official' - } + use: 'official', + }, ], - active: false + active: false, }, { resourceType: 'Patient', - id: 'pt3' + id: 'pt3', }, ] start_case('fhirpath', 'fhirpath features', resources) -describe("fhirpath", () => { - +describe('fhirpath', () => { add_test({ title: 'one element', view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'id', path: 'id'}]}] + select: [{ column: [{ name: 'id', path: 'id' }] }], }, - expect: [{id: 'pt1'}, {id: 'pt2'}, {id: 'pt3'}] + expect: [{ id: 'pt1' }, { id: 'pt2' }, { id: 'pt3' }], }) add_test({ @@ -60,9 +56,9 @@ describe("fhirpath", () => { view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'v', path: 'name.family.first()'}]}] + select: [{ column: [{ name: 'v', path: 'name.family.first()' }] }], }, - expect: [{v: 'f1.1'}, {v: 'f2.1'}, {v: null}] + expect: [{ v: 'f1.1' }, { v: 'f2.1' }, { v: null }], }) add_test({ @@ -70,13 +66,9 @@ describe("fhirpath", () => { view: { resource: 'Patient', status: 'active', - select: [{column: [{name: 'v', path: 'name.family', collection: true}]}] + select: [{ column: [{ name: 'v', path: 'name.family', collection: true }] }], }, - expect: [ - {v: ['f1.1', 'f1.2']}, - {v: ['f2.1','f2.2']}, - {v: []} - ] + expect: [{ v: ['f1.1', 'f1.2'] }, { v: ['f2.1', 'f2.2'] }, { v: [] }], }) add_test({ @@ -84,15 +76,9 @@ describe("fhirpath", () => { view: { resource: 'Patient', status: 'active', - select: [ - { column: [ {name: 'v', path: 'name[0].family'} ] } - ] + select: [{ column: [{ name: 'v', path: 'name[0].family' }] }], }, - expect: [ - {v: 'f1.1'}, - {v: 'f2.1'}, - {v: null} - ] + expect: [{ v: 'f1.1' }, { v: 'f2.1' }, { v: null }], }) add_test({ @@ -100,15 +86,9 @@ describe("fhirpath", () => { view: { resource: 'Patient', status: 'active', - select: [ - { column: [ {name: 'v', path: 'name[1].family'} ] } - ] + select: [{ column: [{ name: 'v', path: 'name[1].family' }] }], }, - expect: [ - {v: 'f1.2'}, - {v: 'f2.2'}, - {v: null} - ] + expect: [{ v: 'f1.2' }, { v: 'f2.2' }, { v: null }], }) add_test({ @@ -116,15 +96,9 @@ describe("fhirpath", () => { view: { resource: 'Patient', status: 'active', - select: [ - { column: [ {name: 'v', path: 'name[2].family'} ] } - ] + select: [{ column: [{ name: 'v', path: 'name[2].family' }] }], }, - expect: [ - {v: null}, - {v: null}, - {v: null} - ] + expect: [{ v: null }, { v: null }, { v: null }], }) add_test({ @@ -132,15 +106,9 @@ describe("fhirpath", () => { view: { resource: 'Patient', status: 'active', - select: [ - { column: [ {name: 'v', path: "name.where(use='official').family"} ] } - ] + select: [{ column: [{ name: 'v', path: "name.where(use='official').family" }] }], }, - expect: [ - {v: 'f1.1'}, - {v: 'f2.2'}, - {v: null} - ] + expect: [{ v: 'f1.1' }, { v: 'f2.2' }, { v: null }], }) add_test({ @@ -151,17 +119,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'has_name', path: "name.exists()"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'has_name', path: 'name.exists()' }, + ], + }, + ], }, expect: [ - {id: 'pt1',has_name: true}, - {id: 'pt2',has_name: true}, - {id: 'pt3',has_name: false} - ] + { id: 'pt1', has_name: true }, + { id: 'pt2', has_name: true }, + { id: 'pt3', has_name: false }, + ], }) add_test({ @@ -172,17 +140,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'has_given', path: "name.given.exists()"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'has_given', path: 'name.given.exists()' }, + ], + }, + ], }, expect: [ - {id: 'pt1',has_given: true}, - {id: 'pt2',has_given: false}, - {id: 'pt3',has_given: false} - ] + { id: 'pt1', has_given: true }, + { id: 'pt2', has_given: false }, + { id: 'pt3', has_given: false }, + ], }) add_test({ @@ -193,17 +161,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'first_given', path: "name.given.first()"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'first_given', path: 'name.given.first()' }, + ], + }, + ], }, expect: [ - {id: 'pt1',first_given: "g1.1.1"}, - {id: 'pt2',first_given: null}, - {id: 'pt3',first_given: null} - ] + { id: 'pt1', first_given: 'g1.1.1' }, + { id: 'pt2', first_given: null }, + { id: 'pt3', first_given: null }, + ], }) add_test({ @@ -214,17 +182,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'first_family', path: "name.family.first()"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'first_family', path: 'name.family.first()' }, + ], + }, + ], }, expect: [ - {id: 'pt1',first_family: "f1.1"}, - {id: 'pt2',first_family: 'f2.1'}, - {id: 'pt3',first_family: null} - ] + { id: 'pt1', first_family: 'f1.1' }, + { id: 'pt2', first_family: 'f2.1' }, + { id: 'pt3', first_family: null }, + ], }) add_test({ @@ -235,17 +203,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'given', path: "name.given.join(', ' )"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'given', path: "name.given.join(', ' )" }, + ], + }, + ], }, expect: [ - {id: 'pt1',given: 'g1.1.1, g1.1.2, g1.2.1'}, - {id: 'pt2',given: ''}, - {id: 'pt3',given: ''} - ] + { id: 'pt1', given: 'g1.1.1, g1.1.2, g1.2.1' }, + { id: 'pt2', given: '' }, + { id: 'pt3', given: '' }, + ], }) add_test({ @@ -256,17 +224,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'given', path: "name.given.join()"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'given', path: 'name.given.join()' }, + ], + }, + ], }, expect: [ - {id: 'pt1',given: 'g1.1.1g1.1.2g1.2.1'}, - {id: 'pt2',given: ''}, - {id: 'pt3',given: ''} - ] + { id: 'pt1', given: 'g1.1.1g1.1.2g1.2.1' }, + { id: 'pt2', given: '' }, + { id: 'pt3', given: '' }, + ], }) // are we sure about this? @@ -275,15 +243,9 @@ describe("fhirpath", () => { view: { resource: 'Patient', status: 'active', - select: [ - {column: [{name: 'id', path: "getResourceKey()"}]} - ] + select: [{ column: [{ name: 'id', path: 'getResourceKey()' }] }], }, - expect: [ - {id: 'Patient/pt1'}, - {id: 'Patient/pt2'}, - {id: 'Patient/pt3'} - ] + expect: [{ id: 'Patient/pt1' }, { id: 'Patient/pt2' }, { id: 'Patient/pt3' }], }) add_test({ @@ -294,17 +256,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'ref', path: "managingOrganization.getReferenceKey()"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'ref', path: 'managingOrganization.getReferenceKey()' }, + ], + }, + ], }, expect: [ - {id: 'pt1', ref: 'o1'}, - {id: 'pt2', ref: 'o2'}, - {id: 'pt3', ref: null} - ] + { id: 'pt1', ref: 'o1' }, + { id: 'pt2', ref: 'o2' }, + { id: 'pt3', ref: null }, + ], }) add_test({ @@ -315,17 +277,17 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'ref', path: "managingOrganization.getReferenceKey(Organization)"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'ref', path: 'managingOrganization.getReferenceKey(Organization)' }, + ], + }, + ], }, expect: [ - {id: 'pt1', ref: 'o1'}, - {id: 'pt2', ref: 'o2'}, - {id: 'pt3', ref: null} - ] + { id: 'pt1', ref: 'o1' }, + { id: 'pt2', ref: 'o2' }, + { id: 'pt3', ref: null }, + ], }) add_test({ @@ -336,18 +298,18 @@ describe("fhirpath", () => { select: [ { column: [ - {name: 'id', path: "id"}, - {name: 'ref', path: "managingOrganization.getReferenceKey(Encounter)"} - ] - } - ] + { name: 'id', path: 'id' }, + { name: 'ref', path: 'managingOrganization.getReferenceKey(Encounter)' }, + ], + }, + ], }, expect: [ - {id: 'pt1', ref: null}, - {id: 'pt2', ref: null}, - {id: 'pt3', ref: null} - ] + { id: 'pt1', ref: null }, + { id: 'pt2', ref: null }, + { id: 'pt3', ref: null }, + ], }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/3_foreach.test.js b/sof-js/tests/3_foreach.test.js index b42c687..cf327cb 100644 --- a/sof-js/tests/3_foreach.test.js +++ b/sof-js/tests/3_foreach.test.js @@ -1,56 +1,43 @@ -import { expect, test, describe } from "bun:test"; -import { start_case, end_case, add_test, debug } from './test_helpers.js' - -let l = console.log +import { describe } from 'bun:test' +import { start_case, end_case, add_test } from './test_helpers.js' let resources = [ { resourceType: 'Patient', id: 'pt1', - name: [ - { family: 'F1.1' }, - { family: 'F1.2' } - ], + name: [{ family: 'F1.1' }, { family: 'F1.2' }], contact: [ { - telecom: [ - { system: 'phone' } - ], + telecom: [{ system: 'phone' }], name: { family: 'FC1.1', - given: ['N1', 'N1`'] - } + given: ['N1', 'N1`'], + }, }, { - telecom: [ - { system: 'email' } - ], - gender: "unknown", + telecom: [{ system: 'email' }], + gender: 'unknown', name: { family: 'FC1.2', - given: ['N2'] - } - } - ] + given: ['N2'], + }, + }, + ], }, { resourceType: 'Patient', id: 'pt2', - name: [ - { family: 'F2.1' }, - { family: 'F2.2' } - ] + name: [{ family: 'F2.1' }, { family: 'F2.2' }], }, { resourceType: 'Patient', - id: 'pt3' - } + id: 'pt3', + }, ] start_case('foreach', 'TBD', resources) describe('foreach', () => { - let result = [ { id: 'pt1', family: 'F1.1' }, { id: 'pt1', family: 'F1.2' }, @@ -65,20 +52,20 @@ describe('foreach', () => { status: 'active', select: [ { - column: [{ name: 'id', path: 'id' }] + column: [{ name: 'id', path: 'id' }], }, { forEach: 'name', - column: [{ name: 'family', path: 'family' }] - } - ] + column: [{ name: 'family', path: 'family' }], + }, + ], }, expect: [ - {id: 'pt1', family: 'F1.1'}, - {id: 'pt1', family: 'F1.2'}, - {id: 'pt2', family: 'F2.1'}, - {id: 'pt2', family: 'F2.2'}, - ] + { id: 'pt1', family: 'F1.1' }, + { id: 'pt1', family: 'F1.2' }, + { id: 'pt2', family: 'F2.1' }, + { id: 'pt2', family: 'F2.2' }, + ], }) add_test({ @@ -90,9 +77,9 @@ describe('foreach', () => { { column: [{ name: 'id', path: 'id' }] }, { forEachOrNull: 'name', - column: [{ name: 'family', path: 'family' }] - } - ] + column: [{ name: 'family', path: 'family' }], + }, + ], }, expect: [ { id: 'pt1', family: 'F1.1' }, @@ -100,7 +87,7 @@ describe('foreach', () => { { id: 'pt2', family: 'F2.1' }, { id: 'pt2', family: 'F2.2' }, { id: 'pt3', family: null }, - ] + ], }) add_test({ @@ -112,14 +99,13 @@ describe('foreach', () => { { column: [{ name: 'id', path: 'id' }] }, { forEach: 'identifier', - column: [{ name: 'value', path: 'value' }] - } - ] + column: [{ name: 'value', path: 'value' }], + }, + ], }, - expect: [] + expect: [], }) - add_test({ title: 'forEach: two on the same level', view: { @@ -128,21 +114,21 @@ describe('foreach', () => { select: [ { forEach: 'contact', - column: [{ name: 'cont_family', path: 'name.family' }] + column: [{ name: 'cont_family', path: 'name.family' }], }, { forEach: 'name', - column: [{ name: 'pat_family' , path: 'family' }] - } - ] + 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.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'} - ] + { pat_family: 'F1.2', cont_family: 'FC1.1' }, + { pat_family: 'F1.2', cont_family: 'FC1.2' }, + ], }) add_test({ @@ -154,15 +140,15 @@ describe('foreach', () => { { column: [{ name: 'id', path: 'id' }] }, { forEach: 'identifier', - column: [{ name: 'value', path: 'value' }] + column: [{ name: 'value', path: 'value' }], }, { forEach: 'name', - column: [{ name: 'family', path: 'family' }] - } - ] + column: [{ name: 'family', path: 'family' }], + }, + ], }, - expect: [] + expect: [], }) add_test({ @@ -174,15 +160,15 @@ describe('foreach', () => { { column: [{ name: 'id', path: 'id' }] }, { forEachOrNull: 'identifier', - column: [{ name: 'value', path: 'value' }] - } - ] + column: [{ name: 'value', path: 'value' }], + }, + ], }, expect: [ { id: 'pt1', value: null }, { id: 'pt2', value: null }, - { id: 'pt3', value: null } - ] + { id: 'pt3', value: null }, + ], }) add_test({ @@ -194,27 +180,27 @@ describe('foreach', () => { { column: [{ name: 'id', path: 'id' }] }, { forEachOrNull: 'identifier', - column: [{ name: 'value', path: 'value' }] + column: [{ name: 'value', path: 'value' }], }, { forEach: 'name', - column: [{ name: 'family', path: 'family' }] - } - ] + 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" }, - { contact_type: "email", name: "N2" , id: "pt1" } - ]; + { contact_type: 'phone', name: 'N1', id: 'pt1' }, + { contact_type: 'phone', name: 'N1`', id: 'pt1' }, + { contact_type: 'email', name: 'N2', id: 'pt1' }, + ] // debug(viewdef, result); add_test({ @@ -230,14 +216,14 @@ describe('foreach', () => { { column: [{ name: 'contact_type', path: 'telecom.system' }] }, { forEach: 'name.given', - column: [{ name: 'name', path: '$this' }] - } - ] - } - ] + column: [{ name: 'name', path: '$this' }], + }, + ], + }, + ], }, - expect: nested_result - }); + expect: nested_result, + }) add_test({ title: 'nested forEach: select & column', @@ -252,14 +238,14 @@ describe('foreach', () => { select: [ { forEach: 'name.given', - column: [{ name: 'name', path: '$this' }] - } - ] - } - ] + column: [{ name: 'name', path: '$this' }], + }, + ], + }, + ], }, - expect: nested_result - }); + expect: nested_result, + }) add_test({ title: 'forEachOrNull & unionAll on the same level', @@ -267,29 +253,26 @@ describe('foreach', () => { resource: 'Patient', select: [ { - column: [{path: 'id', name: 'id'}], + column: [{ path: 'id', name: 'id' }], }, { - forEachOrNull: "contact", + forEachOrNull: 'contact', unionAll: [ - {column: [{path: 'name.family', name: 'name'}]}, - {forEach: "name.given", - column: [{path: '$this', name: 'name'}]} - - ] - } - - ] + { 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}, - ] + { 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({ @@ -298,27 +281,24 @@ describe('foreach', () => { resource: 'Patient', select: [ { - column: [{path: 'id', name: 'id'}], + column: [{ path: 'id', name: 'id' }], }, { - forEach: "contact", + forEach: 'contact', unionAll: [ - {column: [{path: 'name.family', name: 'name'}]}, - {forEach: "name.given", - column: [{path: '$this', name: 'name'}]} - - ] - } - - ] + { 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: 'pt1', name: 'FC1.1' }, + { id: 'pt1', name: 'N1' }, + { id: 'pt1', name: 'N1`' }, + { id: 'pt1', name: 'FC1.2' }, + { id: 'pt1', name: 'N2' }, + ], }) add_test({ @@ -327,29 +307,26 @@ describe('foreach', () => { resource: 'Patient', select: [ { - column: [{path: 'id', name: 'id'}], + column: [{ path: 'id', name: 'id' }], }, { - forEach: "contact", - column: [{path: 'telecom.system', name: 'tel_system'}], - select: [{column: [{path: 'gender', name: 'gender'}]}], + 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'}]} - - ] - } - - ] + { 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: '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({ @@ -358,34 +335,29 @@ describe('foreach', () => { resource: 'Patient', select: [ { - column: [{path: 'id', name: 'id'}], + column: [{ path: 'id', name: 'id' }], }, { - forEachOrNull: "contact", - column: [{path: 'telecom.system', name: 'tel_system'}], - select: [{column: [{path: 'gender', name: 'gender'}]}], + 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'}]} - - ] - } - - ] + { 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}, - ] + { 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(); - + end_case() }) diff --git a/sof-js/tests/4_union.test.js b/sof-js/tests/4_union.test.js index f9dd25c..e822aa0 100644 --- a/sof-js/tests/4_union.test.js +++ b/sof-js/tests/4_union.test.js @@ -1,10 +1,5 @@ -import { describe } from "bun:test"; -import { - start_case, - end_case, - add_test, - add_throwing_test -} from './test_helpers.js' +import { describe } from 'bun:test' +import { start_case, end_case, add_test, add_throwing_test } from './test_helpers.js' let resources = [ { @@ -13,27 +8,27 @@ let resources = [ telecom: [ { value: 't1.1', system: 'phone' }, { value: 't1.2', system: 'fax' }, - { value: 't1.3', system: 'email' } + { value: 't1.3', system: 'email' }, ], contact: [ { - telecom: [{ value: 't1.c1.1', system: 'pager' }] + telecom: [{ value: 't1.c1.1', system: 'pager' }], }, { telecom: [ { value: 't1.c2.1', system: 'url' }, - { value: 't1.c2.2', system: 'sms' } - ] - } - ] + { value: 't1.c2.2', system: 'sms' }, + ], + }, + ], }, { resourceType: 'Patient', id: 'pt2', telecom: [ { value: 't2.1', system: 'phone' }, - { value: 't2.2', system: 'fax' } - ] + { value: 't2.2', system: 'fax' }, + ], }, { resourceType: 'Patient', @@ -42,39 +37,37 @@ let resources = [ { telecom: [ { value: 't3.c1.1', system: 'email' }, - { value: 't3.c1.2', system: 'pager' } - ] + { value: 't3.c1.2', system: 'pager' }, + ], }, { - telecom: [ - { value: 't3.c2.1', system: 'sms' } - ] - } - ] + telecom: [{ value: 't3.c2.1', system: 'sms' }], + }, + ], }, { resourceType: 'Patient', - id: 'pt4' - } + id: 'pt4', + }, ] start_case('union', 'TBD', resources) // TODO: duplicates in union -describe("union", () => { +describe('union', () => { let result = [ - {tel: "t1.1", sys: "phone", id: "pt1"}, - {tel: "t1.2", sys: "fax", id: "pt1"}, - {tel: "t1.3", sys: "email", id: "pt1"}, - {tel: "t1.c1.1", sys: "pager", id: "pt1"}, - {tel: "t1.c2.1", sys: "url", id: "pt1"}, - {tel: "t1.c2.2", sys: "sms", id: "pt1"}, - {tel: "t2.1", sys: "phone", id: "pt2"}, - {tel: "t2.2", sys: "fax", id: "pt2"}, - {tel: "t3.c1.1", sys: "email", id: "pt3"}, - {tel: "t3.c1.2", sys: "pager", id: "pt3"}, - {tel: "t3.c2.1", sys: "sms", id: "pt3"} + { tel: 't1.1', sys: 'phone', id: 'pt1' }, + { tel: 't1.2', sys: 'fax', id: 'pt1' }, + { tel: 't1.3', sys: 'email', id: 'pt1' }, + { tel: 't1.c1.1', sys: 'pager', id: 'pt1' }, + { tel: 't1.c2.1', sys: 'url', id: 'pt1' }, + { tel: 't1.c2.2', sys: 'sms', id: 'pt1' }, + { tel: 't2.1', sys: 'phone', id: 'pt2' }, + { tel: 't2.2', sys: 'fax', id: 'pt2' }, + { tel: 't3.c1.1', sys: 'email', id: 'pt3' }, + { tel: 't3.c1.2', sys: 'pager', id: 'pt3' }, + { tel: 't3.c2.1', sys: 'sms', id: 'pt3' }, ] // debug(unionAll, resources); @@ -91,22 +84,22 @@ describe("union", () => { forEach: 'telecom', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' } - ] + { name: 'sys', path: 'system' }, + ], }, { forEach: 'contact.telecom', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' } - ] - } - ] - } - ] + { name: 'sys', path: 'system' }, + ], + }, + ], + }, + ], }, - expect: result - }); + expect: result, + }) add_test({ title: 'unionAll + column', @@ -121,20 +114,22 @@ describe("union", () => { forEach: 'telecom', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' }]}, + { name: 'sys', path: 'system' }, + ], + }, { forEach: 'contact.telecom', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' } - ] - } - ] - } - ] + { name: 'sys', path: 'system' }, + ], + }, + ], + }, + ], }, - expect: result - }); + expect: result, + }) let unionDups = { resource: 'Patient', @@ -147,35 +142,35 @@ describe("union", () => { forEach: 'telecom', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' } - ] + { name: 'sys', path: 'system' }, + ], }, { forEach: 'telecom', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' } - ] - } - ] - } - ] - }; + { name: 'sys', path: 'system' }, + ], + }, + ], + }, + ], + } let dups_result = [ - { tel: "t1.1", sys: "phone", id: "pt1" }, - { tel: "t1.2", sys: "fax", id: "pt1" }, - { tel: "t1.3", sys: "email", id: "pt1" }, - { tel: "t1.1", sys: "phone", id: "pt1" }, - { tel: "t1.2", sys: "fax", id: "pt1" }, - { tel: "t1.3", sys: "email", id: "pt1" }, - { tel: "t2.1", sys: "phone", id: "pt2" }, - { tel: "t2.2", sys: "fax", id: "pt2" }, - { tel: "t2.1", sys: "phone", id: "pt2" }, - { tel: "t2.2", sys: "fax", id: "pt2" } - ]; + { tel: 't1.1', sys: 'phone', id: 'pt1' }, + { tel: 't1.2', sys: 'fax', id: 'pt1' }, + { tel: 't1.3', sys: 'email', id: 'pt1' }, + { tel: 't1.1', sys: 'phone', id: 'pt1' }, + { tel: 't1.2', sys: 'fax', id: 'pt1' }, + { tel: 't1.3', sys: 'email', id: 'pt1' }, + { tel: 't2.1', sys: 'phone', id: 'pt2' }, + { tel: 't2.2', sys: 'fax', id: 'pt2' }, + { tel: 't2.1', sys: 'phone', id: 'pt2' }, + { tel: 't2.2', sys: 'fax', id: 'pt2' }, + ] - add_test({ title: 'duplicates', view: unionDups, expect: dups_result }); + add_test({ title: 'duplicates', view: unionDups, expect: dups_result }) // TODO: add union with select @@ -190,18 +185,18 @@ describe("union", () => { unionAll: [ { forEach: 'name', - column: [{ name: 'given', path: 'given' }] + column: [{ name: 'given', path: 'given' }], }, { forEach: 'name', - column: [{ name: 'given', path: 'given' }] - } - ] - } - ] + column: [{ name: 'given', path: 'given' }], + }, + ], + }, + ], }, - expect: [] - }); + expect: [], + }) add_test({ title: 'empty with forEachOrNull', @@ -214,27 +209,27 @@ describe("union", () => { unionAll: [ { forEachOrNull: 'name', - column: [{ name: 'given', path: 'given' }] + column: [{ name: 'given', path: 'given' }], }, { forEachOrNull: 'name', - column: [{ name: 'given', path: 'given' }] - } - ] - } - ] + column: [{ name: 'given', path: 'given' }], + }, + ], + }, + ], }, expect: [ - { given: null, id: "pt1" }, - { given: null, id: "pt1" }, - { given: null, id: "pt2" }, - { given: null, id: "pt2" }, - { given: null, id: "pt3" }, - { given: null, id: "pt3" }, - { given: null, id: "pt4" }, - { given: null, id: "pt4" } - ] - }); + { given: null, id: 'pt1' }, + { given: null, id: 'pt1' }, + { given: null, id: 'pt2' }, + { given: null, id: 'pt2' }, + { given: null, id: 'pt3' }, + { given: null, id: 'pt3' }, + { given: null, id: 'pt4' }, + { given: null, id: 'pt4' }, + ], + }) add_test({ title: 'forEachOrNull and forEach', @@ -247,22 +242,22 @@ describe("union", () => { unionAll: [ { forEach: 'name', - column: [{ name: 'given', path: 'given' }] + column: [{ name: 'given', path: 'given' }], }, { forEachOrNull: 'name', - column: [{ name: 'given', path: 'given' }] - } - ] - } - ] + column: [{ name: 'given', path: 'given' }], + }, + ], + }, + ], }, expect: [ - { given: null, id: "pt1" }, - { given: null, id: "pt2" }, - { given: null, id: "pt3" }, - { given: null, id: "pt4" }, - ] + { given: null, id: 'pt1' }, + { given: null, id: 'pt2' }, + { given: null, id: 'pt3' }, + { given: null, id: 'pt4' }, + ], }) add_test({ @@ -276,33 +271,33 @@ describe("union", () => { unionAll: [ { forEach: 'telecom[0]', - column: [{ name: 'tel', path: 'value' }] + column: [{ name: 'tel', path: 'value' }], }, { unionAll: [ { forEach: 'telecom[0]', - column: [{ name: 'tel', path: 'value' }] + column: [{ name: 'tel', path: 'value' }], }, { forEach: 'contact.telecom[0]', - column: [{ name: 'tel', path: 'value' }] - } - ] - } - ] - } - ] + column: [{ name: 'tel', path: 'value' }], + }, + ], + }, + ], + }, + ], }, expect: [ - { id: "pt1", tel: "t1.1" }, - { id: "pt1", tel: "t1.1" }, - { id: "pt1", tel: "t1.c1.1" }, - { id: "pt2", tel: "t2.1" }, - { id: "pt2", tel: "t2.1" }, - { id: "pt3", tel: "t3.c1.1" } - ] - }); + { id: 'pt1', tel: 't1.1' }, + { id: 'pt1', tel: 't1.1' }, + { id: 'pt1', tel: 't1.c1.1' }, + { id: 'pt2', tel: 't2.1' }, + { id: 'pt2', tel: 't2.1' }, + { id: 'pt3', tel: 't3.c1.1' }, + ], + }) add_test({ title: 'one empty operand', @@ -317,29 +312,29 @@ describe("union", () => { forEach: 'telecom.where(false)', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' } - ] + { name: 'sys', path: 'system' }, + ], }, { forEach: 'contact.telecom', column: [ { name: 'tel', path: 'value' }, - { name: 'sys', path: 'system' } - ] - } - ] - } - ] + { name: 'sys', path: 'system' }, + ], + }, + ], + }, + ], }, expect: [ - { id: "pt1", sys: "pager", tel: "t1.c1.1" }, - { id: "pt1", sys: "url", tel: "t1.c2.1" }, - { id: "pt1", sys: "sms", tel: "t1.c2.2" }, - { id: "pt3", sys: "email", tel: "t3.c1.1" }, - { id: "pt3", sys: "pager", tel: "t3.c1.2" }, - { id: "pt3", sys: "sms", tel: "t3.c2.1" } - ] - }); + { id: 'pt1', sys: 'pager', tel: 't1.c1.1' }, + { id: 'pt1', sys: 'url', tel: 't1.c2.1' }, + { id: 'pt1', sys: 'sms', tel: 't1.c2.2' }, + { id: 'pt3', sys: 'email', tel: 't3.c1.1' }, + { id: 'pt3', sys: 'pager', tel: 't3.c1.2' }, + { id: 'pt3', sys: 'sms', tel: 't3.c2.1' }, + ], + }) add_throwing_test({ title: 'column mismatch', @@ -351,22 +346,22 @@ describe("union", () => { unionAll: [ { column: [ - {name: 'a', path: 'a'}, - {name: 'b', path: 'b'} - ] + { name: 'a', path: 'a' }, + { name: 'b', path: 'b' }, + ], }, { column: [ - {name: 'a', path: 'a'}, - {name: 'c', path: 'c'} - ] - } - ] - } - ] + { name: 'a', path: 'a' }, + { name: 'c', path: 'c' }, + ], + }, + ], + }, + ], }, - expectError: true - }); + expectError: true, + }) // as per https://build.fhir.org/ig/FHIR/sql-on-fhir-v2/StructureDefinition-ViewDefinition.html#unionall-column-requirements add_throwing_test({ @@ -379,23 +374,22 @@ describe("union", () => { unionAll: [ { column: [ - {name: 'a', path: 'a'}, - {name: 'b', path: 'b'} - ] + { name: 'a', path: 'a' }, + { name: 'b', path: 'b' }, + ], }, { column: [ - {name: 'b', path: 'b'}, - {name: 'a', path: 'a'} - ] - } - ] - } - ] + { name: 'b', path: 'b' }, + { name: 'a', path: 'a' }, + ], + }, + ], + }, + ], }, - expectError: true - }); + expectError: true, + }) end_case() -}); - +}) diff --git a/sof-js/tests/5_constants.test.js b/sof-js/tests/5_constants.test.js index bf64d69..60e833a 100644 --- a/sof-js/tests/5_constants.test.js +++ b/sof-js/tests/5_constants.test.js @@ -1,216 +1,210 @@ -import { describe } from "bun:test"; -import { - start_case, - end_case, - add_test, - add_throwing_test, - invalid_view -} from "./test_helpers.js"; +import { describe } from 'bun:test' +import { start_case, end_case, add_test, add_throwing_test, invalid_view } from './test_helpers.js' const resources = [ { - resourceType: "Patient", - id: "pt1", + resourceType: 'Patient', + id: 'pt1', name: [ { - family: "Block", - use: "usual" + family: 'Block', + use: 'usual', }, { - family: "Smith", - use: "official" - } - ] + family: 'Smith', + use: 'official', + }, + ], }, { - resourceType: "Patient", - id: "pt2", + resourceType: 'Patient', + id: 'pt2', deceasedBoolean: true, - name: [{ - family: "Johnson", - use: "usual" - }, { - family: "Menendez", - use: "old" - }] - } -]; + name: [ + { + family: 'Johnson', + use: 'usual', + }, + { + family: 'Menendez', + use: 'old', + }, + ], + }, +] -start_case("constant", "constant substitution", resources); +start_case('constant', 'constant substitution', resources) -describe("constant", () => { +describe('constant', () => { add_test({ - title: "constant in path", + title: 'constant in path', view: { resource: 'Patient', status: 'active', - constant: [{ name: "name_use", valueString: "official" }], + constant: [{ name: 'name_use', valueString: 'official' }], select: [ { column: [ - { name: "id", path: "id" }, + { name: 'id', path: 'id' }, { - name: "official_name", - path: "name.where(use = %name_use).family" - } - ] - } - ] + name: 'official_name', + path: 'name.where(use = %name_use).family', + }, + ], + }, + ], }, expect: [ - { id: "pt1", official_name: "Smith" }, - { id: "pt2", official_name: null } - ] - }); + { id: 'pt1', official_name: 'Smith' }, + { id: 'pt2', official_name: null }, + ], + }) add_test({ - title: "constant in forEach", + title: 'constant in forEach', view: { resource: 'Patient', status: 'active', - constant: [{ name: "name_use", valueString: "official" }], + constant: [{ name: 'name_use', valueString: 'official' }], select: [ { - forEach: "name.where(use = %name_use)", - column: [ - { name: "official_name", path: "family" } - ] - } - ] + forEach: 'name.where(use = %name_use)', + column: [{ name: 'official_name', path: 'family' }], + }, + ], }, - expect: [ - { official_name: "Smith" } - ] - }); + expect: [{ official_name: 'Smith' }], + }) add_test({ - title: "constant in where element", + title: 'constant in where element', view: { resource: 'Patient', status: 'active', - constant: [{ name: "name_use", valueString: "official" }], + constant: [{ name: 'name_use', valueString: 'official' }], select: [ { - column: [{ name: "id", path: "id" }] - } + column: [{ name: 'id', path: 'id' }], + }, ], - where: [{ path: "name.where(use = %name_use).exists()" }] + where: [{ path: 'name.where(use = %name_use).exists()' }], }, - expect: [{ id: "pt1" }] - }); + expect: [{ id: 'pt1' }], + }) add_test({ - title: "constant in unionAll", + title: 'constant in unionAll', view: { resource: 'Patient', status: 'active', constant: [ - { name: "use1", valueString: "official" }, - { name: "use2", valueString: "usual" }, + { name: 'use1', valueString: 'official' }, + { name: 'use2', valueString: 'usual' }, + ], + select: [ + { + unionAll: [ + { + forEach: 'name.where(use = %use1)', + column: [ + { + name: 'name', + path: 'family', + }, + ], + }, + { + forEach: 'name.where(use = %use2)', + column: [ + { + name: 'name', + path: 'family', + }, + ], + }, + ], + }, ], - select: [{ - unionAll: [ - { - forEach: "name.where(use = %use1)", - column: [{ - name: "name", - path: "family" - }] - }, { - forEach: "name.where(use = %use2)", - column: [{ - name: "name", - path: "family" - }] - } - ] - }] }, - expect: [ - { name: "Smith" }, - { name: "Block" }, - { name: "Johnson" } - ] + expect: [{ name: 'Smith' }, { name: 'Block' }, { name: 'Johnson' }], }) add_test({ - title: "integer constant", + title: 'integer constant', view: { resource: 'Patient', status: 'active', - constant: [{ name: "name_index", valueInteger: 1 }], + constant: [{ name: 'name_index', valueInteger: 1 }], select: [ { column: [ - { name: "id", path: "id" }, + { name: 'id', path: 'id' }, { - name: "official_name", - path: "name[%name_index].family" - } - ] - } - ] + name: 'official_name', + path: 'name[%name_index].family', + }, + ], + }, + ], }, expect: [ - { id: "pt1", official_name: "Smith" }, - { id: "pt2", official_name: "Menendez" } - ] - }); + { id: 'pt1', official_name: 'Smith' }, + { id: 'pt2', official_name: 'Menendez' }, + ], + }) add_test({ - title: "boolean constant", + title: 'boolean constant', view: { resource: 'Patient', status: 'active', - constant: [{ name: "is_deceased", valueBoolean: true }], + constant: [{ name: 'is_deceased', valueBoolean: true }], select: [ { - column: [{ name: "id", path: "id" }] - } + column: [{ name: 'id', path: 'id' }], + }, ], - where: [{ path: "deceased.ofType(boolean).exists() and deceased.ofType(boolean) = %is_deceased" }] + where: [{ path: 'deceased.ofType(boolean).exists() and deceased.ofType(boolean) = %is_deceased' }], }, - expect: [{ id: "pt2" }] - }); + expect: [{ id: 'pt2' }], + }) add_throwing_test({ - title: "accessing an undefined constant", + title: 'accessing an undefined constant', view: { resource: 'Patient', status: 'active', - constant: [{ name: "name_use", valueString: "official" }], + constant: [{ name: 'name_use', valueString: 'official' }], select: [ { - forEach: "name.where(use = %wrong_name)", - column: [ - { name: "official_name", path: "family" } - ] - } - ] + forEach: 'name.where(use = %wrong_name)', + column: [{ name: 'official_name', path: 'family' }], + }, + ], }, - expectError: true - }); + expectError: true, + }) invalid_view({ - title: "incorrect constant definition", + title: 'incorrect constant definition', view: { resource: 'Patient', status: 'active', - constant: [{ name: "name_use" }], + constant: [{ name: 'name_use' }], select: [ { column: [ - { name: "id", path: "id" }, + { name: 'id', path: 'id' }, { - name: "official_name", - path: "name.where(use = %name_use).family" - } - ] - } - ] + name: 'official_name', + path: 'name.where(use = %name_use).family', + }, + ], + }, + ], }, - expectError: true - }); + expectError: true, + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/collection.test.js b/sof-js/tests/collection.test.js index 4973524..6f90a42 100644 --- a/sof-js/tests/collection.test.js +++ b/sof-js/tests/collection.test.js @@ -1,5 +1,5 @@ -import { describe } from "bun:test"; -import { add_test, add_throwing_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, add_throwing_test, end_case, start_case } from './test_helpers' let resources = [ { @@ -9,16 +9,16 @@ let resources = [ { use: 'official', family: 'f1.1', - given: ['g1.1'] + given: ['g1.1'], }, { family: 'f1.2', - given: ['g1.2', 'g1.3'] - } + given: ['g1.2', 'g1.3'], + }, ], gender: 'male', birthDate: '1950-01-01', - address: [{ city: 'c1' }] + address: [{ city: 'c1' }], }, { resourceType: 'Patient', @@ -26,20 +26,20 @@ let resources = [ name: [ { family: 'f2.1', - given: ['g2.1'] + given: ['g2.1'], }, { use: 'official', family: 'f2.2', - given: ['g2.2', 'g2.3'] - } + given: ['g2.2', 'g2.3'], + }, ], gender: 'female', - birthDate: '1950-01-01' - } -]; + birthDate: '1950-01-01', + }, +] -start_case('collection', 'TBD', resources); +start_case('collection', 'TBD', resources) describe('collection', () => { add_throwing_test({ @@ -54,22 +54,22 @@ describe('collection', () => { { name: 'last_name', path: 'name.family', - collection: false + collection: false, }, { name: 'first_name', path: 'name.given', - collection: true - } - ] - } - ] + collection: true, + }, + ], + }, + ], }, - expectError: true - }); + expectError: true, + }) add_test({ - title: "collection = true", + title: 'collection = true', view: { resource: 'Patient', status: 'active', @@ -80,41 +80,39 @@ describe('collection', () => { { name: 'last_name', path: 'name.family', - collection: true + collection: true, }, { name: 'first_name', path: 'name.given', - collection: true - } - ] - } - ] + collection: true, + }, + ], + }, + ], }, expect: [ { id: 'pt1', last_name: ['f1.1', 'f1.2'], - first_name: ['g1.1', 'g1.2', 'g1.3'] + first_name: ['g1.1', 'g1.2', 'g1.3'], }, { id: 'pt2', last_name: ['f2.1', 'f2.2'], - first_name: ['g2.1', 'g2.2', 'g2.3'] - } - ] - }); + first_name: ['g2.1', 'g2.2', 'g2.3'], + }, + ], + }) add_test({ - title: "collection = false relative to forEach parent", + title: 'collection = false relative to forEach parent', view: { resource: 'Patient', status: 'active', select: [ { - column: [ - { name: 'id', path: 'id' } - ], + column: [{ name: 'id', path: 'id' }], select: [ { forEach: 'name', @@ -122,53 +120,51 @@ describe('collection', () => { { name: 'last_name', path: 'family', - collection: false + collection: false, }, { name: 'first_name', path: 'given', - collection: true - } - ] - } - ] - } - ] + collection: true, + }, + ], + }, + ], + }, + ], }, expect: [ { id: 'pt1', last_name: 'f1.1', - first_name: ['g1.1'] + first_name: ['g1.1'], }, { id: 'pt1', last_name: 'f1.2', - first_name: ['g1.2', 'g1.3'] + first_name: ['g1.2', 'g1.3'], }, { id: 'pt2', last_name: 'f2.1', - first_name: ['g2.1'] + first_name: ['g2.1'], }, { id: 'pt2', last_name: 'f2.2', - first_name: ['g2.2', 'g2.3'] - } - ] - }); + first_name: ['g2.2', 'g2.3'], + }, + ], + }) add_test({ - title: "collection = false relative to forEachOrNull parent", + title: 'collection = false relative to forEachOrNull parent', view: { resource: 'Patient', status: 'active', select: [ { - column: [ - { name: 'id', path: 'id' } - ], + column: [{ name: 'id', path: 'id' }], select: [ { forEach: 'name', @@ -176,42 +172,42 @@ describe('collection', () => { { name: 'last_name', path: 'family', - collection: false + collection: false, }, { name: 'first_name', path: 'given', - collection: true - } - ] - } - ] - } - ] + collection: true, + }, + ], + }, + ], + }, + ], }, expect: [ { id: 'pt1', last_name: 'f1.1', - first_name: ['g1.1'] + first_name: ['g1.1'], }, { id: 'pt1', last_name: 'f1.2', - first_name: ['g1.2', 'g1.3'] + first_name: ['g1.2', 'g1.3'], }, { id: 'pt2', last_name: 'f2.1', - first_name: ['g2.1'] + first_name: ['g2.1'], }, { id: 'pt2', last_name: 'f2.2', - first_name: ['g2.2', 'g2.3'] - } - ] - }); + first_name: ['g2.2', 'g2.3'], + }, + ], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/combination.test.js b/sof-js/tests/combination.test.js index 9ed9d37..3ff42fe 100644 --- a/sof-js/tests/combination.test.js +++ b/sof-js/tests/combination.test.js @@ -1,110 +1,107 @@ -import { describe } from "bun:test"; -import { start_case, end_case, add_test, debug } from './test_helpers.js' - +import { describe } from 'bun:test' +import { start_case, end_case, add_test } from './test_helpers.js' let resources = [ - {id: 'pt1', resourceType: 'Patient'}, - {id: 'pt2', resourceType: 'Patient'}, - {id: 'pt3', resourceType: 'Patient'}, + { id: 'pt1', resourceType: 'Patient' }, + { id: 'pt2', resourceType: 'Patient' }, + { id: 'pt3', resourceType: 'Patient' }, ] start_case('combinations', 'TBD', resources) -describe("combinations", () => { - +describe('combinations', () => { add_test({ title: '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'}] - }); + expect: [{ id: 'pt1' }, { id: 'pt2' }, { id: 'pt3' }], + }) add_test({ title: 'column + select', view: { resource: 'Patient', - select: [{ - column: [{path: 'id', name: 'column_id'}], - select: [ - { - column: [{path: 'id', name: 'select_id'}] - } - ] - }] + select: [ + { + column: [{ path: 'id', name: 'column_id' }], + select: [ + { + column: [{ path: 'id', name: 'select_id' }], + }, + ], + }, + ], }, expect: [ - { column_id: "pt1", select_id: "pt1" }, - { column_id: "pt2", select_id: "pt2" }, - { column_id: "pt3", select_id: "pt3" } - ] - }); + { column_id: 'pt1', select_id: 'pt1' }, + { column_id: 'pt2', 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'}] } - ] + select: [{ column: [{ path: 'id', name: 'id_1' }] }, { column: [{ path: 'id', name: 'id_2' }] }], }, expect: [ - { id_1: "pt1", id_2: "pt1" }, - { id_1: "pt2", id_2: "pt2" }, - { id_1: "pt3", id_2: "pt3" } - ] - }); + { id_1: 'pt1', id_2: 'pt1' }, + { id_1: 'pt2', 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'}] } - ] - }] + select: [ + { + select: [{ column: [{ path: 'id', name: 'id_1' }] }, { column: [{ path: 'id', name: 'id_2' }] }], + }, + ], }, expect: [ - { id_1: "pt1", id_2: "pt1" }, - { id_1: "pt2", id_2: "pt2" }, - { id_1: "pt3", id_2: "pt3" } - ] - }); + { id_1: 'pt1', id_2: 'pt1' }, + { id_1: 'pt2', 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'" }] + 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" } - ] - }); + expect: [{ column_id: 'pt1', select_id: 'pt1' }], + }) add_test({ 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'}] - }); + expect: [{ id: 'pt1' }, { id: 'pt2' }, { id: 'pt3' }], + }) end_case() - }) diff --git a/sof-js/tests/fn_empty.test.js b/sof-js/tests/fn_empty.test.js index 10e6f7d..8025656 100644 --- a/sof-js/tests/fn_empty.test.js +++ b/sof-js/tests/fn_empty.test.js @@ -1,5 +1,5 @@ -import { describe } from "bun:test"; -import { add_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, end_case, start_case } from './test_helpers' const resources = [ { @@ -8,21 +8,21 @@ const resources = [ name: [ { use: 'official', - family: 'f1' - } - ] + family: 'f1', + }, + ], }, { resourceType: 'Patient', - id: 'p2' - } -]; + id: 'p2', + }, +] -start_case('fn_empty', 'TBD', resources); +start_case('fn_empty', 'TBD', resources) describe('empty function', () => { add_test({ - title: "empty names", + title: 'empty names', view: { resource: 'Patient', status: 'active', @@ -30,16 +30,16 @@ describe('empty function', () => { { column: [ { name: 'id', path: 'id' }, - { name: 'name_empty', path: 'name.empty()' } - ] - } - ] + { name: 'name_empty', path: 'name.empty()' }, + ], + }, + ], }, expect: [ { id: 'p1', name_empty: false }, - { id: 'p2', name_empty: true } - ] - }); + { id: 'p2', name_empty: true }, + ], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/fn_extension.test.js b/sof-js/tests/fn_extension.test.js index 4f71ec5..7df5bba 100644 --- a/sof-js/tests/fn_extension.test.js +++ b/sof-js/tests/fn_extension.test.js @@ -1,165 +1,165 @@ -import { describe } from "bun:test"; -import { add_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, end_case, start_case } from './test_helpers' const resources = [ { - resourceType: "Patient", - id: "pt1", + resourceType: 'Patient', + id: 'pt1', meta: { - profile: ["http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"] + profile: ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient'], }, extension: [ { - id: "birthsex", - url: "http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex", - valueCode: "F" + id: 'birthsex', + url: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex', + valueCode: 'F', }, { - id: "race", - url: "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race", + id: 'race', + url: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race', extension: [ { - url: "ombCategory", + url: 'ombCategory', valueCoding: { - system: "urn:oid:2.16.840.1.113883.6.238", - code: "2106-3", - display: "White" - } + system: 'urn:oid:2.16.840.1.113883.6.238', + code: '2106-3', + display: 'White', + }, }, { - url: "text", - valueString: "Mixed" - } - ] + url: 'text', + valueString: 'Mixed', + }, + ], }, { - id: "sex", - url: "http://hl7.org/fhir/us/core/StructureDefinition/us-core-sex", - valueCode: "248152002" - } - ] + id: 'sex', + url: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-sex', + valueCode: '248152002', + }, + ], }, { - resourceType: "Patient", - id: "pt2", + resourceType: 'Patient', + id: 'pt2', meta: { - profile: ["http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"] + profile: ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient'], }, extension: [ { - id: "birthsex", - url: "http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex", - valueCode: "M" + id: 'birthsex', + url: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex', + valueCode: 'M', }, { - url: "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race", - id: "race", + url: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race', + id: 'race', extension: [ { - url: "ombCategory", + url: 'ombCategory', valueCoding: { - system: "urn:oid:2.16.840.1.113883.6.238", - code: "2135-2", - display: "Hispanic or Latino" - } + system: 'urn:oid:2.16.840.1.113883.6.238', + code: '2135-2', + display: 'Hispanic or Latino', + }, }, { - url: "text", - valueString: "Mixed" - } - ] + url: 'text', + valueString: 'Mixed', + }, + ], }, { - id: "sex", - url: "http://hl7.org/fhir/us/core/StructureDefinition/us-core-sex", - valueCode: "248152002" - } - ] + id: 'sex', + url: 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-sex', + valueCode: '248152002', + }, + ], }, { - resourceType: "Patient", - id: "pt3", + resourceType: 'Patient', + id: 'pt3', meta: { - profile: ["http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient"] + profile: ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient'], }, - extension: [] - } -]; + extension: [], + }, +] -start_case('fn_extension', 'TBD', resources); +start_case('fn_extension', 'TBD', resources) describe('extension function', () => { add_test({ - title: "simple extension", - description: "flatten simple extension", + title: 'simple extension', + description: 'flatten simple extension', view: { - resource: "Patient", + resource: 'Patient', select: [ { column: [ { - path: "id", - name: "id" + path: 'id', + name: 'id', }, { - name: "birthsex", - path: "extension('http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex').value.ofType(code).first()" - } - ] - } - ] + name: 'birthsex', + path: "extension('http://hl7.org/fhir/us/core/StructureDefinition/us-core-birthsex').value.ofType(code).first()", + }, + ], + }, + ], }, expect: [ { - id: "pt1", - birthsex: "F" + id: 'pt1', + birthsex: 'F', }, { - id: "pt2", - birthsex: "M" + id: 'pt2', + birthsex: 'M', }, { - id: "pt3", - birthsex: null - } - ] - }); + id: 'pt3', + birthsex: null, + }, + ], + }) add_test({ - title: "nested extension", - description: "flatten simple extension", + title: 'nested extension', + description: 'flatten simple extension', view: { - resource: "Patient", + resource: 'Patient', select: [ { column: [ { - path: "id", - name: "id" + path: 'id', + name: 'id', }, { - name: "race_code", - path: "extension('http://hl7.org/fhir/us/core/StructureDefinition/us-core-race').extension('ombCategory').value.ofType(Coding).code.first()" - } - ] - } - ] + name: 'race_code', + path: "extension('http://hl7.org/fhir/us/core/StructureDefinition/us-core-race').extension('ombCategory').value.ofType(Coding).code.first()", + }, + ], + }, + ], }, expect: [ { - id: "pt1", - race_code: "2106-3" + id: 'pt1', + race_code: '2106-3', }, { - id: "pt2", - race_code: "2135-2" + id: 'pt2', + race_code: '2135-2', }, { - id: "pt3", - race_code: null - } - ] - }); + id: 'pt3', + race_code: null, + }, + ], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/fn_first.test.js b/sof-js/tests/fn_first.test.js index d49a325..daf5a11 100644 --- a/sof-js/tests/fn_first.test.js +++ b/sof-js/tests/fn_first.test.js @@ -1,58 +1,58 @@ -import { describe } from "bun:test"; -import { add_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, end_case, start_case } from './test_helpers' const resources = [ { - resourceType: "Patient", + resourceType: 'Patient', name: [ { - use: "official", - family: "f1", - given: ["g1.1", "g1.2"] + use: 'official', + family: 'f1', + given: ['g1.1', 'g1.2'], }, { - use: "usual", - given: ["g2.1"] + use: 'usual', + given: ['g2.1'], }, { - use: "maiden", - family: "f3", - given: ["g3.1", "g3.2"], - period: { "end": "2002" } - } - ] - } -]; + use: 'maiden', + family: 'f3', + given: ['g3.1', 'g3.2'], + period: { end: '2002' }, + }, + ], + }, +] -start_case('fn_first', 'FHIRPath `first` function.', resources); +start_case('fn_first', 'FHIRPath `first` function.', resources) describe('first function', () => { add_test({ - title: "table level first()", + title: 'table level first()', view: { - resource: "Patient", - select: [{ column: [{ path: "name.first().use", name: "use" }] }] + resource: 'Patient', + select: [{ column: [{ path: 'name.first().use', name: 'use' }] }], }, - expect: [{ use: "official" }] - }); + expect: [{ use: 'official' }], + }) add_test({ - title: "table and field level first()", + title: 'table and field level first()', view: { - resource: "Patient", + resource: 'Patient', select: [ { column: [ { - path: "name.first().given.first()", - name: "given" - } - ] - } - ] + path: 'name.first().given.first()', + name: 'given', + }, + ], + }, + ], }, - expect: [{ given: "g1.1" }] - }); + expect: [{ given: 'g1.1' }], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/fn_join.test.js b/sof-js/tests/fn_join.test.js index 5875483..8b8daf3 100644 --- a/sof-js/tests/fn_join.test.js +++ b/sof-js/tests/fn_join.test.js @@ -1,69 +1,69 @@ -import { describe } from "bun:test"; -import { add_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, end_case, start_case } from './test_helpers' const resources = [ { - resourceType: "Patient", - id: "p1", + resourceType: 'Patient', + id: 'p1', name: [ { - use: "official", - given: ["p1.g1", "p1.g2"] - } - ] - } -]; + use: 'official', + given: ['p1.g1', 'p1.g2'], + }, + ], + }, +] -start_case('fn_join', 'FHIRPath `join` function.', resources); +start_case('fn_join', 'FHIRPath `join` function.', resources) describe('join function', () => { add_test({ - title: "join with comma", + title: 'join with comma', view: { - resource: "Patient", + resource: 'Patient', select: [ { column: [ - { path: "id", name: "id" }, - { path: "name.given.join(',')", name: "given" } - ] - } - ] + { path: 'id', name: 'id' }, + { path: "name.given.join(',')", name: 'given' }, + ], + }, + ], }, - expect: [{ id: "p1", given: "p1.g1,p1.g2" }] - }); + expect: [{ id: 'p1', given: 'p1.g1,p1.g2' }], + }) add_test({ - title: "join with empty value", + title: 'join with empty value', view: { - resource: "Patient", + resource: 'Patient', select: [ { column: [ - { path: "id", name: "id" }, - { path: "name.given.join('')", name: "given" } - ] - } - ] + { path: 'id', name: 'id' }, + { path: "name.given.join('')", name: 'given' }, + ], + }, + ], }, - expect: [{ id: "p1", given: "p1.g1p1.g2" }] - }); + expect: [{ id: 'p1', given: 'p1.g1p1.g2' }], + }) add_test({ - title: "join with no value - default to no separator", + title: 'join with no value - default to no separator', view: { - resource: "Patient", + resource: 'Patient', select: [ { column: [ - { path: "id", name: "id" }, - { path: "name.given.join()", name: "given" } - ] - } - ] + { path: 'id', name: 'id' }, + { path: 'name.given.join()', name: 'given' }, + ], + }, + ], }, - expect: [{ id: "p1", given: "p1.g1p1.g2" }] - }); + expect: [{ id: 'p1', given: 'p1.g1p1.g2' }], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/fn_oftype.test.js b/sof-js/tests/fn_oftype.test.js index f577a13..e95953e 100644 --- a/sof-js/tests/fn_oftype.test.js +++ b/sof-js/tests/fn_oftype.test.js @@ -1,5 +1,5 @@ -import { describe } from "bun:test"; -import { add_test, add_throwing_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, end_case, start_case } from './test_helpers' const resources = [ { @@ -7,24 +7,24 @@ const resources = [ id: 'o1', code: { text: 'code' }, status: 'final', - valueString: 'foo' + valueString: 'foo', }, { resourceType: 'Observation', id: 'o2', code: { text: 'code' }, status: 'final', - valueInteger: 42 + valueInteger: 42, }, { resourceType: 'Observation', id: 'o3', code: { text: 'code' }, status: 'final', - } -]; + }, +] -start_case('fn_oftype', 'TBD', resources); +start_case('fn_oftype', 'TBD', resources) describe('ofType function', () => { add_test({ @@ -38,18 +38,18 @@ describe('ofType function', () => { { path: 'id', name: 'id' }, { path: 'value.ofType(string)', - name: 'string_value' - } - ] - } - ] + name: 'string_value', + }, + ], + }, + ], }, expect: [ { id: 'o1', string_value: 'foo' }, { id: 'o2', string_value: null }, - { id: 'o3', string_value: null } - ] - }); + { id: 'o3', string_value: null }, + ], + }) add_test({ title: 'select integer values', @@ -62,18 +62,18 @@ describe('ofType function', () => { { path: 'id', name: 'id' }, { path: 'value.ofType(integer)', - name: 'integer_value' - } - ] - } - ] + name: 'integer_value', + }, + ], + }, + ], }, expect: [ { id: 'o1', integer_value: null }, { id: 'o2', integer_value: 42 }, - { id: 'o3', integer_value: null } - ] - }); + { id: 'o3', integer_value: null }, + ], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/internal.test.js b/sof-js/tests/internal.test.js new file mode 100644 index 0000000..e1d2659 --- /dev/null +++ b/sof-js/tests/internal.test.js @@ -0,0 +1,66 @@ +import { expect, test, describe } from 'bun:test' +import { get_columns, row_product } from '../src/index.js' + +describe('internal tests', () => { + test('row_product', () => { + expect( + row_product([ + [{ a: 1 }, { a: 2 }], + [{ b: 1 }, { b: 2 }], + ]), + ).toEqual([ + { b: 1, a: 1 }, + { b: 1, a: 2 }, + { b: 2, a: 1 }, + { b: 2, a: 2 }, + ]) + + expect( + row_product([ + [{ b: 1 }, { b: 2 }], + [{ a: 1 }, { a: 2 }], + ]), + ).toEqual([ + { a: 1, b: 1 }, + { a: 1, b: 2 }, + { a: 2, b: 1 }, + { a: 2, b: 2 }, + ]) + + expect(row_product([[{ a: 1 }, { a: 2 }], []])).toEqual([]) + + expect(row_product([[{ a: 1 }, { a: 2 }], [{}]])).toEqual([{ a: 1 }, { a: 2 }]) + + expect(row_product([[{ a: 1 }, { a: 2 }]])).toEqual([{ a: 1 }, { a: 2 }]) + }) + + test('columns', () => { + expect( + get_columns({ + select: [ + { column: [{ name: 'id', path: 'id' }] }, + { + forEach: 'contact', + column: [{ name: 'contact_type', path: 'type' }], + select: [{ forEach: 'person', column: [{ name: 'name', path: 'name' }] }], + }, + ], + }), + ).toEqual(['id', 'contact_type', 'name']) + + expect( + get_columns({ + select: [ + { column: [{ path: 'id' }, { path: 'birthDate' }] }, + { + forEach: 'name', + column: [ + { path: 'family', name: 'last_name' }, + { path: "given.join(' ')", name: 'first_name' }, + ], + }, + ], + }), + ).toEqual(['id', 'birthDate', 'last_name', 'first_name']) + }) +}) diff --git a/sof-js/tests/json.js b/sof-js/tests/json.js index a263f88..f91abf9 100644 --- a/sof-js/tests/json.js +++ b/sof-js/tests/json.js @@ -1,32 +1,32 @@ import fs from 'fs' -import { expect, test, describe } from "bun:test"; +import { expect, test, describe } from 'bun:test' import { evaluate } from '../src/index.js' -const testDirectory = '../tests/'; -const files = fs.readdirSync(testDirectory); +const testDirectory = '../tests/' +const files = fs.readdirSync(testDirectory) -files.forEach(f => { - const testGroup = JSON.parse(fs.readFileSync(testDirectory + f)); - const resources = testGroup.resources; +files.forEach((f) => { + const testGroup = JSON.parse(fs.readFileSync(testDirectory + f)) + const resources = testGroup.resources - testGroup.tests.forEach(testCase => { - const view = testCase.view; + testGroup.tests.forEach((testCase) => { + const view = testCase.view describe(f, () => { if (testCase.expect !== undefined) { test(testCase.title, () => { - const res = evaluate(view, resources); - expect(res).toEqual(testCase.expect); - }); + const res = evaluate(view, resources) + expect(res).toEqual(testCase.expect) + }) } else if (testCase.expectError !== undefined) { test(testCase.title, () => { - expect(() => evaluate(view, resources)).toThrow(); - }); + expect(() => evaluate(view, resources)).toThrow() + }) } else if (testCase.expectCount !== undefined) { - throw new Error("expectCount is not implemented yet"); + throw new Error('expectCount is not implemented yet') } else { - throw new Error(`'${testCase.title}' test has no known expectation`); + throw new Error(`'${testCase.title}' test has no known expectation`) } - }); - }); -}); + }) + }) +}) diff --git a/sof-js/tests/logic.test.js b/sof-js/tests/logic.test.js index 972fe36..4c6a596 100644 --- a/sof-js/tests/logic.test.js +++ b/sof-js/tests/logic.test.js @@ -1,64 +1,64 @@ -import { describe } from "bun:test"; -import { add_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, end_case, start_case } from './test_helpers' const resources = [ { - resourceType: "Patient", - id: "m0", - gender: "male", - deceasedBoolean: false + resourceType: 'Patient', + id: 'm0', + gender: 'male', + deceasedBoolean: false, }, { - resourceType: "Patient", - id: "f0", + resourceType: 'Patient', + id: 'f0', deceasedBoolean: false, - gender: "female" + gender: 'female', }, { - resourceType: "Patient", - id: "m1", - gender: "male", - deceasedBoolean: true + resourceType: 'Patient', + id: 'm1', + gender: 'male', + deceasedBoolean: true, }, { - resourceType: "Patient", - id: "f1", - gender: "female" - } -]; + resourceType: 'Patient', + id: 'f1', + gender: 'female', + }, +] -start_case('logic', 'TBD', resources); +start_case('logic', 'TBD', resources) describe('boolean logic', () => { add_test({ title: "filtering with 'and'", view: { - resource: "Patient", + resource: 'Patient', where: [{ path: "gender = 'male' and deceased.ofType(boolean) = false" }], - select: [{ column: [{ path: "id", name: "id" }]}] + select: [{ column: [{ path: 'id', name: 'id' }] }], }, - expect: [{ id: "m0" }] - }); + expect: [{ id: 'm0' }], + }) add_test({ title: "filtering with 'or'", view: { - resource: "Patient", + resource: 'Patient', where: [{ path: "gender = 'male' or deceased.ofType(boolean) = false" }], - select: [{ column: [{ path: "id", name: "id"}]}] + select: [{ column: [{ path: 'id', name: 'id' }] }], }, - expect: [{ id: "m0" }, { id: "f0" }, { id: "m1" }] - }); + expect: [{ id: 'm0' }, { id: 'f0' }, { id: 'm1' }], + }) add_test({ title: "filtering with 'not'", view: { - resource: "Patient", + resource: 'Patient', where: [{ path: "(gender = 'male').not()" }], - select: [{ column: [{ path: "id", name: "id" }]}] + select: [{ column: [{ path: 'id', name: 'id' }] }], }, - expect: [{ id: "f0" }, { id: "f1" }] - }); + expect: [{ id: 'f0' }, { id: 'f1' }], + }) - end_case(); -}); + end_case() +}) diff --git a/sof-js/tests/test_helpers.js b/sof-js/tests/test_helpers.js index 9a5b0c6..864444e 100644 --- a/sof-js/tests/test_helpers.js +++ b/sof-js/tests/test_helpers.js @@ -1,31 +1,30 @@ -import { evaluate, errors, row_product } from '../src/index.js' -import { expect, test, afterAll } from "bun:test"; +import { evaluate, errors } from '../src/index.js' +import { expect, test, afterAll } from 'bun:test' import fs from 'fs' - -let test_case = null; +let test_case = null export function start_case(name, desc, resources) { test_case = { title: name, description: desc, resources: resources, - tests: [] + tests: [], } } export function add_test(opts) { test(opts.title, () => { - test_case.tests.push(opts); - const res = evaluate(opts.view, test_case.resources); - expect(res).toEqual(opts.expect); + test_case.tests.push(opts) + const res = evaluate(opts.view, test_case.resources) + expect(res).toEqual(opts.expect) }) } export function add_throwing_test(opts) { test(opts.title, () => { test_case.tests.push(opts) - expect(() => evaluate(opts.view, test_case.resources)).toThrow(); + expect(() => evaluate(opts.view, test_case.resources)).toThrow() }) } @@ -40,22 +39,20 @@ export function invalid_view(opts) { export function end_case(name, desc, resources) { // TODO: publish test case - afterAll(()=>{ + afterAll(() => { // console.log(JSON.stringify(test_case, null, " ")); let file_name = __dirname + '/../../tests/' + test_case.title + '.json' - fs.writeFileSync(file_name, JSON.stringify(test_case, null, " ")) + fs.writeFileSync(file_name, JSON.stringify(test_case, null, ' ')) console.log('write: ', file_name) }) } - export function debug(viewdef) { - let res = evaluate( viewdef, test_case.resources) - console.log('result:', res); - return res; + let res = evaluate(viewdef, test_case.resources) + console.log('result:', res) + return res } export function should_fail(viewdef) { // TODO: dump tests } - diff --git a/sof-js/tests/testrunner_example.js b/sof-js/tests/testrunner_example.js index d346576..2095020 100644 --- a/sof-js/tests/testrunner_example.js +++ b/sof-js/tests/testrunner_example.js @@ -10,11 +10,11 @@ function isEqual(a, b) { } const canonicalize = (arr) => { - if(arr === undefined) { + if (arr === undefined) { arr = [] } - if(! Array.isArray(arr)) { - throw new Error("Expected array, got " + JSON.stringify(arr)) + if (!Array.isArray(arr)) { + throw new Error('Expected array, got ' + JSON.stringify(arr)) } return [...arr].sort((a, b) => { const keysA = Object.keys(a).sort() @@ -82,7 +82,7 @@ function runThrowingTest(test, resources) { return { passed: false, expectedFail: true, - actual: result + actual: result, } } catch (e) { return { passed: true } @@ -91,59 +91,59 @@ function runThrowingTest(test, resources) { function runTest(test, resources) { if (test.expectError) { - return runThrowingTest(test); + return runThrowingTest(test) } try { const result = evaluate(test.view, resources) if (test.expectCount) { - const passed = result.length === test.expectCount; + const passed = result.length === test.expectCount return passed ? { passed } : { - passed, - expectedCount: test.expectCount, - actual: result.length - }; + passed, + expectedCount: test.expectCount, + actual: result.length, + } } else { const match = arraysMatch(result, test.expect) return { passed: match.passed, expected: test.expect, actual: result, - message: match.message - }; + message: match.message, + } } } catch (e) { return { passed: null, - message: e.toString() + message: e.toString(), } } } function printResult(title, result) { - let testResult; + let testResult if (result.passed === true) { - testResult = "passed"; + testResult = 'passed' } else if (result.passed === false) { - testResult = "failed"; + testResult = 'failed' } else { - testResult = "error"; + testResult = 'error' } - console.log( " *", title, " => ", testResult); + console.log(' *', title, ' => ', testResult) if (result.passed !== true) { if (result.expected && result.actual) { - console.log("expected:"); - console.dir(result.expected, { depth: null }); - console.log("got:"); - console.dir(result.actual, { depth: null }); + console.log('expected:') + console.dir(result.expected, { depth: null }) + console.log('got:') + console.dir(result.actual, { depth: null }) } if (result.message) { - console.log(result.message); + console.log(result.message) } } } @@ -152,35 +152,35 @@ const tests_dir = '../tests/' const files = fs.readdirSync(tests_dir) let test_summary = { pass: 0, fail: 0, error: 0 } -const result = {}; -files.forEach(f => { +const result = {} +files.forEach((f) => { const testcase = JSON.parse(fs.readFileSync(tests_dir + f)) console.log('running', testcase.title, `file ${f}`) - const testResult = testcase.tests.map(test => { - let result = null; + const testResult = testcase.tests.map((test) => { + let result = null if (test.expectError) { - result = runThrowingTest(test, testcase.resources); - printResult(test.title, result); + result = runThrowingTest(test, testcase.resources) + printResult(test.title, result) } else { - result = runTest(test, testcase.resources); - printResult(test.title, result); + result = runTest(test, testcase.resources) + printResult(test.title, result) } if (result.passed === true) { - test_summary.pass++; + test_summary.pass++ } else if (result.passed === false) { - test_summary.fail++; + test_summary.fail++ } else { - test_summary.error++; + test_summary.error++ } - return { result }; - }); + return { result } + }) - result[f] = { tests: testResult }; -}); + result[f] = { tests: testResult } +}) -fs.writeFileSync('../test_report/public/test-results.json', JSON.stringify(result)); +fs.writeFileSync('../test_report/public/test-results.json', JSON.stringify(result)) console.log(test_summary) diff --git a/sof-js/tests/view_resource.test.js b/sof-js/tests/view_resource.test.js index 965108e..5db34f6 100644 --- a/sof-js/tests/view_resource.test.js +++ b/sof-js/tests/view_resource.test.js @@ -1,11 +1,5 @@ -import { describe } from "bun:test"; -import { - start_case, - end_case, - add_test, - invalid_view, - debug -} from './test_helpers.js' +import { describe } from 'bun:test' +import { start_case, end_case, add_test, invalid_view } from './test_helpers.js' let l = console.log @@ -16,49 +10,41 @@ let resources = [ id: 'ob1', resourceType: 'Observation', code: { text: 'code' }, - status: 'final' - } + status: 'final', + }, ] start_case('view_resource', 'TBD', resources) -describe("view_resource", () => { - +describe('view_resource', () => { add_test({ title: 'only pts', view: { resource: 'Patient', status: 'active', - select: [ - { column: [{path: 'id', name: 'id'}] } - ] + select: [{ column: [{ path: 'id', name: 'id' }] }], }, - expect: [ {id: 'pt1'}, {id: 'pt2'} ] - }); + expect: [{ id: 'pt1' }, { id: 'pt2' }], + }) add_test({ title: 'only obs', view: { resource: 'Observation', status: 'active', - select: [ - {column: [{path: 'id', name: 'id'}]} - ] + select: [{ column: [{ path: 'id', name: 'id' }] }], }, - expect: [{id: 'ob1'}] - }); + expect: [{ id: 'ob1' }], + }) invalid_view({ title: 'resource not specified', view: { status: 'active', - select: [ - { column: [{path: 'id', name: 'id'}] } - ] + select: [{ column: [{ path: 'id', name: 'id' }] }], }, - expectError: true - }); + expectError: true, + }) end_case() - -}); +}) diff --git a/sof-js/tests/where.test.js b/sof-js/tests/where.test.js index 5934c16..63ab337 100644 --- a/sof-js/tests/where.test.js +++ b/sof-js/tests/where.test.js @@ -1,135 +1,135 @@ -import { describe } from "bun:test"; -import { add_test, end_case, start_case } from "./test_helpers"; +import { describe } from 'bun:test' +import { add_test, end_case, start_case } from './test_helpers' const resources = [ { - resourceType: "Patient", - id: "p1", + resourceType: 'Patient', + id: 'p1', name: [ { - use: "official", - family: "f1" - } - ] + use: 'official', + family: 'f1', + }, + ], }, { - resourceType: "Patient", - id: "p2", + resourceType: 'Patient', + id: 'p2', name: [ { - use: "nickname", - family: "f2" - } - ] + use: 'nickname', + family: 'f2', + }, + ], }, { - resourceType: "Patient", - id: "p3", + resourceType: 'Patient', + id: 'p3', name: [ { - use: "nickname", - given: ["g3"], - family: "f3" - } - ] + use: 'nickname', + given: ['g3'], + family: 'f3', + }, + ], }, { - resourceType: "Observation", - id: "o1", - valueInteger: 12 + resourceType: 'Observation', + id: 'o1', + valueInteger: 12, }, { - resourceType: "Observation", - id: "o2", - valueInteger: 10 - } -]; + resourceType: 'Observation', + id: 'o2', + valueInteger: 10, + }, +] -start_case('where', 'FHIRPath `where` function.', resources); +start_case('where', 'FHIRPath `where` function.', resources) describe('where function', () => { add_test({ - title: "simple where path with result", - view: { - resource: "Patient", - select: [{ column: [{ path: "id", name: "id" }] }], - where: [{ path: "name.where(use = 'official').exists()" }] - }, - expect: [{ id: "p1" }] - }); + title: 'simple where path with result', + view: { + resource: 'Patient', + select: [{ column: [{ path: 'id', name: 'id' }] }], + where: [{ path: "name.where(use = 'official').exists()" }], + }, + expect: [{ id: 'p1' }], + }) add_test({ - title: "where path with no results", + title: 'where path with no results', view: { - resource: "Patient", - select: [{ column: [{ path: "id", name: "id" }] }], - where: [{ path: "name.where(use = 'maiden').exists()" }] + resource: 'Patient', + select: [{ column: [{ path: 'id', name: 'id' }] }], + where: [{ path: "name.where(use = 'maiden').exists()" }], }, - expect: [] - }); + expect: [], + }) add_test({ - title: "where path with greater than inequality", + title: 'where path with greater than inequality', view: { - resource: "Observation", - select: [{ column: [{ path: "id", name: "id" }] }], - where: [{ path: "where(value.ofType(integer) > 11).exists()" }] + resource: 'Observation', + select: [{ column: [{ path: 'id', name: 'id' }] }], + where: [{ path: 'where(value.ofType(integer) > 11).exists()' }], }, - expect: [{ id: "o1" }] - }); + expect: [{ id: 'o1' }], + }) add_test({ - title: "where path with less than inequality", + title: 'where path with less than inequality', view: { - resource: "Observation", - select: [{ column: [{ path: "id", name: "id" }] }], - where: [{ path: "where(value.ofType(integer) < 11).exists()" }] + resource: 'Observation', + select: [{ column: [{ path: 'id', name: 'id' }] }], + where: [{ path: 'where(value.ofType(integer) < 11).exists()' }], }, - expect: [{ id: "o2" }] - }); + expect: [{ id: 'o2' }], + }) add_test({ - title: "multiple where paths", + title: 'multiple where paths', view: { - resource: "Patient", - select: [{ column: [{ path: "id", name: "id" }] }], + resource: 'Patient', + select: [{ column: [{ path: 'id', name: 'id' }] }], where: [ { path: "name.where(use = 'official').exists()" }, - { path: "name.where(family = 'f1').exists()" } - ] + { path: "name.where(family = 'f1').exists()" }, + ], }, - expect: [{ id: "p1" }] - }); + expect: [{ id: 'p1' }], + }) add_test({ title: "where path with an 'and' connector", view: { - resource: "Patient", - select: [{ column: [{ path: "id", name: "id" }] }], - where: [{ path: "name.where(use = 'official' and family = 'f1').exists()" }] + resource: 'Patient', + select: [{ column: [{ path: 'id', name: 'id' }] }], + where: [{ path: "name.where(use = 'official' and family = 'f1').exists()" }], }, - expect: [{ id: "p1" }] - }); + expect: [{ id: 'p1' }], + }) add_test({ title: "where path with an 'or' connector", view: { - resource: "Patient", - select: [{ column: [{ path: "id", name: "id" }] }], - where: [{ path: "name.where(use = 'official' or family = 'f2').exists()" }] + resource: 'Patient', + select: [{ column: [{ path: 'id', name: 'id' }] }], + where: [{ path: "name.where(use = 'official' or family = 'f2').exists()" }], }, - expect: [{ id: "p1" }, { id: "p2" }] - }); + expect: [{ id: 'p1' }, { id: 'p2' }], + }) add_test({ - title: "where path that evaluates to true when empty", + title: 'where path that evaluates to true when empty', view: { - resource: "Patient", - select: [{ column: [{ path: "id", name: "id" }] }], - where: [{ path: "name.where(family = 'f2').empty()" }] + resource: 'Patient', + select: [{ column: [{ path: 'id', name: 'id' }] }], + where: [{ path: "name.where(family = 'f2').empty()" }], }, - expect: [{ id: "p1" }, { id: "p3" }] - }); + expect: [{ id: 'p1' }, { id: 'p3' }], + }) - end_case(); -}); + end_case() +})