Skip to content

Commit be5ad00

Browse files
committed
[ML] Migrate server side Mocha tests to Jest. (#65651)
Migrates job validation related server side tests from Mocha to Jest. # Conflicts: # x-pack/plugins/ml/server/models/job_validation/validate_influencers.ts
1 parent e3ff08c commit be5ad00

18 files changed

+298
-223
lines changed
Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import expect from '@kbn/expect';
8-
import { estimateBucketSpanFactory } from '../bucket_span_estimator';
7+
import { APICaller } from 'kibana/server';
8+
9+
import { ES_AGGREGATION } from '../../../common/constants/aggregation_types';
10+
11+
import { estimateBucketSpanFactory, BucketSpanEstimatorData } from './bucket_span_estimator';
912

1013
// Mock callWithRequest with the ability to simulate returning different
1114
// permission settings. On each call using `ml.privilegeCheck` we retrieve
@@ -14,7 +17,7 @@ import { estimateBucketSpanFactory } from '../bucket_span_estimator';
1417
// sufficient permissions should be returned, the second time insufficient
1518
// permissions.
1619
const permissions = [false, true];
17-
const callWithRequest = method => {
20+
const callWithRequest: APICaller = (method: string) => {
1821
return new Promise(resolve => {
1922
if (method === 'ml.privilegeCheck') {
2023
resolve({
@@ -28,34 +31,19 @@ const callWithRequest = method => {
2831
return;
2932
}
3033
resolve({});
31-
});
34+
}) as Promise<any>;
3235
};
3336

34-
const callWithInternalUser = () => {
37+
const callWithInternalUser: APICaller = () => {
3538
return new Promise(resolve => {
3639
resolve({});
37-
});
40+
}) as Promise<any>;
3841
};
3942

40-
// mock xpack_main plugin
41-
function mockXpackMainPluginFactory(isEnabled = false, licenseType = 'platinum') {
42-
return {
43-
info: {
44-
isAvailable: () => true,
45-
feature: () => ({
46-
isEnabled: () => isEnabled,
47-
}),
48-
license: {
49-
getType: () => licenseType,
50-
},
51-
},
52-
};
53-
}
54-
5543
// mock configuration to be passed to the estimator
56-
const formConfig = {
57-
aggTypes: ['count'],
58-
duration: {},
44+
const formConfig: BucketSpanEstimatorData = {
45+
aggTypes: [ES_AGGREGATION.COUNT],
46+
duration: { start: 0, end: 1 },
5947
fields: [null],
6048
index: '',
6149
query: {
@@ -64,58 +52,45 @@ const formConfig = {
6452
must_not: [],
6553
},
6654
},
55+
splitField: undefined,
56+
timeField: undefined,
6757
};
6858

6959
describe('ML - BucketSpanEstimator', () => {
7060
it('call factory', () => {
7161
expect(function() {
72-
estimateBucketSpanFactory(callWithRequest, callWithInternalUser);
73-
}).to.not.throwError('Not initialized.');
62+
estimateBucketSpanFactory(callWithRequest, callWithInternalUser, false);
63+
}).not.toThrow('Not initialized.');
7464
});
7565

7666
it('call factory and estimator with security disabled', done => {
7767
expect(function() {
7868
const estimateBucketSpan = estimateBucketSpanFactory(
7969
callWithRequest,
8070
callWithInternalUser,
81-
mockXpackMainPluginFactory()
71+
true
8272
);
8373

8474
estimateBucketSpan(formConfig).catch(catchData => {
85-
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
75+
expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets');
8676

8777
done();
8878
});
89-
}).to.not.throwError('Not initialized.');
79+
}).not.toThrow('Not initialized.');
9080
});
9181

92-
it('call factory and estimator with security enabled and sufficient permissions.', done => {
82+
it('call factory and estimator with security enabled.', done => {
9383
expect(function() {
9484
const estimateBucketSpan = estimateBucketSpanFactory(
9585
callWithRequest,
9686
callWithInternalUser,
97-
mockXpackMainPluginFactory(true)
87+
false
9888
);
9989
estimateBucketSpan(formConfig).catch(catchData => {
100-
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
90+
expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets');
10191

10292
done();
10393
});
104-
}).to.not.throwError('Not initialized.');
105-
});
106-
107-
it('call factory and estimator with security enabled and insufficient permissions.', done => {
108-
expect(function() {
109-
const estimateBucketSpan = estimateBucketSpanFactory(
110-
callWithRequest,
111-
callWithInternalUser,
112-
mockXpackMainPluginFactory(true)
113-
);
114-
115-
estimateBucketSpan(formConfig).catch(catchData => {
116-
expect(catchData).to.be('Insufficient permissions to call bucket span estimation.');
117-
done();
118-
});
119-
}).to.not.throwError('Not initialized.');
94+
}).not.toThrow('Not initialized.');
12095
});
12196
});

x-pack/plugins/ml/server/models/job_validation/job_validation.d.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66

77
import { APICaller } from 'kibana/server';
88
import { TypeOf } from '@kbn/config-schema';
9+
10+
import { DeepPartial } from '../../../common/types/common';
11+
912
import { validateJobSchema } from '../../routes/schemas/job_validation_schema';
1013

11-
type ValidateJobPayload = TypeOf<typeof validateJobSchema>;
14+
import { ValidationMessage } from './messages';
15+
16+
export type ValidateJobPayload = TypeOf<typeof validateJobSchema>;
1217

1318
export function validateJob(
1419
callAsCurrentUser: APICaller,
15-
payload: ValidateJobPayload,
16-
kbnVersion: string,
17-
callAsInternalUser: APICaller,
18-
isSecurityDisabled: boolean
19-
): string[];
20+
payload?: DeepPartial<ValidateJobPayload>,
21+
kbnVersion?: string,
22+
callAsInternalUser?: APICaller,
23+
isSecurityDisabled?: boolean
24+
): Promise<ValidationMessage[]>;

0 commit comments

Comments
 (0)