Skip to content

Commit

Permalink
Update add tests for the SQL combo parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Aug 22, 2024
1 parent 7c11c95 commit f3d6abf
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ tests_run:
$(compose) $(tests_env) $(tests_profile) up --abort-on-container-exit

tests_down:
$(compose) $(tests_env) $(tests_profile) down
$(compose) $(tests_env) $(tests_profile) down -v
26 changes: 4 additions & 22 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ services:
profiles:
- development
- production
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
env_file:
- ./envs/.env.development
test:
build:
context: ./
Expand All @@ -26,10 +24,8 @@ services:
- postgres
profiles:
- tests
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
env_file:
- ./envs/.env.test
postgres:
image: postgres:15
ports:
Expand All @@ -44,20 +40,6 @@ services:
- development
- tests
- production
pgweb:
container_name: pgweb
restart: always
image: sosedoff/pgweb
ports:
- "8081:8081"
links:
- postgres:postgres
environment:
- DATABASE_URL=postgres://myuser:mypassword@postgres:5432/mydatabase?sslmode=disable
depends_on:
- postgres
profiles:
- development
validator-api:
image: pavelrozhkov/wrapper:6.3.11
ports:
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ async function bootstrap() {
SwaggerModule.setup('api', app, document);

await app.listen(8080);
await initialValidateResource();
initialValidateResource();
}
bootstrap();
3 changes: 3 additions & 0 deletions src/modules/requests/request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export class CreateRequestDto {
@ApiProperty({ description: 'Filters applied to the request' })
filters: jsonbType;

@ApiProperty({ description: 'Filters codes applied to the request' })
filtersCodes: string[];

@ApiProperty({ description: 'HTTP status code of the response' })
status: Response['statusCode'];

Expand Down
3 changes: 3 additions & 0 deletions src/modules/requests/request.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export class Request {
@Column({ name: 'filters', type: 'jsonb', nullable: true })
filters: jsonbType;

@Column({ name: 'filters_codes', type: 'text', array: true, nullable: true })
filtersCodes: string[];

@Column({ name: 'status', type: 'smallint' })
status: Response['statusCode'];

Expand Down
104 changes: 68 additions & 36 deletions src/utils/clientTestingHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ 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 { getRequestsWithUnavailableSearchParams } from './clientTestingHelpers';
import {
getRequestsWithUnavailableComboSearchParams,
getRequestsWithUnavailableSearchParams,
} from './clientTestingHelpers';
import { v4 as uuidv4 } from 'uuid';

const TestDataSource = new DataSource({
type: 'postgres',
Expand All @@ -17,53 +21,81 @@ const TestDataSource = new DataSource({
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 requestData of [
{ resourceType: 'Patient', fhirAction: 'SEARCH', filters: [{ code: '_id', value: '123' }] },
{ resourceType: 'Patient', fhirAction: 'SEARCH', filters: [{ code: 'family', value: '123' }] },
{
resourceType: 'Patient',
fhirAction: 'SEARCH',
filters: [
{ code: 'family', value: '123' },
{ code: 'gender', value: 'unknown' },
],
},
{
resourceType: 'Patient',
fhirAction: 'SEARCH',
filters: [
{ code: 'family', value: '123' },
{ code: 'name', value: 'unknown' },
],
},
{
resourceType: 'Patient',
fhirAction: 'SEARCH',
filters: [
{ code: 'test', value: '123' },
{ code: 'name', value: 'unknown' },
],
},
{ resourceType: 'Patent', fhirAction: 'CREATE' },
{ resourceType: 'Observation', fhirAction: 'SEARCH' },
]) {
await requestsRepository.save({
session: sessionEntity,
resourceType: requestData.resourceType,
fhirAction: requestData.fhirAction,
requestMethod: 'GET',
requestUri: '/Patient',
remoteAddr: 'undefined',
status: 200,
userAgent: 'Mozilla/5.0',
headers: {},
filters: requestData.filters,
filtersCodes: requestData.filters?.map((filter) => filter.code).sort() ?? [],
});
}
});

afterAll(async () => {
await TestDataSource.destroy();
});

describe('Client testing helpers', () => {
const requestsRepository = TestDataSource.getRepository(Request);
it('Should return correct request entities via getRequestsWithUnavailableSearchParams', async () => {
const sessionRepository = TestDataSource.getRepository(Session);
const requestsRepository = TestDataSource.getRepository(Request);

const sessionEntity = await sessionRepository.save({
target: 'http://test.com',
});
const requests = await getRequestsWithUnavailableSearchParams(requestsRepository, sessionUUID, 'Patient', [
'_id',
]);

for (const requestData of [
{ resourceType: 'Patient', fhirAction: 'SEARCH', filters: [{ code: '_id', value: '123' }] },
{ resourceType: 'Patient', fhirAction: 'SEARCH', filters: [{ code: 'family', value: '123' }] },
{
resourceType: 'Patient',
fhirAction: 'SEARCH',
filters: [
{ code: '_id', value: '123' },
{ code: 'family', value: '123' },
],
},
{ resourceType: 'Patent', fhirAction: 'CREATE' },
{ resourceType: 'Observation', fhirAction: 'SEARCH' },
]) {
await requestsRepository.save({
session: sessionEntity,
resourceType: requestData.resourceType,
fhirAction: requestData.fhirAction,
requestMethod: 'GET',
requestUri: '/Patient',
remoteAddr: 'undefined',
status: 200,
userAgent: 'Mozilla/5.0',
headers: {},
filters: requestData.filters,
});
}
expect(requests).toHaveLength(1);
});

const requests = await getRequestsWithUnavailableSearchParams(requestsRepository, sessionEntity.id, 'Patient', [
'_id',
it('Should return correct request entities via getRequestsWithUnavailableComboSearchParams', async () => {
const requests = await getRequestsWithUnavailableComboSearchParams(requestsRepository, sessionUUID, 'Patient', [
['family', 'name'],
['family', 'gender'],
]);

expect(requests).toHaveLength(1);
Expand Down
20 changes: 20 additions & 0 deletions src/utils/clientTestingHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,23 @@ export async function getRequestsWithUnavailableSearchParams(
.andWhere("request.filters->0->>'code' NOT IN (:...availableSearchParams)", { availableSearchParams })
.getMany();
}

export async function getRequestsWithUnavailableComboSearchParams(
repository: Repository<Request>,
sessionId: string,
resourceType: string,
availableSearchParams: string[][],
): Promise<Request[]> {
const result = 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();

return result;
}
1 change: 1 addition & 0 deletions src/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function createRequestObject(
revInclude: JSON.stringify(searchReqObj.revInclude),
sortRules: JSON.stringify(searchReqObj.sortRules),
filters: searchReqObj.filters,
filtersCodes: searchReqObj.filters?.map((filter) => filter.code).sort() ?? [],
status: res.statusCode,
responseBody: JSON.parse(responseBody),
requestBody: req.body,
Expand Down

0 comments on commit f3d6abf

Please sign in to comment.