Skip to content

Commit

Permalink
Most tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lmd59 committed Jul 30, 2024
1 parent ba82f48 commit c521114
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions service/src/db/dbOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export async function findResourceById<T extends fhir4.FhirResource>(id: string,
/**
* searches the database and returns an array of objects that contain metadata and each respective resource of the given type that match the given query
*/
export async function findResourcesWithQuery<T extends fhir4.FhirResource>(
export async function findResourcesWithQuery<T extends fhir4.FhirResource[]>(
query: Filter<any>,
resourceType: FhirResourceType
) {
const collection = Connection.db.collection(resourceType);
const projection = { _id: 0, _dataRequirements: 0 };
const projection = { 'data._id': 0, 'data._dataRequirements': 0 };
const pagination: any = query.skip ? [{ $skip: query.skip }, { $limit: query.limit }] : [];
query._dataRequirements = { $exists: false };
query._summary = { $exists: false };
Expand Down
4 changes: 2 additions & 2 deletions service/src/services/LibraryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ export class LibraryService implements Service<fhir4.Library> {
}
return bundle;
} else {
const result = await findResourcesWithQuery<fhir4.Library>(mongoQuery, 'Library');
const entries = result.map(r => r.data);
const result = await findResourcesWithQuery<fhir4.Library[]>(mongoQuery, 'Library');
const entries = result[0].data;
const bundle = createSearchsetBundle(entries);
if (parsedQuery._count) {
if (parsedQuery._count) {
Expand Down
4 changes: 2 additions & 2 deletions service/src/services/MeasureService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export class MeasureService implements Service<fhir4.Measure> {
}
return bundle;
} else {
const result = await findResourcesWithQuery<fhir4.Measure>(mongoQuery, 'Measure');
const entries = result.map(r => r.data);
const result = await findResourcesWithQuery<fhir4.Measure[]>(mongoQuery, 'Measure');
const entries = result[0].data;
const bundle = createSearchsetBundle(entries);
if (parsedQuery._count) {
if (parsedQuery._count) {
Expand Down
8 changes: 4 additions & 4 deletions service/src/util/bundleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function createMeasurePackageBundle(
params: z.infer<typeof PackageArgs>
): Promise<fhir4.Bundle<fhir4.FhirResource>> {
const mongoQuery = getMongoQueryFromRequest(query);
const measure = (await findResourcesWithQuery<fhir4.Measure>(mongoQuery, 'Measure')).map(r => r.data);
const measure = (await findResourcesWithQuery<fhir4.Measure[]>(mongoQuery, 'Measure'))[0].data;
if (!measure || !(measure.length > 0)) {
throw new ResourceNotFoundError(
`No resource found in collection: Measure, with ${Object.keys(query)
Expand All @@ -137,7 +137,7 @@ export async function createMeasurePackageBundle(
if (measureForPackaging.library && measureForPackaging.library.length > 0) {
const [mainLibraryRef] = measureForPackaging.library;
const mainLibQuery = getQueryFromReference(mainLibraryRef);
const libs = (await findResourcesWithQuery<fhir4.Library>(mainLibQuery, 'Library')).map(r => r.data);
const libs = (await findResourcesWithQuery<fhir4.Library[]>(mainLibQuery, 'Library'))[0].data;
if (!libs || libs.length < 1) {
throw new ResourceNotFoundError(
`Could not find Library ${mainLibraryRef} referenced by Measure ${measureForPackaging.id}`
Expand All @@ -163,7 +163,7 @@ export async function createLibraryPackageBundle(
params: z.infer<typeof PackageArgs>
): Promise<{ libraryBundle: fhir4.Bundle<fhir4.FhirResource>; rootLibRef?: string }> {
const mongoQuery = getMongoQueryFromRequest(query);
const library = (await findResourcesWithQuery<fhir4.Library>(mongoQuery, 'Library')).map(r => r.data);
const library = (await findResourcesWithQuery<fhir4.Library[]>(mongoQuery, 'Library'))[0].data;
if (!library || !(library.length > 0)) {
throw new ResourceNotFoundError(
`No resource found in collection: Library, with ${Object.keys(query)
Expand Down Expand Up @@ -347,7 +347,7 @@ export async function getAllDependentLibraries(lib: fhir4.Library): Promise<fhir
// Obtain all libraries referenced in the related artifact, and recurse on their dependencies
const libraryGets = depLibUrls.map(async url => {
const libQuery = getQueryFromReference(url);
const libs = (await findResourcesWithQuery(libQuery, 'Library')).map(r => r.data);
const libs = (await findResourcesWithQuery(libQuery, 'Library'))[0].data;
if (!libs || libs.length < 1) {
throw new ResourceNotFoundError(
`Failed to find dependent library with ${
Expand Down
6 changes: 3 additions & 3 deletions service/test/services/LibraryService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ describe('LibraryService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('invalid');
expect(response.body.issue[0].details.text).toEqual(
'Multiple resources found in collection: Library, with identifier: http://example.com/libraryWithSameSystem|. /Library/$cqfm.package operation must specify a single Library'
'Multiple resources found in collection: Library, with identifier: http://example.com/libraryWithSameSystem| and page: 1. /Library/$cqfm.package operation must specify a single Library'
);
});
});
Expand Down Expand Up @@ -496,7 +496,7 @@ describe('LibraryService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('not-found');
expect(response.body.issue[0].details.text).toEqual(
'No resource found in collection: Library, with id: testLibraryWithDeps and url: http://example.com/invalid'
'No resource found in collection: Library, with id: testLibraryWithDeps and url: http://example.com/invalid and page: 1'
);
});
});
Expand All @@ -516,7 +516,7 @@ describe('LibraryService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('not-found');
expect(response.body.issue[0].details.text).toEqual(
'No resource found in collection: Library, with id: invalid and url: http://example.com/invalid'
'No resource found in collection: Library, with id: invalid and url: http://example.com/invalid and page: 1'
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions service/test/services/MeasureService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ describe('MeasureService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('not-found');
expect(response.body.issue[0].details.text).toEqual(
'No resource found in collection: Measure, with id: testWithUrl and url: http://example.com/invalid'
'No resource found in collection: Measure, with id: testWithUrl and url: http://example.com/invalid and page: 1'
);
});
});
Expand Down Expand Up @@ -499,7 +499,7 @@ describe('MeasureService', () => {
.then(response => {
expect(response.body.issue[0].code).toEqual('not-found');
expect(response.body.issue[0].details.text).toEqual(
'No resource found in collection: Measure, with id: invalid and url: http://example.com/invalid'
'No resource found in collection: Measure, with id: invalid and url: http://example.com/invalid and page: 1'
);
});
});
Expand Down
10 changes: 6 additions & 4 deletions service/test/util/queryUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ const QUERY_WITH_SYSTEM_AND_CODE_IDEN = { url: 'http://example.com', identifier:
const QUERY_WITH_STRING_PARAM = { url: 'http://example.com', version: 'test' };
const QUERY_WITH_MULTIPLE_STRING_PARAM = { url: 'http://example.com', version: ['test', '...anotherTest?'] };

const EXPECTED_QUERY_WITH_CODE_IDEN = { url: 'http://example.com', 'identifier.value': 'testCode' };
const EXPECTED_QUERY_WITH_SYSTEM_IDEN = { url: 'http://example.com', 'identifier.system': 'testSystem' };
const EXPECTED_QUERY_WITH_NO_IDEN = { skip: 0, url: 'http://example.com' };
const EXPECTED_QUERY_WITH_CODE_IDEN = { skip: 0, url: 'http://example.com', 'identifier.value': 'testCode' };
const EXPECTED_QUERY_WITH_SYSTEM_IDEN = { skip: 0, url: 'http://example.com', 'identifier.system': 'testSystem' };
const EXPECTED_QUERY_WITH_SYSTEM_AND_CODE_IDEN = {
skip: 0,
url: 'http://example.com',
'identifier.system': 'testSystem',
'identifier.value': 'testCode'
};
const EXPECTED_QUERY_WITH_STRING_PARAM = { url: 'http://example.com', version: { $regex: '^test', $options: 'i' } };
const EXPECTED_QUERY_WITH_STRING_PARAM = { skip: 0, url: 'http://example.com', version: { $regex: '^test', $options: 'i' } };

describe('getMongoQueryFromRequest', () => {
it('correctly parses a query with no identifier field', () => {
expect(getMongoQueryFromRequest(QUERY_WITH_NO_IDEN)).toEqual(QUERY_WITH_NO_IDEN);
expect(getMongoQueryFromRequest(QUERY_WITH_NO_IDEN)).toEqual(EXPECTED_QUERY_WITH_NO_IDEN);
});
it('correctly parses a query with a simple code identifier field', () => {
expect(getMongoQueryFromRequest(QUERY_WITH_SIMPLE_CODE_IDEN)).toEqual(EXPECTED_QUERY_WITH_CODE_IDEN);
Expand Down

0 comments on commit c521114

Please sign in to comment.