Skip to content

Commit

Permalink
Formatting/async changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmpotter44 committed Feb 5, 2024
1 parent 788f6e3 commit 93f269d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 52 deletions.
73 changes: 33 additions & 40 deletions spec/clinicaltrialsgov.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,43 +607,38 @@ describe('ClinicalTrialsGovService', () => {
});
});

// These tests basically are only to ensure that all trials are properly visited when given.
it('updates all the given studies', () => {
// Our test studies contain the same NCT ID twice to make sure that works as expected, as well as a NCT ID that
// download spy will return null for to indicate a failure.
const testSearchSetEntries: SearchBundleEntry[] = [
createSearchSetEntry('dupe1', 'NCT00000001'),
createSearchSetEntry('missing', 'NCT00000002'),
createSearchSetEntry('dupe2', 'NCT00000001'),
createSearchSetEntry('singleton', 'NCT00000003', 0.5),

]

const testStudy = createClinicalStudy();
const updateSpy = spyOn(service, 'updateResearchStudy');
const getTrialSpy = jasmine.createSpy('getCachedClinicalStudy').and.callFake((nctId: string) => {
return Promise.resolve(nctId === 'NCT00000002' ? null : testStudy);
});
// These tests basically are only to ensure that all trials are properly visited when given.
it('updates all the given studies', async () => {
// Our test studies contain the same NCT ID twice to make sure that works as expected, as well as a NCT ID that
// download spy will return null for to indicate a failure.
const testSearchSetEntries: SearchBundleEntry[] = [
createSearchSetEntry('dupe1', 'NCT00000001'),
createSearchSetEntry('missing', 'NCT00000002'),
createSearchSetEntry('dupe2', 'NCT00000001'),
createSearchSetEntry('singleton', 'NCT00000003', 0.5)
];

service.getCachedClinicalStudy = getTrialSpy;
return expectAsync(
service.updateSearchSetEntries(testSearchSetEntries).then(() => {
expect(downloadTrialsSpy).toHaveBeenCalledOnceWith(['NCT00000001', 'NCT00000002', 'NCT00000003']);
// Update should have been called three times: twice for the NCT00000001 studies, and once for the NCT00000003 study
expect(updateSpy).toHaveBeenCalledWith(testSearchSetEntries[0].resource as ResearchStudy, testStudy);
expect(updateSpy).not.toHaveBeenCalledWith(testSearchSetEntries[1].resource as ResearchStudy, testStudy);
expect(updateSpy).toHaveBeenCalledWith(testSearchSetEntries[2].resource as ResearchStudy, testStudy);
expect(updateSpy).toHaveBeenCalledWith(testSearchSetEntries[3].resource as ResearchStudy, testStudy);
})
).toBeResolved();
});
const testStudy = createClinicalStudy();
const updateSpy = spyOn(service, 'updateResearchStudy');
const getTrialSpy = jasmine.createSpy('getCachedClinicalStudy').and.callFake((nctId: string) => {
return Promise.resolve(nctId === 'NCT00000002' ? null : testStudy);
});

it('does nothing if no studies have NCT IDs', () => {
return expectAsync(
service.updateSearchSetEntries([ {resource: { resourceType: 'ResearchStudy', status: 'active' }, search: { mode: 'match', score: 0 }}]).then(() => {
expect(downloadTrialsSpy).not.toHaveBeenCalled();
})
).toBeResolved();
service.getCachedClinicalStudy = getTrialSpy;
await service.updateSearchSetEntries(testSearchSetEntries);
expect(downloadTrialsSpy).toHaveBeenCalledOnceWith(['NCT00000001', 'NCT00000002', 'NCT00000003']);
// Update should have been called three times: twice for the NCT00000001 studies, and once for the NCT00000003 study
expect(updateSpy).toHaveBeenCalledWith(testSearchSetEntries[0].resource as ResearchStudy, testStudy);
expect(updateSpy).not.toHaveBeenCalledWith(testSearchSetEntries[1].resource as ResearchStudy, testStudy);
expect(updateSpy).toHaveBeenCalledWith(testSearchSetEntries[2].resource as ResearchStudy, testStudy);
expect(updateSpy).toHaveBeenCalledWith(testSearchSetEntries[3].resource as ResearchStudy, testStudy);
});

it('does nothing if no studies have NCT IDs', async () => {
await service.updateSearchSetEntries([
{ resource: { resourceType: 'ResearchStudy', status: 'active' }, search: { mode: 'match', score: 0 } }
]);
expect(downloadTrialsSpy).not.toHaveBeenCalled();
});

it('handles splitting requests', () => {
Expand All @@ -660,15 +655,14 @@ describe('ClinicalTrialsGovService', () => {
createSearchSetEntry('test1', 'NCT00000001'),
createSearchSetEntry('test2', 'NCT00000002'),
createSearchSetEntry('test3', 'NCT00000003'),
createSearchSetEntry('test4', 'NCT00000004', 0.5),

]
createSearchSetEntry('test4', 'NCT00000004', 0.5)
];
const testStudy = createClinicalStudy();
spyOn(service, 'updateResearchStudy');
const getTrialSpy = jasmine.createSpy('getCachedClinicalStudy').and.callFake(() => {
return Promise.resolve(testStudy);
});

service.getCachedClinicalStudy = getTrialSpy;
return expectAsync(
service.updateResearchStudies(testStudies).then(() => {
Expand All @@ -678,7 +672,6 @@ describe('ClinicalTrialsGovService', () => {
})
).toBeResolved();
});

});

// this functionality is currently unimplemented - this test exists solely to "cover" the method
Expand Down
10 changes: 5 additions & 5 deletions spec/support/researchstudy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ResearchStudy } from '../../src/research-study';
import { SearchBundleEntry } from '../../src/searchset';

export function createResearchStudyObject(nctId?: string): ResearchStudy {
const result = new ResearchStudy(nctId ?? "test");
const result = new ResearchStudy(nctId ?? 'test');
if (nctId) {
result.identifier = [
{
Expand Down Expand Up @@ -35,12 +35,12 @@ export function createResearchStudy(id: string, nctId?: string): IResearchStudy
}

export function createSearchSetEntry(id: string, nctId?: string, score?: number): SearchBundleEntry {
const result:SearchBundleEntry = {
resource: createResearchStudy(id,nctId),
const result: SearchBundleEntry = {
resource: createResearchStudy(id, nctId),
search: {
mode: "match",
mode: 'match',
score: score || 0
}
}
};
return result;
}
14 changes: 7 additions & 7 deletions src/clinicaltrialsgov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { debuglog } from 'util';
import { ResearchStudy } from 'fhir/r4';
import {SearchBundleEntry as SearchSetEntry} from './searchset';
import { SearchBundleEntry as SearchSetEntry } from './searchset';
import { ClinicalTrialsGovAPI, Study } from './clinicaltrialsgov-api';
import { updateResearchStudyWithClinicalStudy } from './study-fhir-converter';
import * as sqlite from 'sqlite';
Expand Down Expand Up @@ -491,17 +491,17 @@ export class ClinicalTrialsGovService {
}
}

async updateSearchSetEntries(entries: SearchSetEntry[]):Promise<SearchSetEntry[]> {
const studies:ResearchStudy[] = entries.map(item => item.resource as ResearchStudy);
async updateSearchSetEntries(entries: SearchSetEntry[]): Promise<SearchSetEntry[]> {
const studies: ResearchStudy[] = entries.map((item) => item.resource as ResearchStudy);

await this.ensureTrialsAvailable(studies);

await Promise.all(
entries.map((entry) => {
const nctId = findNCTNumber(entry.resource as ResearchStudy) || '';
return this.updateResearchStudyFromCache(nctId, entry.resource as ResearchStudy);

}));
const nctId = findNCTNumber(entry.resource as ResearchStudy) || '';
return this.updateResearchStudyFromCache(nctId, entry.resource as ResearchStudy);
})
);

return entries;
}
Expand Down

0 comments on commit 93f269d

Please sign in to comment.