Skip to content

Add unit test for fetchVipMetadataInformation method #36

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

Merged
merged 1 commit into from
Mar 21, 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
34 changes: 14 additions & 20 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,16 @@ export default defineComponent({
}
},
// vue js lifecylce functions
async created() {
// get resource, ensure resource url is not empty!
if (this.url != null && this.url != undefined && this.url != '') {
this.dicomUrl = await this.addWadouriPrefix(this.url)
}

// get vip metadata
await this.fetchVipMetadataInformation(await this.addWadouriPrefix(this.url))

// prefetch all other metadata (in separate function for performance reasons)
await this.fetchMetadataInformation(await this.addWadouriPrefix(this.url))
},
async mounted() {
if (this.url) {
this.dicomUrl = await this.addWadouriPrefix(this.url)
// get vip metadata
await this.fetchVipMetadataInformation(this.dicomUrl)

// prefetch all other metadata (in separate function for performance reasons)
await this.fetchMetadataInformation(this.dicomUrl)
}
// check if cornerstone core is initialized
if (!cornerstone.isCornerstoneInitialized()) {
await this.initCornerstoneCore()
Expand Down Expand Up @@ -248,11 +245,9 @@ export default defineComponent({
this.viewport = <Types.IStackViewport>this.renderingEngine.getViewport(viewportId)

// add resource to stack, ensure resource url is not empty!
if (this.url != null && this.url != undefined && this.url != '') {
let dicomResourceUrl = await this.addWadouriPrefix(this.url)

if (this.dicomUrl) {
// define a stack containing a single image
const dicomStack = [dicomResourceUrl]
const dicomStack = [this.dicomUrl]

// set stack on the viewport (currently only one image in the stack, therefore no frame # required)
await this.viewport.setStack(dicomStack)
Expand All @@ -262,7 +257,7 @@ export default defineComponent({
this.setViewportCameraParallelScaleFactor()

// getting image metadata from viewport
this.getImageMetadataFromViewport(dicomResourceUrl)
this.getImageMetadataFromViewport(this.dicomUrl)
}
},
updated() {
Expand All @@ -277,6 +272,7 @@ export default defineComponent({
this.isImageMetadataExtractedFromViewport = false
this.isDicomImageDataFetched = false
},

methods: {
async initCornerstoneCore() {
try {
Expand All @@ -291,10 +287,8 @@ export default defineComponent({
async fetchVipMetadataInformation(imageId) {
if (!this.isDicomImageDataFetched) {
this.dicomImageData = await fetchDicomImageData(imageId)
if (!this.isDicomImageDataFetched) {
if (this.dicomImageData != null) {
this.isDicomImageDataFetched = true
}
if (this.dicomImageData != null) {
this.isDicomImageDataFetched = true
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/helper/extractMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ export const addSopTagChecker = (tag: string): boolean => {
return tag.endsWith('_addSOPuids') ? true : false
}

export const extractDicomMetadata = async (
imageData: object,
tags: string[],
language: string = 'en'
) => {
export const extractDicomMetadata = async (imageData: object, tags: string[], language = 'en') => {
const extractedData: { label: string; value: string }[] = []

// extracting data
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ After(async function (): Promise<void> {

const sendRequest = function ({ method, path }): Promise<any> {
const headers = {
Authorization: `Basic ${Buffer.from(`${config.adminUser}:${config.adminPassword}`).toString('base64')}`
Authorization: `Basic ${Buffer.from(`${config.adminUser}:${config.adminPassword}`).toString(
'base64'
)}`
}
return axios({
method,
Expand Down
73 changes: 64 additions & 9 deletions tests/unit/App.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallowMount } from '@vue/test-utils'
import { shallowMount, flushPromises } from '@vue/test-utils'
import App from '../../src/App.vue'
import { vi } from 'vitest'
import { defaultPlugins } from '../../src/helper/defaultPlugins'
Expand All @@ -14,24 +14,39 @@ vi.mock('vue3-gettext', () => ({

vi.mock('@cornerstonejs/core', () => {
return {
successCallback: vi.fn(),
errorCallback: vi.fn(),
RenderingEngine: class RenderingEngine {
getViewport() {
return {
setStack: vi.fn(),
render: vi.fn()
render: vi.fn(),
getCamera: vi.fn().mockImplementation(() => {
return { parallelScale: 137.3853139193763 }
}),
getImageData: vi.fn().mockImplementation(() => {
return { dimensions: [] }
})
}
}
enableElement() {}
},
Types: vi.fn(),
Enums: {
ViewportType: {
STACK: '' // "stack",
STACK: ''
}
},
metaData: vi.fn(),
metaData: {
get: vi.fn().mockImplementation(() => {
return {
pixelRepresentation: '',
bitsAllocated: '',
bitsStored: '',
highBit: '',
samplesPerPixel: ''
}
})
},

init: vi.fn(),
getConfiguration: vi.fn().mockImplementation(() => {
return { rendering: '' }
Expand Down Expand Up @@ -60,12 +75,52 @@ vi.mock('dicom-parser', () => ({
}))

describe('App component', () => {
const spyFetchVipMetadataInformation = vi
.spyOn(App.methods, 'fetchVipMetadataInformation')
.mockImplementation(vi.fn())
const spyFetchMetadataInformation = vi
.spyOn(App.methods, 'fetchMetadataInformation')
.mockImplementation(vi.fn())
const spyAddWadouriPrefix = vi
.spyOn(App.methods, 'addWadouriPrefix')
.mockReturnValue('wadouri:https://test')
const spyInitCornerstoneCore = vi
.spyOn(App.methods, 'initCornerstoneCore')
.mockImplementation(vi.fn())

beforeEach(() => {
vi.clearAllMocks()
})

it('should not fetch overlay metadata when the url is not provided', async () => {
getWrapper()
await flushPromises()

expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(0)
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(0)
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(0)
expect(spyInitCornerstoneCore).toHaveBeenCalledTimes(1)
})
it('should fetch overlay metadata when the url is provided', async () => {
getWrapper({ props: { url: 'https://test' } })
await flushPromises()

expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(1)
expect(spyAddWadouriPrefix).toHaveBeenCalledWith('https://test')
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(1)
expect(spyFetchVipMetadataInformation).toHaveBeenCalledWith('wadouri:https://test')
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(1)
expect(spyFetchMetadataInformation).toHaveBeenCalledWith('wadouri:https://test')
expect(spyInitCornerstoneCore).toHaveBeenCalledTimes(1)
})

describe('Methods', () => {
vi.spyOn(App.methods, 'fetchVipMetadataInformation').mockImplementation(vi.fn())
vi.spyOn(App.methods, 'fetchMetadataInformation').mockImplementation(vi.fn())
const wrapper = getWrapper()

describe('method: wadouri', () => {
beforeAll(() => {
spyAddWadouriPrefix.mockRestore()
})

it('should add "wadouri" prefix', async () => {
expect(await wrapper.vm.addWadouriPrefix('https://dummy_url')).toBe(
'wadouri:https://dummy_url'
Expand Down