Skip to content

Commit

Permalink
[ML] Migrate validate_time test from mocha to jest.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed May 7, 2020
1 parent c05a39d commit 4c2d970
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
*/

import _ from 'lodash';
import expect from '@kbn/expect';
import { isValidTimeField, validateTimeRange } from '../validate_time_range';

import mockTimeField from './mock_time_field';
import mockTimeFieldNested from './mock_time_field_nested';
import mockTimeRange from './mock_time_range';
import { APICaller } from 'kibana/server';

import { CombinedJob } from '../../../common/types/anomaly_detection_jobs';

import { isValidTimeField, validateTimeRange } from './validate_time_range';

import mockTimeField from './__mocks__/mock_time_field.json';
import mockTimeFieldNested from './__mocks__/mock_time_field_nested.json';
import mockTimeRange from './__mocks__/mock_time_range.json';

const mockSearchResponse = {
fieldCaps: mockTimeField,
search: mockTimeRange,
};

const callWithRequestFactory = resp => {
return path => {
const callWithRequestFactory = (resp: any): APICaller => {
return (path: string) => {
return new Promise(resolve => {
resolve(resp[path]);
});
}) as Promise<any>;
};
};

function getMinimalValidJob() {
return {
return ({
analysis_config: {
bucket_span: '15m',
detectors: [],
Expand All @@ -36,12 +40,15 @@ function getMinimalValidJob() {
datafeed_config: {
indices: [],
},
};
} as unknown) as CombinedJob;
}

describe('ML - isValidTimeField', () => {
it('called without job config argument triggers Promise rejection', done => {
isValidTimeField(callWithRequestFactory(mockSearchResponse)).then(
isValidTimeField(
callWithRequestFactory(mockSearchResponse),
(undefined as unknown) as CombinedJob
).then(
() => done(new Error('Promise should not resolve for this test without job argument.')),
() => done()
);
Expand All @@ -50,7 +57,7 @@ describe('ML - isValidTimeField', () => {
it('time_field `@timestamp`', done => {
isValidTimeField(callWithRequestFactory(mockSearchResponse), getMinimalValidJob()).then(
valid => {
expect(valid).to.be(true);
expect(valid).toBe(true);
done();
},
() => done(new Error('isValidTimeField Promise failed for time_field `@timestamp`.'))
Expand All @@ -71,7 +78,7 @@ describe('ML - isValidTimeField', () => {
mockJobConfigNestedDate
).then(
valid => {
expect(valid).to.be(true);
expect(valid).toBe(true);
done();
},
() => done(new Error('isValidTimeField Promise failed for time_field `metadata.timestamp`.'))
Expand All @@ -81,38 +88,52 @@ describe('ML - isValidTimeField', () => {

describe('ML - validateTimeRange', () => {
it('called without arguments', done => {
validateTimeRange(callWithRequestFactory(mockSearchResponse)).then(
validateTimeRange(
callWithRequestFactory(mockSearchResponse),
(undefined as unknown) as CombinedJob
).then(
() => done(new Error('Promise should not resolve for this test without job argument.')),
() => done()
);
});

it('called with non-valid job argument #2, missing datafeed_config', done => {
validateTimeRange(callWithRequestFactory(mockSearchResponse), { analysis_config: {} }).then(
validateTimeRange(callWithRequestFactory(mockSearchResponse), ({
analysis_config: {},
} as unknown) as CombinedJob).then(
() => done(new Error('Promise should not resolve for this test without valid job argument.')),
() => done()
);
});

it('called with non-valid job argument #3, missing datafeed_config.indices', done => {
const job = { analysis_config: {}, datafeed_config: {} };
validateTimeRange(callWithRequestFactory(mockSearchResponse), job).then(
validateTimeRange(
callWithRequestFactory(mockSearchResponse),
(job as unknown) as CombinedJob
).then(
() => done(new Error('Promise should not resolve for this test without valid job argument.')),
() => done()
);
});

it('called with non-valid job argument #4, missing data_description', done => {
const job = { analysis_config: {}, datafeed_config: { indices: [] } };
validateTimeRange(callWithRequestFactory(mockSearchResponse), job).then(
validateTimeRange(
callWithRequestFactory(mockSearchResponse),
(job as unknown) as CombinedJob
).then(
() => done(new Error('Promise should not resolve for this test without valid job argument.')),
() => done()
);
});

it('called with non-valid job argument #5, missing data_description.time_field', done => {
const job = { analysis_config: {}, data_description: {}, datafeed_config: { indices: [] } };
validateTimeRange(callWithRequestFactory(mockSearchResponse), job).then(
validateTimeRange(
callWithRequestFactory(mockSearchResponse),
(job as unknown) as CombinedJob
).then(
() => done(new Error('Promise should not resolve for this test without valid job argument.')),
() => done()
);
Expand All @@ -128,7 +149,7 @@ describe('ML - validateTimeRange', () => {
duration
).then(messages => {
const ids = messages.map(m => m.id);
expect(ids).to.eql(['time_field_invalid']);
expect(ids).toStrictEqual(['time_field_invalid']);
});
});

Expand All @@ -142,7 +163,7 @@ describe('ML - validateTimeRange', () => {
duration
).then(messages => {
const ids = messages.map(m => m.id);
expect(ids).to.eql(['time_range_short']);
expect(ids).toStrictEqual(['time_range_short']);
});
});

Expand All @@ -154,7 +175,7 @@ describe('ML - validateTimeRange', () => {
duration
).then(messages => {
const ids = messages.map(m => m.id);
expect(ids).to.eql(['time_range_short']);
expect(ids).toStrictEqual(['time_range_short']);
});
});

Expand All @@ -166,7 +187,7 @@ describe('ML - validateTimeRange', () => {
duration
).then(messages => {
const ids = messages.map(m => m.id);
expect(ids).to.eql(['time_range_short']);
expect(ids).toStrictEqual(['time_range_short']);
});
});

Expand All @@ -178,7 +199,7 @@ describe('ML - validateTimeRange', () => {
duration
).then(messages => {
const ids = messages.map(m => m.id);
expect(ids).to.eql(['success_time_range']);
expect(ids).toStrictEqual(['success_time_range']);
});
});

Expand All @@ -190,7 +211,7 @@ describe('ML - validateTimeRange', () => {
duration
).then(messages => {
const ids = messages.map(m => m.id);
expect(ids).to.eql(['time_range_before_epoch']);
expect(ids).toStrictEqual(['time_range_before_epoch']);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ export async function isValidTimeField(callAsCurrentUser: APICaller, job: Combin
fields: [timeField],
});

let fieldType = fieldCaps.fields[timeField]?.date?.type;
let fieldType = fieldCaps?.fields[timeField]?.date?.type;
if (fieldType === undefined) {
fieldType = fieldCaps.fields[timeField]?.date_nanos?.type;
fieldType = fieldCaps?.fields[timeField]?.date_nanos?.type;
}
return fieldType === ES_FIELD_TYPES.DATE || fieldType === ES_FIELD_TYPES.DATE_NANOS;
}

export async function validateTimeRange(
callAsCurrentUser: APICaller,
job: CombinedJob,
timeRange: TimeRange | undefined
timeRange?: TimeRange
) {
const messages: ValidateTimeRangeMessage[] = [];

Expand Down

0 comments on commit 4c2d970

Please sign in to comment.