Skip to content

Commit

Permalink
Add tests for DefaultPackageLoader
Browse files Browse the repository at this point in the history
Add tests for lookup functions in BasePackageLoader.
  • Loading branch information
mint-thompson committed Oct 1, 2024
1 parent a3053b1 commit fcda419
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 84 deletions.
243 changes: 188 additions & 55 deletions test/loader/BasePackageLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,109 +1142,242 @@ describe('BasePackageLoader', () => {
describe('#findPackageInfos', () => {
it('should return package info array', () => {
const name = 'some.ig';
const version = '1.2.3';
loader.findPackageInfos = jest.fn().mockReturnValueOnce([{ name, version }]);
packageDBMock.findPackageInfos.calledWith(name).mockReturnValueOnce([
{ name: 'some.ig', version: '1.2.3' },
{ name: 'some.ig', version: '2.3.4' }
]);
const result = loader.findPackageInfos(name);
expect(result[0].name).toBe(name);
expect(result[0].version).toBe(version);
expect(result).toEqual([
{ name: 'some.ig', version: '1.2.3' },
{ name: 'some.ig', version: '2.3.4' }
]);
});
});

describe('#findPackageInfo', () => {
it('should return package info object', () => {
const name = 'some.ig';
const version = '1.2.3';
loader.findPackageInfo = jest.fn().mockReturnValueOnce({ name, version });
packageDBMock.findPackageInfo
.calledWith(name, version)
.mockReturnValueOnce({ name: 'some.ig', version: '1.2.3' });
const result = loader.findPackageInfo(name, version);
expect(result.name).toBe(name);
expect(result.version).toBe(version);
expect(result.name).toBe('some.ig');
expect(result.version).toBe('1.2.3');
});
});

describe('#findPackageJSONs', () => {
it('should return package json array', () => {
const name = 'some.ig';
loader.findPackageJSONs = jest
.fn()
.mockReturnValueOnce([
{ resource: 'json/path/name', date: '20240824230227', resourceType: 'resourceTypeName' }
]);
packageDBMock.findPackageInfos.calledWith(name).mockReturnValueOnce([
{
name: 'some.ig',
version: '1.2.3',
packageJSONPath: '/first/package/package.json'
},
{
name: 'some.ig',
version: '2.3.4'
},
{
name: 'some.ig',
version: '3.4.5',
packageJSONPath: '/third/package/package.json'
}
]);
packageCacheMock.getResourceAtPath
.calledWith('/first/package/package.json')
.mockReturnValueOnce({ id: 'some.ig', version: '1.2.3' });
packageCacheMock.getResourceAtPath
.calledWith('/second/package/package.json')
.mockReturnValueOnce({ id: 'some.ig', version: '2.3.4' });
packageCacheMock.getResourceAtPath
.calledWith('/third/package/package.json')
.mockReturnValueOnce({ id: 'some.ig', version: '3.4.5' });
const result = loader.findPackageJSONs(name);
expect(result[0].resource).toBe('json/path/name');
expect(result[0].date).toBe('20240824230227');
expect(result[0].resourceType).toBe('resourceTypeName');
expect(packageCacheMock.getResourceAtPath).toHaveBeenCalledTimes(2);
expect(result).toHaveLength(2);
expect(result[0]).toEqual({ id: 'some.ig', version: '1.2.3' });
expect(result[1]).toEqual({ id: 'some.ig', version: '3.4.5' });
});
});

describe('#findPackageJSON', () => {
it('should return package json array', () => {
it('should return package json', () => {
const name = 'some.ig';
const version = '1.2.3';
loader.findPackageJSON = jest.fn().mockReturnValueOnce({
resource: 'json/path/name',
packageDBMock.findPackageInfo.calledWith('some.ig', '1.2.3').mockReturnValueOnce({
name: 'some.ig',
version: '1.2.3',
packageJSONPath: '/some/package/package.json'
});
packageCacheMock.getResourceAtPath
.calledWith('/some/package/package.json')
.mockReturnValueOnce({
date: '20240824230227',
resourceType: 'resourceTypeName'
});
const result = loader.findPackageJSON(name, version);
expect(result).toEqual({
date: '20240824230227',
resourceType: 'resourceTypeName'
});
});

it('should return undefined when the info does not contain a packageJSONPath', () => {
const name = 'some.ig';
const version = '1.2.3';
packageDBMock.findPackageInfo.calledWith('some.ig', '1.2.3').mockReturnValueOnce({
name: 'some.ig',
version: '1.2.3'
});
packageCacheMock.getResourceAtPath
.calledWith('/some/package/package.json')
.mockReturnValueOnce({
date: '20240824230227',
resourceType: 'resourceTypeName'
});
const result = loader.findPackageJSON(name, version);
expect(result).toBeUndefined();
});

it('should return undefined when no info is found', () => {
const name = 'some.ig';
const version = '1.2.3';
packageDBMock.findPackageInfo.calledWith('some.ig', '1.2.3').mockReturnValueOnce(undefined);
packageCacheMock.getResourceAtPath
.calledWith('/some/package/package.json')
.mockReturnValueOnce({
date: '20240824230227',
resourceType: 'resourceTypeName'
});
const result = loader.findPackageJSON(name, version);
expect(result.resource).toBe('json/path/name');
expect(result.date).toBe('20240824230227');
expect(result.resourceType).toBe('resourceTypeName');
expect(result).toBeUndefined();
});
});

describe('#findResourceInfos', () => {
it('should return resource array', () => {
const name = 'some.ig';
loader.findResourceInfos = jest
.fn()
.mockReturnValueOnce([
{ resource: 'json/path/name', date: '20240824230227', resourceType: 'resourceTypeName' }
]);
const result = loader.findResourceInfos(name);
expect(result[0].resourceType).toBe('resourceTypeName');
const resourceInfos = [
{
name: 'firstResource',
resourceType: 'StructureDefinition',
version: '1.2.3',
resourcePath: '/some/package/first-thing.json'
},
{
name: 'secondResource',
resourceType: 'ValueSet',
version: '1.2.3'
},
{
name: 'thirdResource',
resourceType: 'CodeSystem',
version: '1.2.3',
resourcePath: '/some/package/third-thing.json'
}
];
packageDBMock.findResourceInfos.calledWith('*').mockReturnValueOnce(resourceInfos);
// packageCacheMock.getResourceAtPath
// .calledWith('/some/package/first-thing.json')
// .mockReturnValueOnce({ id: 'first-thing', version: '1.2.3' });
// packageCacheMock.getResourceAtPath
// .calledWith('/some/package/second-thing.json')
// .mockReturnValueOnce({ id: 'second-thing', version: '1.2.3' });
// packageCacheMock.getResourceAtPath
// .calledWith('/some/package/third-thing.json')
// .mockReturnValueOnce({ id: 'third-thing', version: '1.2.3' });
const result = loader.findResourceInfos('*');
// expect(packageCacheMock).toHaveBeenCalledTimes(2);
expect(result).toHaveLength(3);
expect(result).toEqual(resourceInfos);
});
});

describe('#findResourceInfo', () => {
it('should return resource info', () => {
const name = 'some.ig';
loader.findResourceInfo = jest.fn().mockReturnValueOnce({
resource: 'json/path/name',
date: '20240824230227',
resourceType: 'resourceTypeName'
packageDBMock.findResourceInfo.calledWith('firstResource').mockReturnValueOnce({
name: 'firstResource',
resourceType: 'StructureDefinition',
version: '1.2.3',
resourcePath: '/some/package/first-thing.json'
});
const result = loader.findResourceInfo('firstResource');
expect(result).toEqual({
name: 'firstResource',
resourceType: 'StructureDefinition',
version: '1.2.3',
resourcePath: '/some/package/first-thing.json'
});
const result = loader.findResourceInfo(name);
expect(result.resourceType).toBe('resourceTypeName');
});
});

describe('#findResourceJSONs', () => {
it('should return resource json array', () => {
const name = 'some.ig';
loader.findResourceJSONs = jest
.fn()
.mockReturnValueOnce([
{ resource: 'json/path/name', date: '20240824230227', resourceType: 'resourceTypeName' }
]);
const result = loader.findResourceJSONs(name);
expect(result[0].resource).toBe('json/path/name');
expect(result[0].date).toBe('20240824230227');
expect(result[0].resourceType).toBe('resourceTypeName');
const resourceInfos = [
{
name: 'firstResource',
resourceType: 'StructureDefinition',
version: '1.2.3',
resourcePath: '/some/package/first-thing.json'
},
{
name: 'secondResource',
resourceType: 'ValueSet',
version: '1.2.3'
},
{
name: 'thirdResource',
resourceType: 'CodeSystem',
version: '1.2.3',
resourcePath: '/some/package/third-thing.json'
}
];
packageDBMock.findResourceInfos.calledWith('*').mockReturnValueOnce(resourceInfos);
packageCacheMock.getResourceAtPath
.calledWith('/some/package/first-thing.json')
.mockReturnValueOnce({ id: 'first-thing', version: '1.2.3' });
packageCacheMock.getResourceAtPath
.calledWith('/some/package/second-thing.json')
.mockReturnValueOnce({ id: 'second-thing', version: '1.2.3' });
packageCacheMock.getResourceAtPath
.calledWith('/some/package/third-thing.json')
.mockReturnValueOnce({ id: 'third-thing', version: '1.2.3' });
const result = loader.findResourceJSONs('*');
expect(result).toEqual([
{ id: 'first-thing', version: '1.2.3' },
{ id: 'third-thing', version: '1.2.3' }
]);
});
});

describe('#findResourceJSON', () => {
it('should return resource json', () => {
const name = 'some.ig';
loader.findResourceJSON = jest.fn().mockReturnValueOnce({
resource: 'json/path/name',
date: '20240824230227',
resourceType: 'resourceTypeName'
packageDBMock.findResourceInfo.calledWith('firstResource').mockReturnValueOnce({
name: 'firstResource',
resourceType: 'StructureDefinition',
version: '1.2.3',
resourcePath: '/some/package/first-thing.json'
});
const result = loader.findResourceJSON(name);
expect(result.resource).toBe('json/path/name');
expect(result.date).toBe('20240824230227');
expect(result.resourceType).toBe('resourceTypeName');
packageCacheMock.getResourceAtPath
.calledWith('/some/package/first-thing.json')
.mockReturnValueOnce({ id: 'first-thing', version: '1.2.3' });
const result = loader.findResourceJSON('firstResource');
expect(result).toEqual({ id: 'first-thing', version: '1.2.3' });
});

it('should return undefined when the info does not contain a resourcePath', () => {
packageDBMock.findResourceInfo.calledWith('first-thing').mockReturnValueOnce({
name: 'firstResource',
resourceType: 'StructureDefinition',
version: '1.2.3'
});
packageCacheMock.getResourceAtPath
.calledWith('/some/package/first-thing.json')
.mockReturnValueOnce({ id: 'first-thing', version: '1.2.3' });
const result = loader.findResourceJSON('firstResource');
expect(result).toBeUndefined();
});
});

Expand Down
77 changes: 48 additions & 29 deletions test/loader/DefaultPackageLoader.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
import { loggerSpy } from '../testhelpers';
import { mock } from 'jest-mock-extended';
import { BasePackageLoader, BasePackageLoaderOptions } from '../../src/loader/BasePackageLoader';
import { LoadStatus } from '../../src/loader/PackageLoader';
import { PackageDB } from '../../src/db';
import { PackageCache } from '../../src/cache';
import { RegistryClient } from '../../src/registry';
import { CurrentBuildClient } from '../../src/current';
import { jest } from '@jest/globals';
import { defaultPackageLoader, defaultPackageLoaderWithLocalResources } from '../../src/loader';
import { BasePackageLoader } from '../../src/loader/BasePackageLoader';
import { DiskBasedPackageCache } from '../../src/cache/DiskBasedPackageCache';

jest.mock('sql.js', () => {
return () => {
class Database {}
return {
Database
};
};
});
jest.mock('../../src/db/SQLJSPackageDB');
jest.mock('../../src/cache/DiskBasedPackageCache', () => {
return {
DiskBasedPackageCache: jest.fn().mockImplementation(() => {
return {};
})
};
});
jest.mock('../../src/registry/DefaultRegistryClient');
jest.mock('../../src/current/BuildDotFhirDotOrgClient');

describe('DefaultPackageLoader', () => {
async function makeMyDefaultPackageLoader(
options: BasePackageLoaderOptions
): Promise<BasePackageLoader> {
const packageDBMock = mock<PackageDB>();
const packageCacheMock = mock<PackageCache>();
const registryClientMock = mock<RegistryClient>();
const currentBuildClientMock = mock<CurrentBuildClient>();
const loader = new BasePackageLoader(
packageDBMock,
packageCacheMock,
registryClientMock,
currentBuildClientMock,
{ log: options.log }
);
return loader;
}
beforeEach(() => {
(DiskBasedPackageCache as jest.Mock).mockClear();
});

it('should create a package loader with mock default package loader', async () => {
const loader = await makeMyDefaultPackageLoader({ log: loggerSpy.log });
loader.getPackageLoadStatus = jest.fn().mockReturnValueOnce(LoadStatus.LOADED);
it('should create an instance of BasePackageLoader with no local resource folders', async () => {
const loader = await defaultPackageLoader({ log: loggerSpy.log });
expect(loader).toBeInstanceOf(BasePackageLoader);
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledTimes(1);
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledWith(expect.any(String), [], {
log: loggerSpy.log
});
});

const result = await loader.loadPackage('some.ig', '1.2.3');
expect(result).toBe(LoadStatus.LOADED);
expect(loader.getPackageLoadStatus).toHaveBeenCalledWith('some.ig', '1.2.3');
it('should create an instance of BasePackageLoader with specified local resource folders', async () => {
const loader = await defaultPackageLoaderWithLocalResources(
['/some/folder', '/another/good/folder'],
{ log: loggerSpy.log }
);
expect(loader).toBeInstanceOf(BasePackageLoader);
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledTimes(1);
expect(DiskBasedPackageCache as jest.Mock).toHaveBeenCalledWith(
expect.any(String),
['/some/folder', '/another/good/folder'],
{
log: loggerSpy.log
}
);
});
});

0 comments on commit fcda419

Please sign in to comment.