Skip to content

Commit

Permalink
Remove helpers for tests with db
Browse files Browse the repository at this point in the history
Ref: #7
  • Loading branch information
projkov committed Sep 11, 2024
1 parent 39016a4 commit 46f7aae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 208 deletions.
85 changes: 0 additions & 85 deletions packages/app/src/utils/clientTestingHelpers.spec.ts

This file was deleted.

71 changes: 0 additions & 71 deletions packages/app/src/utils/clientTestingHelpers.ts

This file was deleted.

95 changes: 43 additions & 52 deletions packages/client-testing-demo-tests-db/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
import { In, Not } from 'typeorm';
import { Patient } from 'fhir/r4';
import {
getRequestsWithUnavailableComboSearchParams,
getRequestsWithUnavailableSearchParams,
} from '@beda.software/app/src//utils/clientTestingHelpers';
import { Request } from '@beda.software/app/src/modules/requests/request.entity';
import { isResourceValid } from '@beda.software/fhir-validator';

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;
}

export function patientDemoTestDB() {
describe('Patients test', () => {
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 () => {
requests = await global.RequestsRepository.find({
const requests = await global.RequestsRepository.find({
where: {
session: { id: global.SESSION_ID },
resourceType: 'Patient',
Expand All @@ -43,34 +15,53 @@ export function patientDemoTestDB() {
});

test('Should only have available search params', async () => {
const requests = await getRequestsWithUnavailableSearchParams(
global.RequestsRepository,
global.SESSION_ID,
'Patient',
['_id', 'birthdate', 'family', 'gender', 'identifier', 'name', 'gender-identity', 'indigenous-status'],
);
// NOTE: SQL query example
// SELECT *
// FROM request
// WHERE sessionId = 123
// AND resourceType = 'Patient'
// AND fhirAction = 'SEARCH'
// AND jsonb_array_length(filters) = 1
// AND filters->0->>'code' NOT IN ('_id', 'family')

const requests = await global.RequestsRepository
.createQueryBuilder('request')
.where('request.sessionId = :sessionId', { sessionId: global.SESSION_ID })
.andWhere('request.resourceType = :resourceType', { resourceType: 'Patient' })
.andWhere('request.fhirAction = :action', { action: 'SEARCH' })
.andWhere('jsonb_array_length(request.filters) = 1')
.andWhere("request.filters->0->>'code' NOT IN (:...availableSearchParams)", { availableSearchParams: ['_id', 'birthdate', 'family', 'gender', 'identifier', 'name', 'gender-identity', 'indigenous-status'] })
.getMany();

expect(requests.length).toBe(0);
});

test('Should only have available combo search params', async () => {
const requests = await getRequestsWithUnavailableComboSearchParams(
global.RequestsRepository,
global.SESSION_ID,
'Patient',
[
['birthdate', 'family'],
['birthdate', 'name'],
['gender', 'name'],
['family', 'gender'],
],
);
expect(requests.length).toBe(0);
});
// NOTE: SQL query example
// SELECT *
// FROM request
// WHERE sessionId = 123
// AND resourceType = 'Patient'
// AND fhirAction = 'SEARCH'
// AND jsonb_array_length(filters) > 1
// AND NOT filters_codes <@ array[array['birthdate','family'], array['birthdate','name']]
const requests = await global.RequestsRepository
.createQueryBuilder('request')
.where('request.sessionId = :sessionId', { sessionId: global.SESSION_ID })
.andWhere('request.resourceType = :resourceType', { resourceType: 'Patient' })
.andWhere('request.fhirAction = :action', { action: 'SEARCH' })
.andWhere('jsonb_array_length(request.filters) > 1')
.andWhere('NOT request.filters_codes <@ :codes', {
codes: [
['birthdate', 'family'],
['birthdate', 'name'],
['gender', 'name'],
['family', 'gender'],
],
})
.getMany();

test('Should only have valid resources in CREATE action', async () => {
expect(
await patientRequestCreateValidPatient(requests.filter((request) => request.fhirAction === 'CREATE')),
).toBe(true);
expect(requests.length).toBe(0);
});
});
}

0 comments on commit 46f7aae

Please sign in to comment.