Skip to content

Commit

Permalink
Fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Jan 9, 2019
1 parent d635889 commit 062b72b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ describe('findRelationships', () => {
],
})
};
const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', title: 'Foo', type: 'visualization' },
{ id: '2', title: 'Bar', type: 'visualization' },
Expand Down Expand Up @@ -114,12 +114,12 @@ describe('findRelationships', () => {
})
};

const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', title: 'My Dashboard', type: 'dashboard' },
{ id: '2', title: 'Your Dashboard', type: 'dashboard' },
Expand Down Expand Up @@ -177,12 +177,12 @@ describe('findRelationships', () => {
})
};

const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', title: 'Foo', type: 'visualization' },
{ id: '2', title: 'Bar', type: 'visualization' },
Expand Down Expand Up @@ -282,12 +282,12 @@ describe('findRelationships', () => {
}
};

const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', type: 'visualization', title: 'Foo' },
{ id: '2', type: 'visualization', title: 'Bar' },
Expand All @@ -298,7 +298,7 @@ describe('findRelationships', () => {

it('should return an empty object for invalid types', async () => {
const type = 'invalid';
const result = await findRelationships(type);
const result = await findRelationships({ type });
expect(result).to.eql({});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

async function findDashboardRelationships(id, size, savedObjectsClient) {
const dashboard = await savedObjectsClient.get('dashboard', id);
async function findDashboardRelationships({ id, size, namespace, savedObjectsClient }) {
const dashboard = await savedObjectsClient.get('dashboard', id, { namespace });
const visualizations = [];

// TODO: should we handle exceptions here or at the parent level?
Expand All @@ -29,7 +29,8 @@ async function findDashboardRelationships(id, size, savedObjectsClient) {
visualizationIds.slice(0, size).map(id => ({
id,
type: 'visualization',
}))
})),
{ namespace }
);

visualizations.push(
Expand All @@ -49,9 +50,10 @@ async function findDashboardRelationships(id, size, savedObjectsClient) {
return visualizations;
}

async function findVisualizationRelationships(id, size, savedObjectsClient) {
await savedObjectsClient.get('visualization', id);
async function findVisualizationRelationships({ id, size, namespace, savedObjectsClient }) {
await savedObjectsClient.get('visualization', id, { namespace });
const allDashboardsResponse = await savedObjectsClient.find({
namespace,
type: 'dashboard',
fields: ['title', 'panelsJSON'],
});
Expand Down Expand Up @@ -81,20 +83,21 @@ async function findVisualizationRelationships(id, size, savedObjectsClient) {
return dashboards;
}

async function findSavedSearchRelationships(id, size, savedObjectsClient) {
const search = await savedObjectsClient.get('search', id);
async function findSavedSearchRelationships({ id, namespace, savedObjectsClient }) {
const search = await savedObjectsClient.get('search', id, { namespace });

const searchSourceJSON = JSON.parse(search.attributes.kibanaSavedObjectMeta.searchSourceJSON);

const indexPatterns = [];
try {
const indexPattern = await savedObjectsClient.get('index-pattern', searchSourceJSON.index);
const indexPattern = await savedObjectsClient.get('index-pattern', searchSourceJSON.index, { namespace });
indexPatterns.push({ id: indexPattern.id, type: 'index-pattern', title: indexPattern.attributes.title });
} catch (err) {
// Do nothing
}

const allVisualizationsResponse = await savedObjectsClient.find({
namespace,
type: 'visualization',
searchFields: ['savedSearchId'],
search: id,
Expand All @@ -115,16 +118,18 @@ async function findSavedSearchRelationships(id, size, savedObjectsClient) {
return visualizations.concat(indexPatterns);
}

async function findIndexPatternRelationships(id, size, savedObjectsClient) {
await savedObjectsClient.get('index-pattern', id);
async function findIndexPatternRelationships({ id, size, namespace, savedObjectsClient }) {
await savedObjectsClient.get('index-pattern', id, { namespace });
const [allVisualizationsResponse, savedSearchResponse] = await Promise.all([
savedObjectsClient.find({
namespace,
type: 'visualization',
searchFields: ['kibanaSavedObjectMeta.searchSourceJSON'],
search: '*',
fields: [`title`, `kibanaSavedObjectMeta.searchSourceJSON`],
}),
savedObjectsClient.find({
namespace,
type: 'search',
searchFields: ['kibanaSavedObjectMeta.searchSourceJSON'],
search: '*',
Expand Down Expand Up @@ -172,16 +177,16 @@ async function findIndexPatternRelationships(id, size, savedObjectsClient) {
return visualizations.concat(searches);
}

export async function findRelationships(type, id, size, savedObjectsClient) {
export async function findRelationships({ type, id, namespace, size, savedObjectsClient }) {
switch (type) {
case 'dashboard':
return await findDashboardRelationships(id, size, savedObjectsClient);
return await findDashboardRelationships({ id, size, namespace, savedObjectsClient });
case 'visualization':
return await findVisualizationRelationships(id, size, savedObjectsClient);
return await findVisualizationRelationships({ id, size, namespace, savedObjectsClient });
case 'search':
return await findSavedSearchRelationships(id, size, savedObjectsClient);
return await findSavedSearchRelationships({ id, size, namespace, savedObjectsClient });
case 'index-pattern':
return await findIndexPatternRelationships(id, size, savedObjectsClient);
return await findIndexPatternRelationships({ id, size, namespace, savedObjectsClient });
}
return [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function registerRelationships(server) {
handler: async (req) => {
const type = req.params.type;
const id = req.params.id;
const size = req.query.size || 10;
const size = req.query.size || 10000;
const savedObjectsClient = req.getSavedObjectsClient();

return await savedObjectsClient.findRelationships(type, id, { size });
Expand Down
4 changes: 2 additions & 2 deletions src/server/saved_objects/service/lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ export class SavedObjectsRepository {
// This code will be removed as these types migrate to use "references"
let legacyFindRelationshipsCall;
if (['dashboard', 'visualization', 'search', 'index-pattern'].includes(type)) {
legacyFindRelationshipsCall = legacyFindRelationships(type, id, size, this);
legacyFindRelationshipsCall = legacyFindRelationships({ type, size, namespace, id, savedObjectsClient: this });
}
const bulkGetOpts = (sourceObject.references || []).map(ref => ({ id: ref.id, type: ref.type }));
const searchTypes = Array.isArray(filterTypes)
Expand Down Expand Up @@ -654,7 +654,7 @@ export class SavedObjectsRepository {

const relationshipObjects = [].concat(
referencedObjects.map(obj => ({ id: obj.id, type: obj.type, ...obj.attributes })),
legacyResponse || [],
(legacyResponse || []).filter(obj => searchTypes.includes(obj.type)),
referencedResponse.hits.hits
.map(hit => this._rawToSavedObject(hit))
.map(obj => ({ id: obj.id, type: obj.type, ...obj.attributes }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"description": "",
"version": 1,
"kibanaSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"91200a00-9efd-11e7-acb3-3dab96693fab\",\"filter\":[],\"query\":{\"query\":\"\",\"language\":\"lucene\"}}"
"searchSourceJSON": "{\"index\":\"space_1-91200a00-9efd-11e7-acb3-3dab96693fab\",\"filter\":[],\"query\":{\"query\":\"\",\"language\":\"lucene\"}}"
}
}
}
Expand All @@ -216,7 +216,7 @@
"title": "Requests",
"hits": 0,
"description": "",
"panelsJSON": "[{\"size_x\":6,\"size_y\":3,\"panelIndex\":1,\"type\":\"visualization\",\"id\":\"dd7caf20-9efd-11e7-acb3-3dab96693fab\",\"col\":1,\"row\":1}]",
"panelsJSON": "[{\"size_x\":6,\"size_y\":3,\"panelIndex\":1,\"type\":\"visualization\",\"id\":\"space_1-dd7caf20-9efd-11e7-acb3-3dab96693fab\",\"col\":1,\"row\":1}]",
"optionsJSON": "{\"darkTheme\":false}",
"uiStateJSON": "{}",
"version": 1,
Expand Down Expand Up @@ -291,7 +291,7 @@
"description": "",
"version": 1,
"kibanaSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"91200a00-9efd-11e7-acb3-3dab96693fab\",\"filter\":[],\"query\":{\"query\":\"\",\"language\":\"lucene\"}}"
"searchSourceJSON": "{\"index\":\"space_2-91200a00-9efd-11e7-acb3-3dab96693fab\",\"filter\":[],\"query\":{\"query\":\"\",\"language\":\"lucene\"}}"
}
}
}
Expand All @@ -312,7 +312,7 @@
"title": "Requests",
"hits": 0,
"description": "",
"panelsJSON": "[{\"size_x\":6,\"size_y\":3,\"panelIndex\":1,\"type\":\"visualization\",\"id\":\"dd7caf20-9efd-11e7-acb3-3dab96693fab\",\"col\":1,\"row\":1}]",
"panelsJSON": "[{\"size_x\":6,\"size_y\":3,\"panelIndex\":1,\"type\":\"visualization\",\"id\":\"space_2-dd7caf20-9efd-11e7-acb3-3dab96693fab\",\"col\":1,\"row\":1}]",
"optionsJSON": "{\"darkTheme\":false}",
"uiStateJSON": "{}",
"version": 1,
Expand Down

0 comments on commit 062b72b

Please sign in to comment.