Skip to content

Commit

Permalink
fix/adapt zdt algo utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Apr 27, 2023
1 parent 1bca7a3 commit f3f37ed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
* Side Public License, v 1.
*/

export const getModelVersionsFromMappingsMock = jest.fn();
export const compareModelVersionsMock = jest.fn();
export const getModelVersionMapForTypesMock = jest.fn();
export const getVirtualVersionsFromMappingsMock = jest.fn();
export const compareVirtualVersionsMock = jest.fn();
export const getVirtualVersionMapMock = jest.fn();

jest.doMock('@kbn/core-saved-objects-base-server-internal', () => {
const actual = jest.requireActual('@kbn/core-saved-objects-base-server-internal');
return {
...actual,
getModelVersionsFromMappings: getModelVersionsFromMappingsMock,
compareModelVersions: compareModelVersionsMock,
getModelVersionMapForTypes: getModelVersionMapForTypesMock,
getVirtualVersionsFromMappings: getVirtualVersionsFromMappingsMock,
compareVirtualVersions: compareVirtualVersionsMock,
getVirtualVersionMap: getVirtualVersionMapMock,
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
*/

import {
compareModelVersionsMock,
getModelVersionsFromMappingsMock,
getModelVersionMapForTypesMock,
compareVirtualVersionsMock,
getVirtualVersionMapMock,
getVirtualVersionsFromMappingsMock,
} from './check_version_compatibility.test.mocks';
import type { SavedObjectsType } from '@kbn/core-saved-objects-server';
import type {
IndexMapping,
ModelVersionMap,
VirtualVersionMap,
CompareModelVersionResult,
} from '@kbn/core-saved-objects-base-server-internal';
import { checkVersionCompatibility } from './check_version_compatibility';
Expand All @@ -27,9 +27,9 @@ describe('checkVersionCompatibility', () => {
let mappings: IndexMapping;

beforeEach(() => {
compareModelVersionsMock.mockReset().mockReturnValue({});
getModelVersionsFromMappingsMock.mockReset().mockReturnValue({});
getModelVersionMapForTypesMock.mockReset().mockReturnValue({ status: 'equal' });
compareVirtualVersionsMock.mockReset().mockReturnValue({});
getVirtualVersionMapMock.mockReset().mockReturnValue({});
getVirtualVersionsFromMappingsMock.mockReset().mockReturnValue({ status: 'equal' });

types = [createType({ name: 'foo' }), createType({ name: 'bar' })];

Expand All @@ -46,8 +46,8 @@ describe('checkVersionCompatibility', () => {
deletedTypes,
});

expect(getModelVersionMapForTypesMock).toHaveBeenCalledTimes(1);
expect(getModelVersionMapForTypesMock).toHaveBeenCalledWith(types);
expect(getVirtualVersionMapMock).toHaveBeenCalledTimes(1);
expect(getVirtualVersionMapMock).toHaveBeenCalledWith(types);
});

it('calls getModelVersionsFromMappings with the correct parameters', () => {
Expand All @@ -58,20 +58,20 @@ describe('checkVersionCompatibility', () => {
deletedTypes,
});

expect(getModelVersionsFromMappingsMock).toHaveBeenCalledTimes(1);
expect(getModelVersionsFromMappingsMock).toHaveBeenCalledWith({
expect(getVirtualVersionsFromMappingsMock).toHaveBeenCalledTimes(1);
expect(getVirtualVersionsFromMappingsMock).toHaveBeenCalledWith({
mappings,
source: 'mappingVersions',
knownTypes: ['foo', 'bar'],
});
});

it('calls compareModelVersions with the correct parameters', () => {
const appVersions: ModelVersionMap = { foo: 2, bar: 2 };
const indexVersions: ModelVersionMap = { foo: 1, bar: 1 };
const appVersions: VirtualVersionMap = { foo: '10.2.0', bar: '10.2.0' };
const indexVersions: VirtualVersionMap = { foo: '10.1.0', bar: '10.1.0' };

getModelVersionMapForTypesMock.mockReturnValue(appVersions);
getModelVersionsFromMappingsMock.mockReturnValue(indexVersions);
getVirtualVersionMapMock.mockReturnValue(appVersions);
getVirtualVersionsFromMappingsMock.mockReturnValue(indexVersions);

checkVersionCompatibility({
types,
Expand All @@ -80,8 +80,8 @@ describe('checkVersionCompatibility', () => {
deletedTypes,
});

expect(compareModelVersionsMock).toHaveBeenCalledTimes(1);
expect(compareModelVersionsMock).toHaveBeenCalledWith({
expect(compareVirtualVersionsMock).toHaveBeenCalledTimes(1);
expect(compareVirtualVersionsMock).toHaveBeenCalledWith({
appVersions,
indexVersions,
deletedTypes,
Expand All @@ -97,7 +97,7 @@ describe('checkVersionCompatibility', () => {
equal: [],
},
};
compareModelVersionsMock.mockReturnValue(expected);
compareVirtualVersionsMock.mockReturnValue(expected);

const result = checkVersionCompatibility({
types,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@
* Side Public License, v 1.
*/

import type { SavedObjectsModelVersion } from '@kbn/core-saved-objects-server';
import type { IndexMappingMeta } from '@kbn/core-saved-objects-base-server-internal';
import { generateAdditiveMappingDiff } from './generate_additive_mapping_diff';
import { createType } from '../test_helpers';

describe('generateAdditiveMappingDiff', () => {
const deletedTypes = ['deletedType'];

const stubMigration = jest.fn();
const stubModelVersion: SavedObjectsModelVersion = { modelChange: { type: 'expansion' } };

const getTypes = () => {
const foo = createType({
name: 'foo',
switchToModelVersionAt: '8.0.0',
modelVersions: {
1: { modelChange: { type: 'expansion' } },
2: { modelChange: { type: 'expansion' } },
1: stubModelVersion,
2: stubModelVersion,
},
mappings: { properties: { fooProp: { type: 'text' } } },
});
const bar = createType({
name: 'bar',
modelVersions: {
1: { modelChange: { type: 'expansion' } },
2: { modelChange: { type: 'expansion' } },
migrations: {
'8.0.0': stubMigration,
'8.5.0': stubMigration,
},
mappings: { properties: { barProp: { type: 'text' } } },
});
Expand All @@ -39,8 +44,8 @@ describe('generateAdditiveMappingDiff', () => {
const types = [foo, bar];
const meta: IndexMappingMeta = {
mappingVersions: {
foo: 1,
bar: 1,
foo: '10.1.0',
bar: '7.9.0',
},
};

Expand All @@ -61,8 +66,8 @@ describe('generateAdditiveMappingDiff', () => {
const types = [foo, bar];
const meta: IndexMappingMeta = {
mappingVersions: {
foo: 1,
bar: 2,
foo: '10.1.0',
bar: '8.5.0',
},
};

Expand All @@ -82,9 +87,9 @@ describe('generateAdditiveMappingDiff', () => {
const types = [foo, bar];
const meta: IndexMappingMeta = {
mappingVersions: {
foo: 1,
bar: 1,
deletedType: 42,
foo: '10.1.0',
bar: '8.2.0',
deletedType: '10.2.0',
},
};

Expand All @@ -105,8 +110,8 @@ describe('generateAdditiveMappingDiff', () => {
const types = [foo, bar];
const meta: IndexMappingMeta = {
mappingVersions: {
foo: 1,
bar: 3,
foo: '10.1.0',
bar: '10.1.0',
},
};

Expand Down

0 comments on commit f3f37ed

Please sign in to comment.