From dd0ba0131f1176e966e5aa1c460f306c414e7b06 Mon Sep 17 00:00:00 2001 From: nvim Date: Thu, 12 Sep 2024 10:01:37 +0200 Subject: [PATCH] Use tests without export Ref: https://github.com/beda-software/fhir-client-testing-tool/issues/7 --- .../modules/test_runs/testRun.controller.ts | 13 +-- .../1.0.0-ballot/Patient/patient-db.spec.ts | 3 - .../1.0.0-ballot/Patient/patient.spec.ts | 3 - .../client-testing-demo-tests-db/src/index.ts | 65 -------------- .../src/patientdb.spec.ts | 63 ++++++++++++++ .../src/index.ts | 84 ------------------- .../src/patientjs.spec.ts | 82 ++++++++++++++++++ 7 files changed, 152 insertions(+), 161 deletions(-) delete mode 100644 packages/app/src/suites/1.0.0-ballot/Patient/patient-db.spec.ts delete mode 100644 packages/app/src/suites/1.0.0-ballot/Patient/patient.spec.ts delete mode 100644 packages/client-testing-demo-tests-db/src/index.ts create mode 100644 packages/client-testing-demo-tests-db/src/patientdb.spec.ts delete mode 100644 packages/client-testing-demo-tests-standard/src/index.ts create mode 100644 packages/client-testing-demo-tests-standard/src/patientjs.spec.ts diff --git a/packages/app/src/modules/test_runs/testRun.controller.ts b/packages/app/src/modules/test_runs/testRun.controller.ts index a880783..2b9f2e8 100644 --- a/packages/app/src/modules/test_runs/testRun.controller.ts +++ b/packages/app/src/modules/test_runs/testRun.controller.ts @@ -9,9 +9,9 @@ import { TestRun } from './testRun.entity'; import { createTestListObject } from '../../utils/data'; const testOptions = { - globalSetup: './src/utils/setup/jest.setup.ts', - globalTeardown: './src/utils/setup/jest.teardown.ts', - rootDir: './', + globalSetup: './app/src/utils/setup/jest.setup.ts', + globalTeardown: './app/src/utils/setup/jest.teardown.ts', + rootDir: '../', testEnvironment: 'node', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], maxWorkers: 1, @@ -30,7 +30,8 @@ export class TestRunController { @ApiOperation({ summary: 'Create a new test session' }) async create(@Res() res: Response, @Body() createTestSessionDto: InitiateTestRunDto) { const { suiteId, sessionId, testId } = createTestSessionDto; - const testRegex = `/src/suites/${suiteId}/.*\\.(test|spec)\\.[jt]sx?$`; + // const testRegex = `/src/suites/${suiteId}/.*\\.(test|spec)\\.[jt]sx?$`; + const testRegex = `./${suiteId}/src/.*\\.spec\\.ts$`; const optionsWithTest = testId ? { testNamePattern: testId } : {}; const currentSession = await this.sessionService.findOne(sessionId); @@ -49,7 +50,7 @@ export class TestRunController { reporters: [ 'default', [ - '/src/custom-reporter.js', + '/app/src/custom-reporter.js', { TEST_RUN_ID: testRun.id, TEST_RUN_SERVICE: this.testRunService, @@ -81,7 +82,7 @@ export class TestRunController { @Get('test-list') @ApiOperation({ summary: 'List all tests' }) async list(@Res() res: Response) { - const testRegex = `/src/suites/.*\\.(test|spec)\\.[jt]sx?$`; + const testRegex = `./.*\\.(test|spec)\\.[jt]sx?$`; const options = { ...testOptions, diff --git a/packages/app/src/suites/1.0.0-ballot/Patient/patient-db.spec.ts b/packages/app/src/suites/1.0.0-ballot/Patient/patient-db.spec.ts deleted file mode 100644 index 055aaaa..0000000 --- a/packages/app/src/suites/1.0.0-ballot/Patient/patient-db.spec.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { patientDemoTestDB } from '@beda.software/client-testing-demo-tests-db'; - -patientDemoTestDB(); diff --git a/packages/app/src/suites/1.0.0-ballot/Patient/patient.spec.ts b/packages/app/src/suites/1.0.0-ballot/Patient/patient.spec.ts deleted file mode 100644 index 905959f..0000000 --- a/packages/app/src/suites/1.0.0-ballot/Patient/patient.spec.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { patientDemoTest } from '@beda.software/client-testing-demo-tests-standard'; - -patientDemoTest(); diff --git a/packages/client-testing-demo-tests-db/src/index.ts b/packages/client-testing-demo-tests-db/src/index.ts deleted file mode 100644 index 03064a1..0000000 --- a/packages/client-testing-demo-tests-db/src/index.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { In, Not } from 'typeorm'; - -export function patientDemoTestDB() { - describe('Patients test', () => { - test('Should only have available interactions', async () => { - const requests = await global.RequestsRepository.find({ - where: { - session: { id: global.SESSION_ID }, - resourceType: 'Patient', - fhirAction: Not(In(['READ', 'SEARCH', 'CREATE', 'UPDATE'])), - }, - relations: ['session'], - }); - expect(requests.length).toBe(0); - }); - - test('Should only have available search params', async () => { - // 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 () => { - // 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(); - - expect(requests.length).toBe(0); - }); - }); -} diff --git a/packages/client-testing-demo-tests-db/src/patientdb.spec.ts b/packages/client-testing-demo-tests-db/src/patientdb.spec.ts new file mode 100644 index 0000000..4689dda --- /dev/null +++ b/packages/client-testing-demo-tests-db/src/patientdb.spec.ts @@ -0,0 +1,63 @@ +import { In, Not } from 'typeorm'; + +describe('Patients test', () => { + test('Should only have available interactions', async () => { + const requests = await global.RequestsRepository.find({ + where: { + session: { id: global.SESSION_ID }, + resourceType: 'Patient', + fhirAction: Not(In(['READ', 'SEARCH', 'CREATE', 'UPDATE'])), + }, + relations: ['session'], + }); + expect(requests.length).toBe(0); + }); + + test('Should only have available search params', async () => { + // 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 () => { + // 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(); + + expect(requests.length).toBe(0); + }); +}); diff --git a/packages/client-testing-demo-tests-standard/src/index.ts b/packages/client-testing-demo-tests-standard/src/index.ts deleted file mode 100644 index 3f9e5bb..0000000 --- a/packages/client-testing-demo-tests-standard/src/index.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { Request } from '@beda.software/app/src/modules/requests/request.entity'; -import { isResourceValid } from '@beda.software/fhir-validator'; - -/** - * Temporary function to check if a set is a subset of another set - */ -function isSubsetOf(setA: Set, setB: Set): boolean { - if (setA.size > setB.size) { - return false; - } - - for (const elem of setA) { - if (!setB.has(elem)) { - return false; - } - } - return true; -} - -export function patientDemoTest() { - describe('Patients test (2nd version)', () => { - let requests: Request[] = []; - beforeAll(async () => { - requests = global.requests.filter( - (request) => request.resourceType === 'Patient' && request.session.id === global.SESSION_ID, - ); - }); - - test('Should only have available interactions', async () => { - const availableInteractions = ['READ', 'SEARCH', 'CREATE', 'UPDATE']; - const filteredRequests = requests.filter((request) => !availableInteractions.includes(request.fhirAction)); - - expect(filteredRequests.length).toBe(0); - }); - - test('Should only have available search params', async () => { - const availableSearchParams = new Set([ - '_id', - 'birthdate', - 'family', - 'gender', - 'identifier', - 'name', - 'gender-identity', - 'indigenous-status', - ]); - const filteredRequests = new Set( - requests - .filter((request) => request.filtersCodes.length === 1) - .map((request) => request.filtersCodes[0]), - ); - - expect(isSubsetOf(filteredRequests, availableSearchParams)).toBe(true); - }); - - test('Should only have available combo search params', async () => { - const availableComboSearchParams = new Set([ - 'birthdate+family', - 'birthdate+name', - 'gender+name', - 'family+gender', - ]); - const filteredRequests = new Set( - requests - .filter((request) => request.filtersCodes.length > 1) - .map((request) => request.filtersCodes.join('+')), - ); - - expect(isSubsetOf(filteredRequests, availableComboSearchParams)).toBe(true); - }); - - test('Should only have valid resources in CREATE action', async () => { - 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)), - ); - const falseValidations = validationStatuses.filter((status) => status === false); - - expect(falseValidations.length).toBe(0); - }); - }); -} diff --git a/packages/client-testing-demo-tests-standard/src/patientjs.spec.ts b/packages/client-testing-demo-tests-standard/src/patientjs.spec.ts new file mode 100644 index 0000000..319161c --- /dev/null +++ b/packages/client-testing-demo-tests-standard/src/patientjs.spec.ts @@ -0,0 +1,82 @@ +import { Request } from '@beda.software/app/src/modules/requests/request.entity'; +import { isResourceValid } from '@beda.software/fhir-validator'; + +/** + * Temporary function to check if a set is a subset of another set + */ +function isSubsetOf(setA: Set, setB: Set): boolean { + if (setA.size > setB.size) { + return false; + } + + for (const elem of setA) { + if (!setB.has(elem)) { + return false; + } + } + return true; +} + +describe('Patients test (2nd version)', () => { + let requests: Request[] = []; + beforeAll(async () => { + requests = global.requests.filter( + (request) => request.resourceType === 'Patient' && request.session.id === global.SESSION_ID, + ); + }); + + test('Should only have available interactions', async () => { + const availableInteractions = ['READ', 'SEARCH', 'CREATE', 'UPDATE']; + const filteredRequests = requests.filter((request) => !availableInteractions.includes(request.fhirAction)); + + expect(filteredRequests.length).toBe(0); + }); + + test('Should only have available search params', async () => { + const availableSearchParams = new Set([ + '_id', + 'birthdate', + 'family', + 'gender', + 'identifier', + 'name', + 'gender-identity', + 'indigenous-status', + ]); + const filteredRequests = new Set( + requests + .filter((request) => request.filtersCodes.length === 1) + .map((request) => request.filtersCodes[0]), + ); + + expect(isSubsetOf(filteredRequests, availableSearchParams)).toBe(true); + }); + + test('Should only have available combo search params', async () => { + const availableComboSearchParams = new Set([ + 'birthdate+family', + 'birthdate+name', + 'gender+name', + 'family+gender', + ]); + const filteredRequests = new Set( + requests + .filter((request) => request.filtersCodes.length > 1) + .map((request) => request.filtersCodes.join('+')), + ); + + expect(isSubsetOf(filteredRequests, availableComboSearchParams)).toBe(true); + }); + + test('Should only have valid resources in CREATE action', async () => { + 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)), + ); + const falseValidations = validationStatuses.filter((status) => status === false); + + expect(falseValidations.length).toBe(0); + }); +});