Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #38

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* © Copyright Outburn Ltd. 2022-2023 All Rights Reserved
* Project name: FUME
*/

import { test } from '@jest/globals';

import config from './config';

const serverDefaults = {
EXCLUDE_FHIR_PACKAGES: '',
FHIR_PACKAGES: '[email protected],[email protected],[email protected],laniado.test.fhir.r4',
FHIR_SERVER_AUTH_TYPE: 'NONE',
FHIR_SERVER_BASE: 'http://hapi-fhir.outburn.co.il/fhir',
FHIR_SERVER_PW: '',
FHIR_SERVER_TIMEOUT: 10000,
FHIR_SERVER_UN: '',
FHIR_VERSION: '4.0.1',
SEARCH_BUNDLE_PAGE_SIZE: 20,
SERVER_PORT: 42420,
SERVER_STATELESS: false
};

jest.mock('./serverConfig', () => (serverDefaults));

describe('setServerConfig', () => {
test('Uses defaults', async () => {
config.setServerConfig({});
expect(config.getServerConfig()).toEqual({
...serverDefaults,
FHIR_SERVER_BASE: '',
SERVER_STATELESS: true
});
});

test('Uses defaults, with FHIR_SERVER_BASE, sets stateless to false', async () => {
config.setServerConfig({
FHIR_SERVER_BASE: 'http://hapi-fhir.outburn.co.il/fhir-test '
});
expect(config.getServerConfig()).toEqual({
EXCLUDE_FHIR_PACKAGES: '',
FHIR_PACKAGES: '[email protected],[email protected],[email protected],laniado.test.fhir.r4',
FHIR_SERVER_AUTH_TYPE: 'NONE',
FHIR_SERVER_BASE: 'http://hapi-fhir.outburn.co.il/fhir-test',
FHIR_SERVER_PW: '',
FHIR_SERVER_TIMEOUT: 10000,
FHIR_SERVER_UN: '',
FHIR_VERSION: '4.0.1',
SEARCH_BUNDLE_PAGE_SIZE: 20,
SERVER_PORT: 42420,
SERVER_STATELESS: false
});
});

test('Uses defaults, without FHIR_SERVER_BASE, sets stateless to true', async () => {
config.setServerConfig({
SERVER_STATELESS: false
});
expect(config.getServerConfig()).toEqual({
EXCLUDE_FHIR_PACKAGES: '',
FHIR_PACKAGES: '[email protected],[email protected],[email protected],laniado.test.fhir.r4',
FHIR_SERVER_AUTH_TYPE: 'NONE',
FHIR_SERVER_BASE: '',
FHIR_SERVER_PW: '',
FHIR_SERVER_TIMEOUT: 10000,
FHIR_SERVER_UN: '',
FHIR_VERSION: '4.0.1',
SEARCH_BUNDLE_PAGE_SIZE: 20,
SERVER_PORT: 42420,
SERVER_STATELESS: true
});
});
});
10 changes: 6 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import type { IAppBinding, IConfig } from './types';
const additionalBindings: Record<string, IAppBinding> = {}; // additional functions to bind when running transformations
let serverConfig: IConfig = { ...defaultConfig };

