Skip to content

Commit

Permalink
Merge branch 'master' into boundary-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrimes committed Feb 23, 2024
2 parents 7a9f47d + 2ccb5e2 commit efbc99d
Show file tree
Hide file tree
Showing 8 changed files with 2,675 additions and 80 deletions.
164 changes: 106 additions & 58 deletions sof-js/tests/testrunner_example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs'
import { evaluate } from '../src/index.js'


function isEqual(a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
return a.length === b.length && a.every((val, index) => isEqual(val, b[index]))
Expand Down Expand Up @@ -33,7 +32,7 @@ const canonicalize = (arr) => {
function arraysMatch(arr1, arr2) {
// Canonicalize arrays

arr1 = canonicalize(arr1) // Spread to avoid mutating the original array
arr1 = canonicalize(arr1)
arr2 = canonicalize(arr2)

// Check if arrays are of the same length
Expand Down Expand Up @@ -77,62 +76,111 @@ function arraysMatch(arr1, arr2) {
}
}

// let tests_dir = '../../tests/'
let fast_fail = true
let tests_dir = '../../legacy-tests/content/'
let files = fs.readdirSync(tests_dir)
console.log(files);
let test_results = {pass: 0, fail: 0, error: 0}
let result = files.map((f)=>{
let testcase = JSON.parse(fs.readFileSync(tests_dir + f))
console.log('run', testcase.title, `file ${f}`)
testcase.tests.map((test)=>{
if(test.expectError) {
try {
let _result = evaluate(test.view, testcase.resources)
console.log(' *', test.title, ' => ', 'fail' )
test_results.fail+=1
} catch (e) {
console.log(' *', test.title, ' => ', 'pass' )
test_results.pass+=1
}
function runThrowingTest(test, resources) {
try {
const result = evaluate(test.view, resources)
return {
passed: false,
expectedFail: true,
actual: result
}
} catch (e) {
return { passed: true }
}
}

function runTest(test, resources) {
if (test.expectError) {
return runThrowingTest(test);
}

try {
const result = evaluate(test.view, resources)

if (test.expectCount) {
const passed = result.length === test.expectCount;
return passed
? { passed }
: {
passed,
expectedCount: test.expectCount,
actual: result.length
};
} else {
try {
let result = evaluate(test.view, testcase.resources)
if(test.expectCount) {
if(result.length == test.expectCount) {
console.log(' *', test.title, ' => ', 'pass' )
test_results.pass+=1
} else {
console.log(' *', test.title, ' => ', 'fail', 'expected: ', test.expectCount, 'got ', result.length, result)
test_results.fail+=1
}
} else {
let match = arraysMatch(result, test.expect)
test.observed = result;
test.passed = match.passed
test.error = match.error
if(match.passed){
console.log(' *', test.title, ' => ', 'pass' )
test_results.pass+=1
} else {
console.log(' *', test.title, ' => ', 'fail' , 'expeceted: ', test.expect, 'got: ', result)
test_results.fail+=1
}
}
} catch (e) {
console.log(' *', test.title, ' => ', 'error', e.toString())
test_results.error+=1
if(fast_fail) {
console.log('view', JSON.stringify(test, null, " "))
throw e
}
// console.log(e)
}
const match = arraysMatch(result, test.expect)
return {
passed: match.passed,
expected: test.expect,
actual: result,
message: match.message
};
}
})
return testcase;
})
} catch (e) {
return {
passed: null,
message: e.toString()
}
}
}

function printResult(title, result) {
let testResult;
if (result.passed === true) {
testResult = "passed";
} else if (result.passed === false) {
testResult = "failed";
} else {
testResult = "error";
}

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 });
}
if (result.message) {
console.log(result.message);
}
}
}

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 testcase = JSON.parse(fs.readFileSync(tests_dir + f))
console.log('running', testcase.title, `file ${f}`)

const testResult = testcase.tests.map(test => {
let result = null;

if (test.expectError) {
result = runThrowingTest(test, testcase.resources);
printResult(test.title, result);
} else {
result = runTest(test, testcase.resources);
printResult(test.title, result);
}

if (result.passed === true) {
test_summary.pass++;
} else if (result.passed === false) {
test_summary.fail++;
} else {
test_summary.error++;
}

return { result };
});

