-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcql-tests-runner.js
304 lines (272 loc) · 10.3 KB
/
cql-tests-runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/node
const fs = require('fs');
const path = require('path');
const { format } = require('date-fns');
const loadTests = require('./loadTests');
const colors = require('colors/safe');
const currentDate = format(new Date(), 'yyyyMMddhhmm');
const axios = require('axios');
const CQLEngine = require('./lib/cql-engine/CQLEngine.js');
const ConfigLoader = require('./configLoader');
const config = new ConfigLoader();
const resultsShared = require('./resultsShared');
// const TestResults = require('./lib/test-results/TestResults.js');
const CQLTestResults = require('./lib/test-results/CQLTestResults.js');
// Setup for running both $cql and Library/$evaluate
// Expand outputType to allow Parameters representation
// Test Container Structure:
/*
class Tests {
name: String
version: String // The version in which the capability being tested was introduced
description: String
reference: String // A reference to the section of the spec being tested
notes: String
group: TestGroup[]
}
class TestGroup {
name: String
version: String // The version in which the capability being tested was introduced
description: String
reference: String // A reference to the section of the spec being tested
notes: String
test: Test[]
}
class Test {
name: String
version: String // The version in which the capability being tested was introduced
description: String
reference: String // A reference to the section of the spec being tested
inputFile: String // Input data, if any
predicate: Boolean // True if this test represents a predicate
mode: String // strict | loose
ordered: Boolean // Whether the results are expected to be ordered, false if not present
checkOrderedFunctions: Boolean // Whether to ensure that attempting to use ordered functions with an unordered input should throw (e.g., using .skip() on an unordered list)
expression: String | { text: String, invalid: false, semantic, true }
output: String([]) | { text: String, type: boolean | code | date | dateTime | decimal | integer | long | quantity | string | time }([])
}
*/
class Result {
testStatus; // String: pass | fail | skip | error
responseStatus; // Integer
actual; // String
expected; // String
error; // Error
constructor(testsName, groupName, test) {
this.testsName = testsName;
this.groupName = groupName;
this.testName = test.name;
if (typeof test.expression !== 'string') {
this.invalid = test.expression.invalid;
this.expression = test.expression.text;
}
else {
this.invalid = 'false';
this.expression = test.expression;
}
if (test.output !== undefined) {
if (typeof test.output !== 'string') {
// TODO: Structure the result if it can be structured (i.e. is one of the expected types)
this.expected = test.output.text;
}
else {
this.expected = test.output;
}
}
else {
this.testStatus = 'skip';
}
}
}
// Iterate through tests
async function main() {
let serverBaseUrl = config.FhirServer.BaseUrl
let cqlEndpoint = config.CqlEndpoint;
let outputPath = config.Tests.ResultsPath;
//TODO: CQLEngine needs adjustments to handle Library/$evaluate. Config forces use of proper operation name and baseURL
let cqlEngine = new CQLEngine(serverBaseUrl, cqlEndpoint);
cqlEngine.cqlVersion = '1.5';
var x;
await require('./cvl/cvlLoader.js').then(([{ default: cvl }]) => { x = cvl });
const tests = loadTests.load();
// Set this to true to run only the first group of tests
const quickTest = config.Debug.QuickTest
const emptyResults = await resultsShared.generateEmptyResults(tests, quickTest);
const skipMap = config.skipListMap();
let results = new CQLTestResults(cqlEngine);
for (let testFile of emptyResults) {
for (let result of testFile) {
await runTest(result, cqlEngine.apiUrl, x, skipMap);
results.add(result);
}
}
results.save(outputPath);
results.validate();
};
main();
async function runTest(result, apiUrl, cvl, skipMap) {
const key = `${result.testsName}-${result.groupName}-${result.testName}`;
if (result.testStatus === 'skip') {
result.SkipMessage = 'Skipped by cql-tests-runner';
return result;
} else if (skipMap.has(key)) {
let reason = skipMap.get(key);
result.SkipMessage = `Skipped by config: ${reason}"`;
result.testStatus = 'skip';
return result;
}
//TODO: handle instance api location for library/$evaluate
let data = resultsShared.generateParametersResource(result, config.FhirServer.CqlOperation);
try {
console.log('Running test %s:%s:%s', result.testsName, result.groupName, result.testName);
const response = await axios.post(apiUrl, data, {
headers: {
'Content-Type': 'application/json',
}
});
result.responseStatus = response.status;
const responseBody = response.data;
result.actual = extractResult(responseBody);
const invalid = result.invalid;
if (invalid === 'true' || invalid === 'semantic') {
// TODO: Validate the error message is as expected...
result.testStatus = response.status === 200 ? 'fail' : 'pass';
}
else {
if (response.status === 200) {
result.testStatus = resultsEqual(cvl.parse(result.expected), result.actual) ? 'pass' : 'fail';
}
else {
result.testStatus = 'fail';
}
}
}
catch (error) {
result.testStatus = 'error';
result.error = error;
};
console.log('Test %s:%s:%s status: %s expected: %s actual: %s', result.testsName, result.groupName, result.name, result.testStatus, result.expected, result.actual);
return result;
};
function resultsEqual(expected, actual) {
if (expected === undefined && actual === undefined) {
return true;
}
else if (typeof expected === 'number') {
return Math.abs(actual - expected) < 0.00000001;
}
else {
return expected === actual
}
}
function extractResult(response) {
var result;
if (response.hasOwnProperty('resourceType') && response.resourceType === 'Parameters') {
for (let p of response.parameter) {
// if (p.name === 'return') {
if (result === undefined) {
if (p.hasOwnProperty("valueBoolean")) {
result = p.valueBoolean;
}
else if (p.hasOwnProperty("valueInteger")) {
result = p.valueInteger;
}
else if (p.hasOwnProperty("valueString")) {
result = p.valueString;
}
else if (p.hasOwnProperty("valueDecimal")) {
result = p.valueDecimal;
}
else if (p.hasOwnProperty("valueDate")) {
result = '@' + p.valueDate.toString();
}
else if (p.hasOwnProperty("valueDateTime")) {
result = '@' + p.valueDateTime.toString();
if (result.length <= 10) {
result = result + 'T';
}
}
else if (p.hasOwnProperty("valueTime")) {
result = '@T' + p.valueTime.toString();
}
else if (p.hasOwnProperty("valueQuantity")) {
result = { value: p.valueQuantity.value, unit: p.valueQuantity.code };
}
else {
result = null;
}
// TODO: Handle other types: Period, Range, Ratio, code, Coding, CodeableConcept
}
else {
// TODO: Handle lists
// TODO: Handle tuples
result = undefined;
break;
}
// }
}
if (result !== undefined) {
return result.toString();
}
}
// Anything that can't be structured directly, return as the actual output...
return JSON.stringify(response);
}
// Output test results
function logResult(result, outputPath) {
const fileName = `${result.testsName}_${result.groupName}_${result.testName}_${currentDate}_results.json`;
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
const filePath = path.join(outputPath, fileName);
fs.writeFileSync(filePath, JSON.stringify(result, null, 2), (error) => {
if (error) throw error;
});
}
function logResults(cqlengine, results, outputPath) {
if (cqlengine instanceof CQLEngine) {
const fileName = `${currentDate}_results.json`;
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
const filePath = path.join(outputPath, fileName);
const result = new TestResults(cqlengine, summarizeResults(results), null, results);
// const result = {
// cqlengine,
// summary: summarizeResults(results),
// results: results
// };
fs.writeFileSync(filePath, JSON.stringify(result, null, 2), (error) => {
if (error) throw error;
});
} else {
throw new Error(`Not a valid CQL Engine Instance !!`)
}
}
function summarizeResults(results) {
let passCount = 0;
let skipCount = 0;
let failCount = 0;
let errorCount = 0;
for (let r of results) {
if (r.testStatus === 'pass') {
passCount++;
}
else if (r.testStatus === 'skip') {
skipCount++;
}
else if (r.testStatus === 'fail') {
failCount++;
}
else if (r.testStatus === 'error') {
errorCount++;
}
}
console.log("pass: %d skip: %d fail: %d error: %d", passCount, skipCount, failCount, errorCount);
return {
pass: passCount,
skip: skipCount,
fail: failCount,
error: errorCount
}
}