mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 14:19:40 +00:00
add engine tests
This commit is contained in:
parent
e20ce4ba47
commit
f7a261db7d
@ -27,8 +27,8 @@ export const getStrapiFactory =
|
|||||||
>(
|
>(
|
||||||
properties?: T
|
properties?: T
|
||||||
) =>
|
) =>
|
||||||
() => {
|
(additionalProperties?: T) => {
|
||||||
return { ...properties } as Strapi.Strapi;
|
return { ...properties, ...additionalProperties } as Strapi.Strapi;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,11 +1,105 @@
|
|||||||
import { Readable } from 'stream';
|
import { Readable } from 'stream';
|
||||||
import { Duplex } from 'stream-chain';
|
import { Duplex } from 'stream-chain';
|
||||||
import { createTransferEngine } from '..';
|
import { createTransferEngine } from '..';
|
||||||
import { IDestinationProvider, ISourceProvider, ITransferEngineOptions } from '../../../types';
|
import {
|
||||||
|
IDestinationProvider,
|
||||||
|
ISourceProvider,
|
||||||
|
ITransferEngine,
|
||||||
|
ITransferEngineOptions,
|
||||||
|
} from '../../../types';
|
||||||
|
|
||||||
import { collect, createMockedQueryBuilder, getStrapiFactory } from '../../__tests__/test-utils';
|
import { getStrapiFactory } from '../../__tests__/test-utils';
|
||||||
|
|
||||||
const strapiFactory = getStrapiFactory();
|
const strapiFactory = getStrapiFactory({});
|
||||||
|
|
||||||
|
const providerStages = ['bootstrap', 'close'];
|
||||||
|
|
||||||
|
const sourceStages = [
|
||||||
|
...providerStages,
|
||||||
|
'streamEntities',
|
||||||
|
'streamLinks',
|
||||||
|
'streamMedia',
|
||||||
|
'streamConfiguration',
|
||||||
|
'streamSchemas',
|
||||||
|
];
|
||||||
|
const destinationStages = [
|
||||||
|
...providerStages,
|
||||||
|
'getEntitiesStream',
|
||||||
|
'getLinksStream',
|
||||||
|
'getMediaStream',
|
||||||
|
'getConfigurationStream',
|
||||||
|
'getSchemasStream',
|
||||||
|
];
|
||||||
|
|
||||||
|
// add some helpers to jest
|
||||||
|
expect.extend({
|
||||||
|
toBeValidTransferEngine(engine: ITransferEngine) {
|
||||||
|
try {
|
||||||
|
expect(engine).toBeDefined();
|
||||||
|
|
||||||
|
// All required methods exists
|
||||||
|
expect(engine.integrityCheck).toBeInstanceOf(Function);
|
||||||
|
expect(engine.transfer).toBeInstanceOf(Function);
|
||||||
|
expect(engine.bootstrap).toBeInstanceOf(Function);
|
||||||
|
expect(engine.close).toBeInstanceOf(Function);
|
||||||
|
expect(engine.transferSchemas).toBeInstanceOf(Function);
|
||||||
|
expect(engine.transferEntities).toBeInstanceOf(Function);
|
||||||
|
expect(engine.transferLinks).toBeInstanceOf(Function);
|
||||||
|
expect(engine.transferMedia).toBeInstanceOf(Function);
|
||||||
|
expect(engine.transferConfiguration).toBeInstanceOf(Function);
|
||||||
|
expect(engine.close).toBeInstanceOf(Function);
|
||||||
|
expect(engine.transfer).toBeInstanceOf(Function);
|
||||||
|
} catch (e) {
|
||||||
|
return {
|
||||||
|
pass: false,
|
||||||
|
message: () => `Expected engine to be valid: ${e.message}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
pass: true,
|
||||||
|
message: () => `Expected engine not to be valid`,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
toHaveSourceStagesCalledOnce(provider: ISourceProvider) {
|
||||||
|
try {
|
||||||
|
sourceStages.forEach((stage) => {
|
||||||
|
if (provider[stage]) {
|
||||||
|
expect(provider[stage]).toHaveBeenCalledTimes(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
return {
|
||||||
|
pass: false,
|
||||||
|
message: () => `Expected provider to have all stages called ${e.message}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
pass: true,
|
||||||
|
message: () => `Expected provider not to have all stages called`,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
toHaveDestinationStagesCalledOnce(provider: IDestinationProvider) {
|
||||||
|
try {
|
||||||
|
destinationStages.forEach((stage) => {
|
||||||
|
// only check the stages that exist
|
||||||
|
if (provider[stage]) {
|
||||||
|
expect(provider[stage]).toHaveBeenCalledTimes(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
return {
|
||||||
|
pass: false,
|
||||||
|
message: () => `Expected provider to have all stages called`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
pass: true,
|
||||||
|
message: () => `Expected provider not to have all stages called`,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
describe('Transfer engine', () => {
|
describe('Transfer engine', () => {
|
||||||
// TODO: if these are needed for any other tests, a factory should be added to test-utils
|
// TODO: if these are needed for any other tests, a factory should be added to test-utils
|
||||||
@ -44,29 +138,18 @@ describe('Transfer engine', () => {
|
|||||||
exclude: [],
|
exclude: [],
|
||||||
} as ITransferEngineOptions;
|
} as ITransferEngineOptions;
|
||||||
const engine = createTransferEngine(mockedSource, mockedDestination, engineOptions);
|
const engine = createTransferEngine(mockedSource, mockedDestination, engineOptions);
|
||||||
expect(engine).toBeDefined();
|
expect(engine).toBeValidTransferEngine();
|
||||||
|
|
||||||
// All required methods exists
|
|
||||||
expect(engine.integrityCheck).toBeInstanceOf(Function);
|
|
||||||
expect(engine.transfer).toBeInstanceOf(Function);
|
|
||||||
expect(engine.bootstrap).toBeInstanceOf(Function);
|
|
||||||
expect(engine.close).toBeInstanceOf(Function);
|
|
||||||
expect(engine.transferSchemas).toBeInstanceOf(Function);
|
|
||||||
expect(engine.transferEntities).toBeInstanceOf(Function);
|
|
||||||
expect(engine.transferLinks).toBeInstanceOf(Function);
|
|
||||||
expect(engine.transferMedia).toBeInstanceOf(Function);
|
|
||||||
expect(engine.transferConfiguration).toBeInstanceOf(Function);
|
|
||||||
expect(engine.close).toBeInstanceOf(Function);
|
|
||||||
expect(engine.transfer).toBeInstanceOf(Function);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('bootstrap', () => {
|
describe('bootstrap', () => {
|
||||||
test('works for providers without a bootstrap', async () => {
|
test('works for providers without a bootstrap', async () => {
|
||||||
const engine = createTransferEngine(mockedSource, mockedDestination, defaultOptions);
|
const engine = createTransferEngine(mockedSource, mockedDestination, defaultOptions);
|
||||||
|
expect(engine).toBeValidTransferEngine();
|
||||||
await engine.transfer();
|
await engine.transfer();
|
||||||
|
expect(mockedSource).toHaveSourceStagesCalledOnce();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('bootstraps all providers with a bootstrap', async () => {
|
test('bootstraps all providers with a bootstrap', async () => {
|
||||||
const source = {
|
const source = {
|
||||||
...mockedSource,
|
...mockedSource,
|
||||||
@ -77,11 +160,10 @@ describe('Transfer engine', () => {
|
|||||||
bootstrap: jest.fn().mockReturnValue(new Duplex()),
|
bootstrap: jest.fn().mockReturnValue(new Duplex()),
|
||||||
};
|
};
|
||||||
const engine = createTransferEngine(source, destination, defaultOptions);
|
const engine = createTransferEngine(source, destination, defaultOptions);
|
||||||
|
expect(engine).toBeValidTransferEngine();
|
||||||
await engine.transfer();
|
await engine.transfer();
|
||||||
|
|
||||||
expect(source.bootstrap).toHaveBeenCalledTimes(1);
|
expect(mockedSource).toHaveSourceStagesCalledOnce();
|
||||||
expect(destination.bootstrap).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -97,12 +179,14 @@ describe('Transfer engine', () => {
|
|||||||
strategy: 'restore',
|
strategy: 'restore',
|
||||||
});
|
});
|
||||||
await restoreEngine.transfer();
|
await restoreEngine.transfer();
|
||||||
|
expect(restoreEngine).toBeValidTransferEngine();
|
||||||
|
|
||||||
const mergeEngine = createTransferEngine(mockedSource, mockedDestination, {
|
const mergeEngine = createTransferEngine(mockedSource, mockedDestination, {
|
||||||
...engineOptions,
|
...engineOptions,
|
||||||
strategy: 'merge',
|
strategy: 'merge',
|
||||||
});
|
});
|
||||||
await mergeEngine.transfer();
|
await mergeEngine.transfer();
|
||||||
|
expect(mergeEngine).toBeValidTransferEngine();
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
(async () => {
|
(async () => {
|
||||||
@ -115,5 +199,24 @@ describe('Transfer engine', () => {
|
|||||||
})()
|
})()
|
||||||
).rejects.toThrow();
|
).rejects.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('calls all provider stages', async () => {
|
||||||
|
const engineOptions = {
|
||||||
|
versionMatching: 'exact',
|
||||||
|
exclude: [],
|
||||||
|
strategy: 'restore',
|
||||||
|
} as unknown as ITransferEngineOptions;
|
||||||
|
const engine = createTransferEngine(mockedSource, mockedDestination, engineOptions);
|
||||||
|
|
||||||
|
await engine.transfer();
|
||||||
|
|
||||||
|
expect(mockedSource).toHaveSourceStagesCalledOnce();
|
||||||
|
expect(mockedDestination).toHaveDestinationStagesCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
test.todo('returns provider results');
|
||||||
|
});
|
||||||
|
describe('progressStream', () => {
|
||||||
|
test.todo('returns correct result count for each stage');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user