From ef8633596e1049c7317ddccc26b0dc1e7cae5981 Mon Sep 17 00:00:00 2001 From: JP Lomas Date: Fri, 20 Sep 2024 19:53:17 +0100 Subject: [PATCH] Run test data file through API Signed-off-by: JP Lomas --- test/api.test.mjs | 50 ++++++++++++++++++++++++++++++++++++++++ test/calculator.test.mjs | 4 +++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/test/api.test.mjs b/test/api.test.mjs index d7052cb..357493c 100644 --- a/test/api.test.mjs +++ b/test/api.test.mjs @@ -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(); @@ -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])); + }); + } + } +}); diff --git a/test/calculator.test.mjs b/test/calculator.test.mjs index 94c317e..a9d46a4 100644 --- a/test/calculator.test.mjs +++ b/test/calculator.test.mjs @@ -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'); @@ -505,3 +505,5 @@ describe('Test vectors file', () => { } } }); + +export { getIndications, maxIndication };