Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maryia-lapata committed Apr 17, 2020
1 parent d79236c commit 28a7912
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
* under the License.
*/

import { SavedObjectUnsanitizedDoc } from 'kibana/server';
import { dashboardSavedObjectTypeMigrations as migrations } from './dashboard_migrations';

describe('dashboard', () => {
describe('7.0.0', () => {
const migration = migrations['7.0.0'];

test('skips error on empty object', () => {
expect(migration({})).toMatchInlineSnapshot(`
expect(migration({} as SavedObjectUnsanitizedDoc)).toMatchInlineSnapshot(`
Object {
"references": Array [],
}
Expand Down Expand Up @@ -329,7 +330,7 @@ Object {
attributes: {
panelsJSON: 123,
},
};
} as SavedObjectUnsanitizedDoc;
expect(migration(doc)).toMatchInlineSnapshot(`
Object {
"attributes": Object {
Expand All @@ -347,7 +348,7 @@ Object {
attributes: {
panelsJSON: '{123abc}',
},
};
} as SavedObjectUnsanitizedDoc;
expect(migration(doc)).toMatchInlineSnapshot(`
Object {
"attributes": Object {
Expand All @@ -365,7 +366,7 @@ Object {
attributes: {
panelsJSON: '{}',
},
};
} as SavedObjectUnsanitizedDoc;
expect(migration(doc)).toMatchInlineSnapshot(`
Object {
"attributes": Object {
Expand All @@ -383,7 +384,7 @@ Object {
attributes: {
panelsJSON: '[{"id":"123"}]',
},
};
} as SavedObjectUnsanitizedDoc;
expect(migration(doc)).toMatchInlineSnapshot(`
Object {
"attributes": Object {
Expand All @@ -401,7 +402,7 @@ Object {
attributes: {
panelsJSON: '[{"type":"visualization"}]',
},
};
} as SavedObjectUnsanitizedDoc;
expect(migration(doc)).toMatchInlineSnapshot(`
Object {
"attributes": Object {
Expand All @@ -420,7 +421,7 @@ Object {
panelsJSON:
'[{"id":"1","type":"visualization","foo":true},{"id":"2","type":"visualization","bar":true}]',
},
};
} as SavedObjectUnsanitizedDoc;
const migratedDoc = migration(doc);
expect(migratedDoc).toMatchInlineSnapshot(`
Object {
Expand Down
43 changes: 28 additions & 15 deletions src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@

import { get, flow } from 'lodash';

import { SavedObjectMigrationFn } from 'kibana/server';
import {
SavedObjectMigrationFn,
SavedObjectUnsanitizedDoc,
SavedObjectMigrationContext,
} from 'kibana/server';
import { migrations730 } from './migrations_730';
import { migrateMatchAllQuery } from './migrate_match_all_query';
import { DashboardDoc700To720 } from '../../common';

const migrations700: SavedObjectMigrationFn = doc => {
// Set new "references" attribute
doc.references = doc.references || [];

// Migrate index pattern
function migrateIndexPattern(doc: DashboardDoc700To720) {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
if (typeof searchSourceJSON !== 'string') {
return doc;
return;
}
let searchSource;
try {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
return;
}
if (searchSource.index) {
searchSource.indexRefName = 'kibanaSavedObjectMeta.searchSourceJSON.index';
Expand All @@ -63,28 +64,35 @@ const migrations700: SavedObjectMigrationFn = doc => {
});
}
doc.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(searchSource);
}

const migrations700: SavedObjectMigrationFn = (doc): DashboardDoc700To720 => {
// Set new "references" attribute
doc.references = doc.references || [];

// Migrate index pattern
migrateIndexPattern(doc as DashboardDoc700To720);
// Migrate panels
const panelsJSON = get(doc, 'attributes.panelsJSON');
if (typeof panelsJSON !== 'string') {
return doc;
return doc as DashboardDoc700To720;
}
let panels;
try {
panels = JSON.parse(panelsJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
return doc as DashboardDoc700To720;
}
if (!Array.isArray(panels)) {
return doc;
return doc as DashboardDoc700To720;
}
panels.forEach((panel, i) => {
if (!panel.type || !panel.id) {
return;
}
panel.panelRefName = `panel_${i}`;
doc.references.push({
doc.references!.push({
name: `panel_${i}`,
type: panel.type,
id: panel.id,
Expand All @@ -93,7 +101,7 @@ const migrations700: SavedObjectMigrationFn = doc => {
delete panel.id;
});
doc.attributes.panelsJSON = JSON.stringify(panels);
return doc;
return doc as DashboardDoc700To720;
};

export const dashboardSavedObjectTypeMigrations = {
Expand All @@ -108,6 +116,11 @@ export const dashboardSavedObjectTypeMigrations = {
* only contained the 6.7.2 migration and not the 7.0.1 migration.
*/
'6.7.2': flow<SavedObjectMigrationFn>(migrateMatchAllQuery),
'7.0.0': flow<SavedObjectMigrationFn>(migrations700),
'7.3.0': flow<SavedObjectMigrationFn>(migrations730),
'7.0.0': flow<(doc: SavedObjectUnsanitizedDoc) => DashboardDoc700To720>(migrations700),
'7.3.0': flow<
(
doc: SavedObjectUnsanitizedDoc,
context: SavedObjectMigrationContext
) => SavedObjectUnsanitizedDoc
>(migrations730),
};
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ test('dashboard migration 7.3.0 migrates filters to query on search source when
},
};

const doc700: DashboardDoc700To720 = migrations.dashboard['7.0.0'](doc, mockContext);
const newDoc = migrations.dashboard['7.3.0'](doc700, mockContext);
const doc700: DashboardDoc700To720 = migrations['7.0.0'](doc);
const newDoc = migrations['7.3.0'](doc700, mockContext);

const parsedSearchSource = JSON.parse(newDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON);
expect(parsedSearchSource.filter.length).toBe(0);
Expand Down Expand Up @@ -127,8 +127,8 @@ test('dashboard migration works when panelsJSON is missing panelIndex', () => {
},
};

const doc700: DashboardDoc700To720 = migrations.dashboard['7.0.0'](doc, mockContext);
const newDoc = migrations.dashboard['7.3.0'](doc700, mockContext);
const doc700: DashboardDoc700To720 = migrations['7.0.0'](doc);
const newDoc = migrations['7.3.0'](doc700, mockContext);

const parsedSearchSource = JSON.parse(newDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON);
expect(parsedSearchSource.filter.length).toBe(0);
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/dashboard/server/saved_objects/migrations_730.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/

import { inspect } from 'util';
import { SavedObjectMigrationFn } from 'kibana/server';
import { SavedObjectMigrationContext } from 'kibana/server';
import { DashboardDoc730ToLatest } from '../../common';
import { isDashboardDoc } from './is_dashboard_doc';
import { moveFiltersToQuery } from './move_filters_to_query';
import { migratePanelsTo730 } from '../../common/migrate_to_730_panels';
import { migratePanelsTo730, DashboardDoc700To720 } from '../../common';

export const migrations730: SavedObjectMigrationFn = (doc, { log }) => {
export const migrations730 = (doc: DashboardDoc700To720, { log }: SavedObjectMigrationContext) => {
if (!isDashboardDoc(doc)) {
// NOTE: we should probably throw an error here... but for now following suit and in the
// case of errors, just returning the same document.
Expand Down

0 comments on commit 28a7912

Please sign in to comment.