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

Unit tests #67

Merged
merged 22 commits into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d98ca3a
test(getAttribute.test.js): adding unit tests for getAttribute
biharck Aug 3, 2019
4ff10fc
feat(getAuthorizationHeader.test.js): including getAuthorizationHeade…
biharck Aug 4, 2019
78b2950
feat(getName.test.js): adding getName tests
biharck Aug 4, 2019
8bb8d01
feat(getNumber.test.js): adding getNumber.test.js
biharck Aug 4, 2019
2bb3be5
feat(getString.test.js): adding getString.test.js
biharck Aug 4, 2019
4bdfc5a
feat(index.test.js): adding index.test.js file
biharck Aug 4, 2019
9ec7f11
feat(index.test.js): improviments on index test file
biharck Aug 4, 2019
94e9323
feat(getModalities.test.js): adding getModalities tests
biharck Aug 4, 2019
7e35154
feat(index.test.js): emoving call tests since the expect test already…
biharck Aug 6, 2019
58957ea
adding and refactoring absoluteUrl function
biharck Aug 7, 2019
e5b1056
adding addServer.test.js
biharck Aug 8, 2019
c497001
adding unit tests for guid
biharck Aug 8, 2019
438e592
adding unit tests for index.js
biharck Aug 8, 2019
e3248a8
fixing typo in guid.test.js and adding isImage.test.js file
biharck Aug 8, 2019
e43ae2f
adding tests for objectPath.js
biharck Aug 8, 2019
bfd062f
replacing toBeCalled to toBeCalledWith
biharck Aug 9, 2019
8918c50
Merge branch 'master' into unit-tests
biharck Aug 9, 2019
b157816
fixing typo
biharck Aug 9, 2019
91658db
removing beforeEach
biharck Aug 9, 2019
e76725c
making the test description and variables clear
biharck Aug 9, 2019
8ec4b54
Clearing all mocks after tests run
biharck Aug 9, 2019
c959b12
improving addServer tests
biharck Aug 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});