Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Commit

Permalink
test(DICOMWeb): adding tests for DICOMWeb files
Browse files Browse the repository at this point in the history
* test(getAttribute.test.js): adding unit tests for getAttribute

* feat(getAuthorizationHeader.test.js): including getAuthorizationHeader.test.js

* feat(getName.test.js): adding getName tests

* feat(getNumber.test.js): adding getNumber.test.js

* feat(getString.test.js): adding getString.test.js

* feat(index.test.js): adding index.test.js file

* feat(index.test.js): improviments on index test file

* feat(getModalities.test.js): adding getModalities tests

* feat(index.test.js): emoving call tests since the expect test already covers it
  • Loading branch information
biharck authored and dannyrb committed Aug 6, 2019
1 parent 3f9126b commit 60ca6f0
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/DICOMWeb/getAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function getAttribute(element, defaultValue) {

function convertToInt(input) {
function padFour(input) {
var l = input.length;
const l = input.length;

if (l == 0) return '0000';
if (l == 1) return '000' + input;
Expand All @@ -33,9 +33,9 @@ function convertToInt(input) {
return input;
}

var output = '';
for (var i = 0; i < input.length; i++) {
for (var j = 0; j < input[i].length; j++) {
let output = '';
for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input[i].length; j++) {
output += padFour(input[i].charCodeAt(j).toString(16));
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/DICOMWeb/getAttribute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import getAttribute from './getAttribute';

describe('getAttribute', () => {
it('should return a default value if element is null or undefined', () => {
const defaultValue = '0000';
const nullElement = null;
const undefinedElement = undefined;

expect(getAttribute(nullElement, defaultValue)).toEqual(defaultValue);
expect(getAttribute(undefinedElement, defaultValue)).toEqual(defaultValue);
});

it('should return a default value if element.Value is null, undefined or not present', () => {
const defaultValue = '0000';
const nullElement = {
id: 0,
Value: null,
};
const undefinedElement = {
id: 0,
Value: undefined,
};
const noValuePresentElement = {
id: 0,
};

expect(getAttribute(nullElement, defaultValue)).toEqual(defaultValue);
expect(getAttribute(undefinedElement, defaultValue)).toEqual(defaultValue);
expect(getAttribute(noValuePresentElement, defaultValue)).toEqual(
defaultValue
);
});

it('should return 48 for element with value 0', () => {
const returnValue = 48;
const element = {
Value: '0',
};
expect(getAttribute(element, null)).toEqual(returnValue);
});

it('should return 3211313 for element with value 11', () => {
const returnValue = 3211313;
const element = {
Value: '11',
};
expect(getAttribute(element, null)).toEqual(returnValue);
});

it('should return 2.4923405222191973e+35 for element with value 00280009', () => {
const returnValue = 2.4923405222191973e35;
const element = {
id: 0,
Value: '00280009',
};
expect(getAttribute(element, null)).toEqual(returnValue);
});

it('should return 2949169 for element with value -1', () => {
const returnValue = 2949169;
const element = {
id: 0,
Value: '-1',
};
expect(getAttribute(element, null)).toEqual(returnValue);
});
});
82 changes: 82 additions & 0 deletions src/DICOMWeb/getAuthorizationHeader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import getAuthorizationHeader from './getAuthorizationHeader';
import user from './../user';

jest.mock('./../user.js');

describe('getAuthorizationHeader', () => {
it('should return a HTTP Basic Auth when server contains requestOptions.auth', () => {
const validServer = {
requestOptions: {
auth: {
user: 'dummy_user',
password: 'dummy_password',
},
},
};

const expectedAuthorizationHeader = {
Authorization: `Basic ${btoa(validServer.requestOptions.auth)}`,
};

const authentication = getAuthorizationHeader(validServer);

expect(authentication).toEqual(expectedAuthorizationHeader);
});

it('should return a HTTP Basic Auth when server contains requestOptions.auth even though there is no password', () => {
const validServerWithoutPassword = {
requestOptions: {
auth: {
user: 'dummy_user',
},
},
};

const expectedAuthorizationHeader = {
Authorization: `Basic ${btoa(
validServerWithoutPassword.requestOptions.auth
)}`,
};

const authentication = getAuthorizationHeader(validServerWithoutPassword);

expect(authentication).toEqual(expectedAuthorizationHeader);
});

it('should return a HTTP Basic Auth when server contains requestOptions.auth even though there is no username', () => {
const validServerWithoutPassword = {
requestOptions: {
auth: {
user: 'dummy_user',
},
},
};

const expectedAuthorizationHeader = {
Authorization: `Basic ${btoa(
validServerWithoutPassword.requestOptions.auth
)}`,
};

const authentication = getAuthorizationHeader(validServerWithoutPassword);

expect(authentication).toEqual(expectedAuthorizationHeader);
});

it('should return an empty object when there is no either server.requestOptions.auth or accessToken', () => {
const authentication = getAuthorizationHeader({});

expect(authentication).toEqual({});
});

it('should return an Authorization with accessToken when server is not defined and there is an accessToken', () => {
user.getAccessToken.mockImplementationOnce(() => 'MOCKED_TOKEN');

const authentication = getAuthorizationHeader({});
const exptecteHeaderBasedOnUserAccessToekn = {
Authorization: 'Bearer MOCKED_TOKEN',
};

expect(authentication).toEqual(exptecteHeaderBasedOnUserAccessToekn);
});
});
6 changes: 3 additions & 3 deletions src/DICOMWeb/getModalities.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export default function getModalities(modality, modalitiesInStudy) {
var modalities = {};
let modalities = {};
if (modality) {
modalities = modality;
}

if (modalitiesInStudy) {
// Find vr in modalities
if (modalities.vr && modalities.vr === modalitiesInStudy.vr) {
for (var i = 0; i < modalitiesInStudy.Value.length; i++) {
var value = modalitiesInStudy.Value[i];
for (let i = 0; i < modalitiesInStudy.Value.length; i++) {
const value = modalitiesInStudy.Value[i];
if (modalities.Value.indexOf(value) === -1) {
modalities.Value.push(value);
}
Expand Down
74 changes: 74 additions & 0 deletions src/DICOMWeb/getModalities.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import getModalities from './getModalities';

describe('getModalities', () => {
test('should return an empty object when modality and modalitiesInStudy are not present', () => {
const modality = null;
const modalitiesInStudy = null;

expect(getModalities(modality, modalitiesInStudy)).toEqual({});
});

test('should return an empty object when modality and modalitiesInStudy are not present', () => {
const modality = null;
const modalitiesInStudy = null;

expect(getModalities(modality, modalitiesInStudy)).toEqual({});
});

test('should return modalities in Study when modality is not defined', () => {
const modality = null;

const modalitiesInStudy = {
Value: ['MOCKED_VALUE'],
vr: 'MOCKED_VALUE',
};

expect(getModalities(modality, modalitiesInStudy)).toEqual(
modalitiesInStudy
);
});

test('should return only the modalitues that exists in modalitiesInStudy', () => {
const modality = {
Value: ['DESIRED_VALUE'],
vr: 'DESIRED_VR',
};

const modalitiesInStudy = {
Value: ['DESIRED_VALUE', 'NOT_DESIRED_VALUE'],
vr: 'DESIRED_VR',
};

expect(getModalities(modality, modalitiesInStudy)).toEqual(modality);
});

test('should return the seek modality when the desired modality does not exist in modalitiesInStudy', () => {
const modality = {
Value: ['DESIRED_VALUE'],
vr: 'DESIRED_VR',
};

const modalitiesInStudy = {
Value: ['NOT_DESIRED_VALUE'],
vr: 'DESIRED_VR',
};

expect(getModalities(modality, modalitiesInStudy)).toEqual(modality);
});

test('should return the seek modality when the desired modality does not exist in modalitiesInStudy VR', () => {
const modality = {
Value: ['DESIRED_VALUE'],
vr: 'DESIRED_VR',
};

const modalitiesInStudy = {
Value: ['NOT_DESIRED_VALUE'],
vr: 'ANOTHER_VR',
};

expect(getModalities(modality, modalitiesInStudy)).toEqual(
modalitiesInStudy
);
});
});
62 changes: 62 additions & 0 deletions src/DICOMWeb/getName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import getName from './getName';

describe('getName', () => {
it('should return a default value if element is null or undefined', () => {
const defaultValue = 'DEFAULT_NAME';
const nullElement = null;
const undefinedElement = undefined;

expect(getName(nullElement, defaultValue)).toEqual(defaultValue);
expect(getName(undefinedElement, defaultValue)).toEqual(defaultValue);
});

it('should return a default value if element.Value is null, undefined or not present', () => {
const defaultValue = 'DEFAULT_NAME';
const nullElement = {
id: 0,
Value: null,
};
const undefinedElement = {
id: 0,
Value: undefined,
};
const noValuePresentElement = {
id: 0,
};

expect(getName(nullElement, defaultValue)).toEqual(defaultValue);
expect(getName(undefinedElement, defaultValue)).toEqual(defaultValue);
expect(getName(noValuePresentElement, defaultValue)).toEqual(defaultValue);
});

it('should return A for element when Alphabetic is [A, B, C, D]', () => {
const returnValue = 'A';
const element = {
Value: [
{ Alphabetic: 'A' },
{ Alphabetic: 'B' },
{ Alphabetic: 'C' },
{ Alphabetic: 'D' },
],
};
expect(getName(element, null)).toEqual(returnValue);
});

it('should return FIRST for element when Alphabetic is [FIRST, SECOND]', () => {
const returnValue = 'FIRST';
const element = {
Value: [{ Alphabetic: 'FIRST' }, { Alphabetic: 'SECOND' }],
};
expect(getName(element, null)).toEqual(returnValue);
});

it('should return element.value[0] for element with not Alphabetic and when there is at least on element.Value', () => {
const returnValue = {
anyOtherProperty: 'FIRST',
};
const element = {
Value: [{ anyOtherProperty: 'FIRST' }, { Alphabetic: 'SECOND' }],
};
expect(getName(element, null)).toEqual(returnValue);
});
});
57 changes: 57 additions & 0 deletions src/DICOMWeb/getNumber.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import getNumber from './getNumber';

describe('getNumber', () => {
it('should return a default value if element is null or undefined', () => {
const defaultValue = 1.0;
const nullElement = null;
const undefinedElement = undefined;

expect(getNumber(nullElement, defaultValue)).toEqual(defaultValue);
expect(getNumber(undefinedElement, defaultValue)).toEqual(defaultValue);
});

it('should return a default value if element.Value is null, undefined or not present', () => {
const defaultValue = 1.0;
const nullElement = {
id: 0,
Value: null,
};
const undefinedElement = {
id: 0,
Value: undefined,
};
const noValuePresentElement = {
id: 0,
};

expect(getNumber(nullElement, defaultValue)).toEqual(defaultValue);
expect(getNumber(undefinedElement, defaultValue)).toEqual(defaultValue);
expect(getNumber(noValuePresentElement, defaultValue)).toEqual(
defaultValue
);
});

it('should return 2.0 for element when element.Value[0] = 2', () => {
const returnValue = 2.0;
const element = {
Value: ['2'],
};
expect(getNumber(element, null)).toEqual(returnValue);
});

it('should return -1.0 for element when element.Value[0] is -1', () => {
const returnValue = -1.0;
const element = {
Value: ['-1'],
};
expect(getNumber(element, null)).toEqual(returnValue);
});

it('should return -1.0 for element when element.Value is [-1, 2, 5, -10] ', () => {
const returnValue = -1.0;
const element = {
Value: ['-1', '2', '5', '-10'],
};
expect(getNumber(element, null)).toEqual(returnValue);
});
});
Loading

0 comments on commit 60ca6f0

Please sign in to comment.