Skip to content

Commit

Permalink
[ML] Change allEventsExistInCalendar to be in service with debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jul 22, 2020
1 parent 2a5eb37 commit 27bfb01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import expect from '@kbn/expect';
import { allEventsExistInCalendar } from './helpers';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { USER } from '../../../../functional/services/ml/security_common';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common';
Expand Down Expand Up @@ -55,7 +54,7 @@ export default ({ getService }: FtrProviderContext) => {

expect(body).to.have.length(testCalendars.length);
expect(body[0].events).to.have.length(testEvents.length);
expect(allEventsExistInCalendar(testEvents, body[0])).to.eql(true);
ml.api.assertAllEventsExistInCalendar(testEvents, body[0]);
});

it('should fetch all calendars for user with view permission', async () => {
Expand All @@ -67,7 +66,7 @@ export default ({ getService }: FtrProviderContext) => {

expect(body).to.have.length(testCalendars.length);
expect(body[0].events).to.have.length(testEvents.length);
expect(allEventsExistInCalendar(testEvents, body[0])).to.eql(true);
ml.api.assertAllEventsExistInCalendar(testEvents, body[0]);
});

it('should not fetch calendars for unauthorized user', async () => {
Expand Down Expand Up @@ -107,7 +106,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(body.job_ids).to.eql(testCalendar.job_ids);
expect(body.description).to.eql(testCalendar.description);
expect(body.events).to.have.length(testEvents.length);
expect(allEventsExistInCalendar(testEvents, body)).to.eql(true);
ml.api.assertAllEventsExistInCalendar(testEvents, body);
});

it('should fetch calendar & associated events by id for user with view permission', async () => {
Expand All @@ -120,7 +119,7 @@ export default ({ getService }: FtrProviderContext) => {
expect(body.job_ids).to.eql(testCalendar.job_ids);
expect(body.description).to.eql(testCalendar.description);
expect(body.events).to.have.length(testEvents.length);
expect(allEventsExistInCalendar(testEvents, body)).to.eql(true);
ml.api.assertAllEventsExistInCalendar(testEvents, body);
});

it('should not fetch calendars for unauthorized user', async () => {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/api_integration/apis/ml/calendars/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Calendar, CalendarEvent } from '../../../../../plugins/ml/server/models/calendar';

export const allEventsExistInCalendar = (
export const assertAllEventsExistInCalendar = (
eventsToCheck: CalendarEvent[],
calendar: Calendar
): boolean => {
Expand Down
33 changes: 31 additions & 2 deletions x-pack/test/functional/services/ml/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { FtrProviderContext } from '../../ftr_provider_context';
import { DATAFEED_STATE, JOB_STATE } from '../../../../plugins/ml/common/constants/states';
import { DATA_FRAME_TASK_STATE } from '../../../../plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/common';
import { Datafeed, Job } from '../../../../plugins/ml/common/types/anomaly_detection_jobs';
import { allEventsExistInCalendar } from '../../../api_integration/apis/ml/calendars/helpers';
export type MlApi = ProvidedType<typeof MachineLearningAPIProvider>;

export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
Expand Down Expand Up @@ -374,6 +373,36 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
return await esSupertest.get(`/_ml/calendars/${calendarId}/events`).expect(expectedCode);
},

assertAllEventsExistInCalendar: (
eventsToCheck: CalendarEvent[],
calendar: Calendar
): boolean => {
const updatedCalendarEvents = calendar.events as CalendarEvent[];
let allEventsAreUpdated = true;
for (const eventToCheck of eventsToCheck) {
// if at least one of the events that we need to check is not in the updated events
// no need to continue
if (
updatedCalendarEvents.findIndex(
(updatedEvent) =>
updatedEvent.description === eventToCheck.description &&
updatedEvent.start_time === eventToCheck.start_time &&
updatedEvent.end_time === eventToCheck.end_time
) < 0
) {
allEventsAreUpdated = false;
break;
}
}
expect(allEventsAreUpdated).to.eql(
true,
`Expected calendar ${calendar.calendar_id} to contain events ${JSON.stringify(
eventsToCheck
)}`
);
return true;
},

async waitForEventsToExistInCalendar(
calendarId: string,
eventsToCheck: CalendarEvent[],
Expand All @@ -383,7 +412,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
// validate if calendar events have been updated with the requested events
const { body } = await this.getCalendarEvents(calendarId, 200);

if (allEventsExistInCalendar(eventsToCheck, body)) {
if (this.assertAllEventsExistInCalendar(eventsToCheck, body)) {
return true;
} else {
throw new Error(
Expand Down

0 comments on commit 27bfb01

Please sign in to comment.