Skip to content

Commit

Permalink
add unit tests for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed Jun 17, 2021
1 parent 63b0710 commit 3c53b05
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ export async function rollDailyData(
return;
}
try {
const { saved_objects: savedObjects } = await savedObjectsClient.find<EventLoopDelaysDaily>({
type: SAVED_OBJECTS_DAILY_TYPE,
filter: `${SAVED_OBJECTS_DAILY_TYPE}.attributes.timestamp < "now-3d/d"`,
});

const settledDeletes = await deleteHistogramSavedObjects(savedObjects, savedObjectsClient);
const settledDeletes = await deleteHistogramSavedObjects(savedObjectsClient);
const failedDeletes = settledDeletes.filter(({ status }) => status !== 'fulfilled');
if (failedDeletes.length) {
throw failedDeletes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
* Side Public License, v 1.
*/

import { storeHistogram, serializeSavedObjectId } from './saved_objects';
import {
storeHistogram,
serializeSavedObjectId,
deleteHistogramSavedObjects,
} from './saved_objects';
import { savedObjectsRepositoryMock } from '../../../../../core/server/mocks';
import type { SavedObjectsFindResponse } from '../../../../../core/server/';
import { mocked } from './event_loop_delays.mocks';
import moment from 'moment';

Expand Down Expand Up @@ -41,3 +46,78 @@ describe('storeHistogram', () => {
);
});
});

describe('deleteHistogramSavedObjects', () => {
const mockInternalRepository = savedObjectsRepositoryMock.create();

beforeEach(() => {
jest.clearAllMocks();
mockInternalRepository.find.mockResolvedValue({
saved_objects: [{ id: 'test_obj_1' }, { id: 'test_obj_1' }],
} as SavedObjectsFindResponse);
});

it('builds filter query based on time range passed in days', async () => {
await deleteHistogramSavedObjects(mockInternalRepository);
await deleteHistogramSavedObjects(mockInternalRepository, 20);
expect(mockInternalRepository.find.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
"filter": "event_loop_delays_daily.attributes.timestamp < \\"now-3d/d\\"",
"type": "event_loop_delays_daily",
},
],
Array [
Object {
"filter": "event_loop_delays_daily.attributes.timestamp < \\"now-20d/d\\"",
"type": "event_loop_delays_daily",
},
],
]
`);
});

it('loops over saved objects and deletes them', async () => {
mockInternalRepository.delete.mockImplementation(async (type, id) => {
return id;
});

const results = await deleteHistogramSavedObjects(mockInternalRepository);
expect(results).toMatchInlineSnapshot(`
Array [
Object {
"status": "fulfilled",
"value": "test_obj_1",
},
Object {
"status": "fulfilled",
"value": "test_obj_1",
},
]
`);
});

it('settles all promises even if some of the deletes fail.', async () => {
mockInternalRepository.delete.mockImplementationOnce(async (type, id) => {
throw new Error('Intentional failure');
});
mockInternalRepository.delete.mockImplementationOnce(async (type, id) => {
return id;
});

const results = await deleteHistogramSavedObjects(mockInternalRepository);
expect(results).toMatchInlineSnapshot(`
Array [
Object {
"reason": [Error: Intentional failure],
"status": "rejected",
},
Object {
"status": "fulfilled",
"value": "test_obj_1",
},
]
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ export function serializeSavedObjectId({ date, pid }: { date: moment.MomentInput
}

export async function deleteHistogramSavedObjects(
savedObjects: Array<SavedObject<EventLoopDelaysDaily>>,
internalRepository: ISavedObjectsRepository
internalRepository: ISavedObjectsRepository,
daysTimeRange = 3
) {
const { saved_objects: savedObjects } = await internalRepository.find<EventLoopDelaysDaily>({
type: SAVED_OBJECTS_DAILY_TYPE,
filter: `${SAVED_OBJECTS_DAILY_TYPE}.attributes.timestamp < "now-${daysTimeRange}d/d"`,
});

return await Promise.allSettled(
savedObjects.map(async (savedObject) => {
return await internalRepository.delete(SAVED_OBJECTS_DAILY_TYPE, savedObject.id);
Expand Down

0 comments on commit 3c53b05

Please sign in to comment.