Skip to content

Commit

Permalink
Split patients tests to DB and just code (for the demo)
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Sep 8, 2024
1 parent 4acb35f commit 8dcd50f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 39 deletions.
83 changes: 83 additions & 0 deletions src/suites/1.0.0-ballot/Patient/patients-2nd.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Patient } from 'fhir/r4';
import { Request } from '../../../modules/requests/request.entity';
import { isResourceValid } from '@beda.software/fhir-validator';

function patientRequestsOnlyAvailableInteractionsExists(requests: Request[]): boolean {
const availableInteractions = ['READ', 'SEARCH', 'CREATE', 'UPDATE'];
const filteredRequests = requests.filter((request) => !availableInteractions.includes(request.fhirAction));

return filteredRequests.length === 0;
}

function patientRequestsOnlyAvailableSearchParamsExists(requests: Request[]): boolean {
const availableSearchParams = [
'_id',
'birthdate',
'family',
'gender',
'identifier',
'name',
'gender-identity',
'indigenous-status',
];

return checkAvailableParams(availableSearchParams, false, requests);
}

function patientRequestsOnlyAvailableComboSearchParamsExists(requests: Request[]): boolean {
const availableComboSearchParams = ['birthdate+family', 'birthdate+name', 'gender+name', 'family+gender'];

return checkAvailableParams(availableComboSearchParams, true, requests);
}

function checkAvailableParams(availableParams: string[], combo: boolean, requests: Request[]): boolean {
const filterCombo = (param: string) => (combo ? param.includes('+') : !param.includes('+'));
const usedComboSearchParams = requests
.flatMap((request) => request.filters.map((filter) => filter.code))
.filter((param) => filterCombo(param));

const uniqueUsedComboSearchParams = [...new Set(usedComboSearchParams)];

return uniqueUsedComboSearchParams.every((searchParam) => availableParams.includes(searchParam));
}

async function patientRequestCreateValidPatient(requests: Request[]): Promise<boolean> {
const filteredRequests = requests.filter(
(request) => request.fhirAction === 'CREATE' && request.requestBody?.resourceType === 'Patient',
);
const validationStatuses = await Promise.all(
filteredRequests.map(async (request) => await isResourceValid(request.requestBody as Patient)),
);
const falseValidations = validationStatuses.filter((status) => status === false);

return falseValidations.length === 0;
}

describe('Patients test (2nd version)', () => {
let requests: Request[];

beforeAll(async () => {
requests = await global.RequestsRepository.find({
where: { session: { id: global.SESSION_ID }, resourceType: 'Patient' },
relations: ['session'],
});
});

test('Should only have available interactions', async () => {
expect(patientRequestsOnlyAvailableInteractionsExists(requests)).toBe(true);
});

test('Should only have available search params', async () => {
expect(patientRequestsOnlyAvailableSearchParamsExists(requests)).toBe(true);
});

test('Should only have available combo search params', async () => {
expect(patientRequestsOnlyAvailableComboSearchParamsExists(requests)).toBe(true);
});

test('Should only have valid resources in CREATE action', async () => {
expect(
await patientRequestCreateValidPatient(requests.filter((request) => request.fhirAction === 'CREATE')),
).toBe(true);
});
});
39 changes: 0 additions & 39 deletions src/suites/1.0.0-ballot/Patient/patients.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,6 @@ import {
import { Request } from '../../../modules/requests/request.entity';
import { isResourceValid } from '@beda.software/fhir-validator';

// function patientRequestsOnlyAvailableInteractionsExists(requests: Request[]): boolean {
// const availableInteractions = ['READ', 'SEARCH', 'CREATE', 'UPDATE'];
// const filteredRequests = requests.filter((request) => !availableInteractions.includes(request.fhirAction));

// return filteredRequests.length === 0;
// }

// function patientRequestsOnlyAvailableSearchParamsExists(requests: Request[]): boolean {
// const availableSearchParams = [
// '_id',
// 'birthdate',
// 'family',
// 'gender',
// 'identifier',
// 'name',
// 'gender-identity',
// 'indigenous-status',
// ];

// return checkAvailableParams(availableSearchParams, false, requests);
// }

// function patientRequestsOnlyAvailableComboSearchParamsExists(requests: Request[]): boolean {
// const availableComboSearchParams = ['birthdate+family', 'birthdate+name', 'gender+name', 'family+gender'];

// return checkAvailableParams(availableComboSearchParams, true, requests);
// }

// function checkAvailableParams(availableParams: string[], combo: boolean, requests: Request[]): boolean {
// const filterCombo = (param: string) => (combo ? param.includes('+') : !param.includes('+'));
// const usedComboSearchParams = requests
// .flatMap((request) => request.filters.map((filter) => filter.code))
// .filter((param) => filterCombo(param));

// const uniqueUsedComboSearchParams = [...new Set(usedComboSearchParams)];

// return uniqueUsedComboSearchParams.every((searchParam) => availableParams.includes(searchParam));
// }

async function patientRequestCreateValidPatient(requests: Request[]): Promise<boolean> {
const filteredRequests = requests.filter(
(request) => request.fhirAction === 'CREATE' && request.requestBody?.resourceType === 'Patient',
Expand Down

0 comments on commit 8dcd50f

Please sign in to comment.