Skip to content

Commit

Permalink
Fix expectation in basic > where returns non-boolean for some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrimes committed Feb 28, 2024
1 parent d07b16e commit 403227b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 116 deletions.
237 changes: 122 additions & 115 deletions sof-js/tests/1_basic.test.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,102 @@
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', ()=>{
import { describe, expect, test } from 'bun:test'
import { get_columns, row_product } from '../src/index.js'
import { add_test, add_throwing_test, end_case, start_case } 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 }])
})

expect(get_columns(
{
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'])

{ 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'])
})

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',
Expand All @@ -98,23 +106,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({
Expand All @@ -125,13 +133,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({
Expand All @@ -140,66 +148,66 @@ 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({
title: 'where - 1',
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({
title: 'where - 2',
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_throwing_test({
title: 'where returns non-boolean for some cases',
view: {
resource: 'Patient',
status: 'active',
select: [{column: [{name: 'id', path: 'id'}]}],
where: [{path: 'active'}]
select: [{ column: [{ name: 'id', path: 'id' }] }],
where: [{ path: 'active' }],
},
expectError: true
expect: [{ id: 'pt1' }],
})

add_test({
title: 'where as expr - 1',
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({
title: 'where as expr - 2',
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({
Expand All @@ -208,18 +216,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'}
]
});

end_case();
});
{ c_id: 'pt1', s_id: 'pt1' },
{ c_id: 'pt2', s_id: 'pt2' },
{ c_id: 'pt3', s_id: 'pt3' },
],
})

end_case()
})
6 changes: 5 additions & 1 deletion tests/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@
}
]
},
"expectError": true
"expect": [
{
"id": "pt1"
}
]
},
{
"title": "where as expr - 1",
Expand Down

0 comments on commit 403227b

Please sign in to comment.