From 46f7aae74b5f4dd0799d3d3182e594cd08e3c8b0 Mon Sep 17 00:00:00 2001 From: nvim Date: Wed, 11 Sep 2024 13:50:30 +0200 Subject: [PATCH] Remove helpers for tests with db Ref: https://github.com/beda-software/fhir-client-testing-tool/issues/7 --- .../src/utils/clientTestingHelpers.spec.ts | 85 ----------------- .../app/src/utils/clientTestingHelpers.ts | 71 -------------- .../client-testing-demo-tests-db/src/index.ts | 95 +++++++++---------- 3 files changed, 43 insertions(+), 208 deletions(-) delete mode 100644 packages/app/src/utils/clientTestingHelpers.spec.ts delete mode 100644 packages/app/src/utils/clientTestingHelpers.ts diff --git a/packages/app/src/utils/clientTestingHelpers.spec.ts b/packages/app/src/utils/clientTestingHelpers.spec.ts deleted file mode 100644 index 2b6d9f4..0000000 --- a/packages/app/src/utils/clientTestingHelpers.spec.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { DataSource } from 'typeorm'; -import { Request } from '../modules/requests/request.entity'; -import { Session } from '../modules/sessions/session.entity'; -import { TestRun } from '../modules/test_runs/testRun.entity'; -import { - getRequestsWithUnavailableComboSearchParams, - getRequestsWithUnavailableSearchParams, -} from './clientTestingHelpers'; -import { v4 as uuidv4 } from 'uuid'; -import { createRequestObject } from './data'; - -const TestDataSource = new DataSource({ - type: 'postgres', - host: 'postgres', - port: 5432, - username: process.env.POSTGRES_USER, - password: process.env.POSTGRES_PASSWORD, - database: process.env.POSTGRES_DB, - entities: [Request, Session, TestRun], - synchronize: true, - dropSchema: true, - logging: false, -}); - -const sessionUUID = uuidv4(); - -beforeAll(async () => { - await TestDataSource.initialize(); - const sessionRepository = TestDataSource.getRepository(Session); - const requestsRepository = TestDataSource.getRepository(Request); - const sessionEntity = await sessionRepository.save({ - id: sessionUUID, - target: 'http://test.com', - }); - - for (const searchParameters of [ - '/Patient?_id=123', - '/Patient?family=123', - '/Patient?family=123&gender=unknown', - '/Patient?family=123&name=unknown', - '/Patient?test=123&gender=unknown', - '/Observation?_id=123', - ]) { - await requestsRepository.save( - createRequestObject( - sessionUUID, - 'http://test.com', - sessionEntity, - { - method: 'GET', - headers: { 'user-agent': 'Mozilla/5.0' }, - body: {}, - originalUrl: `${sessionEntity.target}/sessions/${sessionUUID}/${searchParameters}`, - ip: '192.168.0.1', - }, - { statusCode: 200 }, - '{}', - ), - ); - } -}); - -afterAll(async () => { - await TestDataSource.destroy(); -}); - -describe('Client testing helpers', () => { - const requestsRepository = TestDataSource.getRepository(Request); - it('Should return correct request entities via getRequestsWithUnavailableSearchParams', async () => { - const requests = await getRequestsWithUnavailableSearchParams(requestsRepository, sessionUUID, 'Patient', [ - '_id', - ]); - - expect(requests).toHaveLength(1); - }); - - it('Should return correct request entities via getRequestsWithUnavailableComboSearchParams', async () => { - const requests = await getRequestsWithUnavailableComboSearchParams(requestsRepository, sessionUUID, 'Patient', [ - ['family', 'name'], - ['family', 'gender'], - ]); - - expect(requests).toHaveLength(1); - }); -}); diff --git a/packages/app/src/utils/clientTestingHelpers.ts b/packages/app/src/utils/clientTestingHelpers.ts deleted file mode 100644 index bf56053..0000000 --- a/packages/app/src/utils/clientTestingHelpers.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { Request } from '../modules/requests/request.entity'; -import { Repository } from 'typeorm'; - -/** - * Retrieves a list of requests with unavailable search parameters. - * - * @param repository - The repository to query for requests. - * @param sessionId - The session ID to filter requests by. - * @param resourceType - The resource type to filter requests by. - * @param availableSearchParams - An array of available search parameters. - * @returns A promise that resolves to an array of requests. - */ -export async function getRequestsWithUnavailableSearchParams( - repository: Repository, - sessionId: string, - resourceType: string, - availableSearchParams: string[], -): Promise { - // 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') - - return await repository - .createQueryBuilder('request') - .where('request.sessionId = :sessionId', { sessionId }) - .andWhere('request.resourceType = :resourceType', { resourceType }) - .andWhere('request.fhirAction = :action', { action: 'SEARCH' }) - .andWhere('jsonb_array_length(request.filters) = 1') - .andWhere("request.filters->0->>'code' NOT IN (:...availableSearchParams)", { availableSearchParams }) - .getMany(); -} - -/** - * Retrieves a list of requests with unavailable combination search parameters. - * - * @param repository - The repository to query requests from. - * @param sessionId - The session ID to filter requests by. - * @param resourceType - The resource type to filter requests by. - * @param availableSearchParams - The available search parameters. - * @returns A promise that resolves to an array of requests. - */ -export async function getRequestsWithUnavailableComboSearchParams( - repository: Repository, - sessionId: string, - resourceType: string, - availableSearchParams: string[][], -): Promise { - // 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']] - return await repository - .createQueryBuilder('request') - .where('request.sessionId = :sessionId', { sessionId }) - .andWhere('request.resourceType = :resourceType', { resourceType }) - .andWhere('request.fhirAction = :action', { action: 'SEARCH' }) - .andWhere('jsonb_array_length(request.filters) > 1') - .andWhere('NOT request.filters_codes <@ :codes', { - codes: availableSearchParams, - }) - .getMany(); -} diff --git a/packages/client-testing-demo-tests-db/src/index.ts b/packages/client-testing-demo-tests-db/src/index.ts index be9579a..588adcb 100644 --- a/packages/client-testing-demo-tests-db/src/index.ts +++ b/packages/client-testing-demo-tests-db/src/index.ts @@ -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 { - 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', @@ -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); }); }); }