Skip to content

Commit

Permalink
Run test data file through API
Browse files Browse the repository at this point in the history
Signed-off-by: JP Lomas <[email protected]>
  • Loading branch information
jplomas committed Sep 20, 2024
1 parent f75f057 commit ef86335
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
50 changes: 50 additions & 0 deletions test/api.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeAll, describe, expect, expectTypeOf, test } from 'vitest';
import createServer from '../src/server';
import { maxIndication, getIndications } from './calculator.test.mjs';

createServer();

Expand Down Expand Up @@ -147,3 +148,52 @@ describe('POST request to /nela-risk with invalid input returns an error', () =>
expect(body.error).toBe('Missing required fields');
});
});

describe('Test vectors file through API', () => {
// load sim.csv
const fs = require('fs');
const path = require('path');
// read whole of sim.csv into variable
const data = fs.readFileSync(path.resolve(__dirname, '../test/sim.csv'), 'utf8');
// split into lines
const lines = data.split('\n');
// for each line, split into fields
for (let i = 1; i < lines.length; i++) {
// for (let i = 4175; i < 4176; i++) {
const fields = lines[i].split(',');
// for each field, convert to correct type
const input = {
age: parseInt(fields[1]),
heartRate: parseInt(fields[4]),
systolicBloodPressure: parseInt(fields[5]),
urea: parseFloat(fields[6]),
whiteBloodCellCount: parseFloat(fields[7]),
albumin: parseFloat(fields[3]),
asaGrade: parseInt(fields[2]),
glasgowComaScore: parseInt(fields[8]),
malignancy: fields[9],
dyspnoea: fields[10],
urgency: fields[11],
indicationForSurgery: maxIndication(getIndications(fields)),
soiling: fields[43] === 'Free pus blood or bowel contents',
};
// fields[46] is intercept - skip if empty
if (fields[46] !== '') {
// check predictedRisk is correct
test(`predictedRisk is correct for test vector ${i}`, async () => {
let response;
let body;

response = await fetch('http://localhost:3000/nela-risk', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(input),
});
body = await response.json();
expect(body.predictedRisk).toBe(parseFloat(fields[78]));
});
}
}
});
4 changes: 3 additions & 1 deletion test/calculator.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ describe('Ancillary tests', () => {
});
});

describe('Test vectors file', () => {
describe('Test vectors file through calculation function', () => {
// load sim.csv
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -505,3 +505,5 @@ describe('Test vectors file', () => {
}
}
});

export { getIndications, maxIndication };

0 comments on commit ef86335

Please sign in to comment.