Skip to content

Commit

Permalink
Adding additional tests, error checking within client
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelynJefferson committed Jul 22, 2024
1 parent a82f35c commit 3fb2357
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/cache/DiskBasedPackageCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,21 @@ export class DiskBasedPackageCache implements PackageCache {
let resource = this.lruCache.get(resourcePath);
if (!resource) {
if (/.xml$/i.test(resourcePath)) {
// TODO: Consider error handling
const xml = fs.readFileSync(resourcePath).toString();
resource = this.fhirConverter.xmlToObj(xml);
} else {
resource = fs.readJSONSync(resourcePath);
try {
const xml = fs.readFileSync(resourcePath).toString();
resource = this.fhirConverter.xmlToObj(xml);
} catch {
throw new Error(`Failed to get XML resource at path ${resourcePath}`);
}
} else if (/.json$/i.test(resourcePath)) {
try {
resource = fs.readJSONSync(resourcePath);
} catch {
throw new Error(`Failed to get JSON resource at path ${resourcePath}`);
}
}
else {
throw new Error(`Failed to find XML or JSON file at path ${resourcePath}`);
}
this.lruCache.set(resourcePath, resource);
}
Expand Down
29 changes: 29 additions & 0 deletions test/cache/DiskBasedPackageCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ describe('DiskBasedPackageCache', () => {
expect(resource.id).toBe('MyPatient');
expect(loggerSpy.getAllLogs('error')).toHaveLength(0);
});

it('should return a resource with an xml path where xml was converted to a resource', () => {
const totalPath = path.resolve(local1Folder, 'StructureDefinition-true-false.xml');
const resource = cache.getResourceAtPath(totalPath);
Expand All @@ -247,5 +248,33 @@ describe('DiskBasedPackageCache', () => {
expect(resource.xml).toBeUndefined();
expect(loggerSpy.getAllLogs('error')).toHaveLength(0);
});

it('should throw error when path points to a xml file that does not exist', () => {
const totalPath = path.resolve(local1Folder, 'example-file-that-doesnt-exist.xml');
expect(() => {
cache.getResourceAtPath(totalPath);
}).toThrow(/Failed to get XML resource at path/);
});

it('should throw error when path points to a json file that does not exist', () => {
const totalPath = path.resolve(local1Folder, 'example-file-that-doesnt-exist.json');
expect(() => {
cache.getResourceAtPath(totalPath);
}).toThrow(/Failed to get JSON resource at path/);
});

it('should throw error when path points to an invalid file type that is not json or xml', () => {
const totalPath = path.resolve(local1Folder, 'example-file-that-doesnt-exist.txt');
expect(() => {
cache.getResourceAtPath(totalPath);
}).toThrow(/Failed to find XML or JSON file/);
});

it('should throw error when path points to a file that does not exist', () => {
const totalPath = path.resolve(local1Folder, '');
expect(() => {
cache.getResourceAtPath(totalPath);
}).toThrow(/Failed to find XML or JSON file/);
});
});
});

0 comments on commit 3fb2357

Please sign in to comment.