const setServerConfig = <ConfigType extends IConfig>(config: ConfigType) => {
let fhirServerBase: string = config.FHIR_SERVER_BASE;
let isStatelessMode: boolean = config.SERVER_STATELESS;
const setServerConfig = <ConfigType extends IConfig>(config: Partial<ConfigType>) => {
let fhirServerBase: string | undefined = config.FHIR_SERVER_BASE ? config.FHIR_SERVER_BASE.trim() : undefined;
let isStatelessMode: boolean | undefined = config.SERVER_STATELESS;

if (!fhirServerBase || fhirServerBase.trim() === '' || isStatelessMode) {
if (!fhirServerBase || fhirServerBase === '' || isStatelessMode) {
fhirServerBase = '';
isStatelessMode = true;
} else {
isStatelessMode = false;
};
serverConfig = {
...defaultConfig,
Expand Down
15 changes: 15 additions & 0 deletions src/helpers/cache/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { test } from '@jest/globals';

import { getCache, initCache } from './cache';
import { SimpleCache } from './simpleCache';

class TestCache extends SimpleCache<any> {}

describe('SimpleCache', () => {
test('getCache fails if not init', async () => {
Expand All @@ -15,4 +18,16 @@ describe('SimpleCache', () => {
getCache();
}).not.toThrow();
});

test('initCache with override class', async () => {
initCache({
aliases: {
cacheClass: TestCache,
cacheClassOptions: {}
}
});
const { aliases, mappings } = getCache();
expect(aliases).toBeInstanceOf(TestCache);
expect(mappings).toBeInstanceOf(SimpleCache);
});
});
167 changes: 167 additions & 0 deletions src/helpers/fhirServer/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@

import { test } from '@jest/globals';
import mockAxios from 'jest-mock-axios';

import config from '../../config';
import { FhirClient } from './client';

const mockConfig = {
FHIR_SERVER_BASE: 'test.com',
FHIR_SERVER_TIMEOUT: 1000,
SERVER_PORT: 111,
FHIR_SERVER_AUTH_TYPE: '',
FHIR_SERVER_UN: '',
FHIR_SERVER_PW: '',
SERVER_STATELESS: false,
FHIR_VERSION: '4.0.1',
SEARCH_BUNDLE_PAGE_SIZE: 200,
FHIR_PACKAGES: '',
EXCLUDE_FHIR_PACKAGES: ''
};

describe('FhirClient', () => {
let mockAxiosInstance: jest.Mocked<any>;

afterEach(() => {
jest.resetAllMocks();
mockAxios.reset();
});

beforeEach(() => {
mockAxiosInstance = {
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
delete: jest.fn()

} as any;
mockAxios.create.mockReturnValue(mockAxiosInstance);
});

test('constructor uses config, no auth, stateless = false', async () => {
config.setServerConfig(mockConfig);
/* eslint-disable no-new */
new FhirClient();

expect(mockAxios.create).toHaveBeenCalledWith({
baseURL: mockConfig.FHIR_SERVER_BASE,
timeout: mockConfig.FHIR_SERVER_TIMEOUT,
headers: {
Accept: 'application/fhir+json;fhirVersion=4.0',
'Content-Type': 'application/fhir+json;fhirVersion=4.0',
Authorization: undefined
}
});
});

test('constructor uses config, with BASIC auth, stateless = false', async () => {
config.setServerConfig({
...mockConfig,
FHIR_SERVER_AUTH_TYPE: 'BASIC',
FHIR_SERVER_UN: 'user',
FHIR_SERVER_PW: 'pw'
});
/* eslint-disable no-new */
new FhirClient();

expect(mockAxios.create).toHaveBeenCalledWith({
baseURL: mockConfig.FHIR_SERVER_BASE,
timeout: mockConfig.FHIR_SERVER_TIMEOUT,
headers: {
Accept: 'application/fhir+json;fhirVersion=4.0',
'Content-Type': 'application/fhir+json;fhirVersion=4.0',
Authorization: 'Basic dXNlcjpwdw=='
}
});
});

test('constructor uses config, no auth, stateless = false', async () => {
config.setServerConfig({
...mockConfig,
SERVER_STATELESS: true
});

/* eslint-disable no-new */
new FhirClient();

expect(mockAxios.create).not.toHaveBeenCalled();
});

test('read returns empty in stateless mode', async () => {
config.setServerConfig({
...mockConfig,
SERVER_STATELESS: true
});
const client = new FhirClient();
const url = 'test';
const res = await client.read(url);
expect(res).toBeUndefined();
expect(mockAxios.get).not.toHaveBeenCalled();
});

test('read returns response data', async () => {
config.setServerConfig(mockConfig);
const client = new FhirClient();
const url = 'test-url';
mockAxiosInstance.get.mockResolvedValue({ data: 'test' });
const response = await client.read(url);
expect(response).toBe('test');
expect(mockAxiosInstance.get).toHaveBeenCalledWith(url);
});

test('search returns empty in stateless mode', async () => {
config.setServerConfig({
...mockConfig,
SERVER_STATELESS: true
});
const client = new FhirClient();
const url = 'test';
const res = await client.search(url);
expect(res).toBeUndefined();
expect(mockAxios.get).not.toHaveBeenCalled();
});

test('search returns response data', async () => {
config.setServerConfig({
...mockConfig,
SEARCH_BUNDLE_PAGE_SIZE: 45
});
const client = new FhirClient();
const url = 'test-url';
mockAxiosInstance.get.mockResolvedValue({ data: 'test' });
const response = await client.search(url, { test: 'test-param' });
expect(response).toBe('test');

expect(mockAxiosInstance.get).toHaveBeenCalledWith('/test-url', { params: { _count: 45, test: 'test-param' } });
});

test('create throws error in community', async () => {
config.setServerConfig({
...mockConfig,
SERVER_STATELESS: true
});
const client = new FhirClient();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
expect(client.create({}, 'test')).rejects.toThrow('Method not implemented.');
});

test('update throws error in community', async () => {
config.setServerConfig({
...mockConfig,
SERVER_STATELESS: true
});
const client = new FhirClient();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
expect(client.update('test', 'test', {})).rejects.toThrow('Method not implemented.');
});

test('simpleDelete throws error in community', async () => {
config.setServerConfig({
...mockConfig,
SERVER_STATELESS: true
});
const client = new FhirClient();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
expect(client.simpleDelete('test', 'test')).rejects.toThrow('Method not implemented.');
});
});