result[f] = { tests: testResult };
});

fs.writeFileSync('../../test_report/public/test-results.json', JSON.stringify(result));
console.log(test_results)
fs.writeFileSync('../test_report/public/test-results.json', JSON.stringify(result));
console.log(test_summary)
15 changes: 7 additions & 8 deletions test_report/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import fhirpath from 'fhirpath'
import fs from 'fs'
import path from 'path'

const CONTENT = '../sof-tests/'
const files = fs.readdirSync(CONTENT)
const CONTENT = '../tests/';
const files = fs.readdirSync(CONTENT);

const allResults = []
await Promise.all(
files.map(async (file) => {
if (path.extname(file) !== '.json') return
let testData
let validate
testData = JSON.parse(fs.readFileSync(CONTENT + file))
allResults.push(testData)
if (path.extname(file) !== '.json') return;
const testData = JSON.parse(fs.readFileSync(CONTENT + file));
testData.file = path.basename(file);
allResults.push(testData);
})
)

fs.writeFileSync('public/tests.json', JSON.stringify(allResults, null, " "))
fs.writeFileSync('public/tests.json', JSON.stringify(allResults, null, " "));
1 change: 0 additions & 1 deletion test_report/public/implementations.json

This file was deleted.

29 changes: 29 additions & 0 deletions test_report/public/implementations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"name": "Ref.Impl",
"url": "https://github.com/FHIR/sql-on-fhir-v2",
"description": "Official reference implementation in JavaScript",
"testResultsUrl": "http://localhost:5000/test-results.json"
},
{
"name": "Aidbox",
"url": "https://docs.aidbox.app/storage-1/sql-on-fhir",
"description": "FHIR++ Platform by Health Samurai",
"testResultsUrl": "https://storage.googleapis.com/aidbox-public/sof-test-results.json"
},
{
"name": "Google",
"url": "TBD",
"description": "TBD"
},
{
"name": "Pathling",
"url": "TBD",
"description": "TBD"
},
{
"name": "Grahame Impl",
"url": "TBD",
"description": "TBD"
}
]
2,530 changes: 2,529 additions & 1 deletion test_report/public/test-results.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions test_report/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@
export let url = "";
onMount(async function () {
console.log('add location change')
window.addEventListener('hashchange', function(){
hash = window.location.hash.substring(1);
console.log(hash);
})
})
let hash = window.location.hash.substring(1);
console.log(hash);
</script>

<Tailwindcss />

<div class="flex flex-col" style="height: 100%">
<div class="flex space-x-4 px-4 py-1 border-b items-center" style="background-color: rgb(239 253 254);">
<a href="" class="flex items-center px-4">
<a href="" class="flex items-center px-4">
<img src="logo.jpeg" class="w-10 h-10" style="" /> SQL on FHIR 2.0
</a>
<a href="#pg" class="px-2 hover:text-blue-500 hover:bg-gray-150">Playground</a>
Expand Down
8 changes: 3 additions & 5 deletions test_report/src/Impls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
onMount(async function () {
tests = await load('tests.json');
console.log('tests',tests);
var tmp = await load('implementations.json');
const tmp = await load('implementations.json');
for (const impl of tmp) {
if (impl.testResultsUrl){
let res = await load(impl.testResultsUrl);
impl.results = res;
console.log('res', res);
}
}
console.log('impls', tmp);
impls = tmp;
console.dir(tests, {depth: null})
console.dir(impls, {depth: null})
});
</script>
Expand Down
1 change: 0 additions & 1 deletion test_report/src/Playground.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
function tabelize(v, results) {
let columns = get_columns(v)
console.log('?',columns);
let rows = []
for( var row of results) {
rows.push(columns.map((k)=>{
Expand Down
2 changes: 0 additions & 2 deletions test_report/src/Tests.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
onMount(async function () {
menu = await load('tests.json');
impls = await load('implementations.json');
console.log('?', impls);
let hash = window.location.hash.substring(1);
current = menu[0];
menu.forEach((x)=> {
console.log('*',hash, x.file);
if(x.file == hash) {
current = x
};
Expand Down

0 comments on commit efbc99d

Please sign in to comment.