Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make saved queries share-capable #111462

Merged
merged 11 commits into from
Sep 14, 2021
113 changes: 76 additions & 37 deletions src/plugins/data/public/query/saved_query/saved_query_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const mockSavedObjectsClient = {
create: jest.fn(),
error: jest.fn(),
find: jest.fn(),
get: jest.fn(),
resolve: jest.fn(),
delete: jest.fn(),
};

Expand All @@ -74,7 +74,7 @@ describe('saved query service', () => {
afterEach(() => {
mockSavedObjectsClient.create.mockReset();
mockSavedObjectsClient.find.mockReset();
mockSavedObjectsClient.get.mockReset();
mockSavedObjectsClient.resolve.mockReset();
mockSavedObjectsClient.delete.mockReset();
});

Expand Down Expand Up @@ -267,85 +267,124 @@ describe('saved query service', () => {

describe('getSavedQuery', function () {
it('should retrieve a saved query by id', async () => {
mockSavedObjectsClient.get.mockReturnValue({ id: 'foo', attributes: savedQueryAttributes });
mockSavedObjectsClient.resolve.mockReturnValue({
saved_object: {
id: 'foo',
attributes: savedQueryAttributes,
},
outcome: 'exactMatch',
});

const response = await getSavedQuery('foo');
expect(response).toEqual({ id: 'foo', attributes: savedQueryAttributes });
});
it('should only return saved queries', async () => {
mockSavedObjectsClient.get.mockReturnValue({ id: 'foo', attributes: savedQueryAttributes });
mockSavedObjectsClient.resolve.mockReturnValue({
saved_object: {
id: 'foo',
attributes: savedQueryAttributes,
},
outcome: 'exactMatch',
});

await getSavedQuery('foo');
expect(mockSavedObjectsClient.get).toHaveBeenCalledWith('query', 'foo');
expect(mockSavedObjectsClient.resolve).toHaveBeenCalledWith('query', 'foo');
});

it('should parse a json query', async () => {
mockSavedObjectsClient.get.mockReturnValue({
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: '{"x": "y"}',
mockSavedObjectsClient.resolve.mockReturnValue({
saved_object: {
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: '{"x": "y"}',
},
},
},
outcome: 'exactMatch',
});

const response = await getSavedQuery('food');
expect(response.attributes.query.query).toEqual({ x: 'y' });
});

it('should handle null string', async () => {
mockSavedObjectsClient.get.mockReturnValue({
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: 'null',
mockSavedObjectsClient.resolve.mockReturnValue({
saved_object: {
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: 'null',
},
},
},
outcome: 'exactMatch',
});

const response = await getSavedQuery('food');
expect(response.attributes.query.query).toEqual('null');
});

it('should handle null quoted string', async () => {
mockSavedObjectsClient.get.mockReturnValue({
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: '"null"',
mockSavedObjectsClient.resolve.mockReturnValue({
saved_object: {
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: '"null"',
},
},
},
outcome: 'exactMatch',
});

const response = await getSavedQuery('food');
expect(response.attributes.query.query).toEqual('"null"');
});

it('should not lose quotes', async () => {
mockSavedObjectsClient.get.mockReturnValue({
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: '"Bob"',
mockSavedObjectsClient.resolve.mockReturnValue({
saved_object: {
id: 'food',
attributes: {
title: 'food',
description: 'bar',
query: {
language: 'kuery',
query: '"Bob"',
},
},
},
outcome: 'exactMatch',
});

const response = await getSavedQuery('food');
expect(response.attributes.query.query).toEqual('"Bob"');
});

it('should throw if conflict', async () => {
mockSavedObjectsClient.resolve.mockReturnValue({
saved_object: {
id: 'foo',
attributes: savedQueryAttributes,
},
outcome: 'conflict',
});

const result = getSavedQuery('food');
expect(result).rejects.toMatchInlineSnapshot(
`[Error: Multiple saved queries found with ID: food]`
);
});
});

describe('deleteSavedQuery', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ export const createSavedQueryService = (
};

const getSavedQuery = async (id: string): Promise<SavedQuery> => {
const savedObject = await savedObjectsClient.get<SerializedSavedQueryAttributes>('query', id);
if (savedObject.error) {
const {
saved_object: savedObject,
outcome,
} = await savedObjectsClient.resolve<SerializedSavedQueryAttributes>('query', id);
if (outcome === 'conflict') {
throw new Error(`Multiple saved queries found with ID: ${id} (legacy URL alias conflict)`);
} else if (savedObject.error) {
throw new Error(savedObject.error.message);
}
return parseSavedQueryObject(savedObject);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/server/saved_objects/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { SavedObjectsType } from 'kibana/server';
export const querySavedObjectType: SavedObjectsType = {
name: 'query',
hidden: false,
namespaceType: 'single',
namespaceType: 'multiple-isolated',
convertToMultiNamespaceTypeVersion: '8.0.0',
Comment on lines -14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you accidentally included all of the other changes that are in the other PR, too.

This PR should only need these changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I did this so I don't forget to merge the other PR first before merging this one

management: {
icon: 'search',
defaultSearchField: 'title',
Expand Down