Skip to content

Commit

Permalink
add basic runtime mapping functional tests to analyics
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Apr 5, 2021
1 parent ea03eb1 commit a7f7905
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const COPY_TO_CLIPBOARD_RUNTIME_MAPPINGS = i18n.translate(

const { useXJsonMode } = XJson;
const xJsonMode = new XJsonMode();
export type XJsonModeType = ReturnType<typeof XJsonMode>;

interface Props {
actions: CreateAnalyticsFormProps['actions'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import React, { memo, FC } from 'react';
import { EuiCodeEditor } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { isRuntimeMappings } from '../../../../../../../common/util/runtime_field_utils';
import { XJsonModeType } from './runtime_mappings';

interface Props {
convertToJson: (data: string) => string;
setAdvancedRuntimeMappingsConfig: React.Dispatch<string>;
setIsRuntimeMappingsEditorApplyButtonEnabled: React.Dispatch<React.SetStateAction<boolean>>;
advancedEditorRuntimeMappingsLastApplied: string | undefined;
advancedRuntimeMappingsConfig: string;
xJsonMode: any;
xJsonMode: XJsonModeType;
}

export const RuntimeMappingsEditor: FC<Props> = memo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export default function ({ getService }: FtrProviderContext) {
get destinationIndex(): string {
return `user-${this.jobId}`;
},
runtimeFields: {
uppercase_y: {
type: 'keyword',
script: 'emit(params._source.y.toUpperCase())',
},
},
dependentVariable: 'y',
trainingPercent: 20,
modelMemory: '60mb',
Expand Down Expand Up @@ -92,6 +98,19 @@ export default function ({ getService }: FtrProviderContext) {
await ml.dataFrameAnalyticsCreation.assertJobTypeSelectExists();
await ml.dataFrameAnalyticsCreation.selectJobType(testData.jobType);

await ml.testExecution.logTestStep('displays the runtime mappings editor switch');
await ml.dataFrameAnalyticsCreation.assertRuntimeMappingSwitchExists();

await ml.testExecution.logTestStep('enables the runtime mappings editor');
await ml.dataFrameAnalyticsCreation.toggleRuntimeMappingsEditorSwitch(true);
await ml.dataFrameAnalyticsCreation.assertRuntimeMappingsEditorContent(['']);

await ml.testExecution.logTestStep('sets runtime mappings');
await ml.dataFrameAnalyticsCreation.setRuntimeMappingsEditorContent(
JSON.stringify(testData.runtimeFields)
);
await ml.dataFrameAnalyticsCreation.applyRuntimeMappings();

await ml.testExecution.logTestStep('inputs the dependent variable');
await ml.dataFrameAnalyticsCreation.assertDependentVariableInputExists();
await ml.dataFrameAnalyticsCreation.selectDependentVariable(testData.dependentVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export default function ({ getService }: FtrProviderContext) {
get destinationIndex(): string {
return `user-${this.jobId}`;
},
runtimeFields: {
lowercase_central_air: {
type: 'keyword',
script: 'emit(params._source.CentralAir.toLowerCase())',
},
},
modelMemory: '5mb',
createIndexPattern: true,
expected: {
Expand Down Expand Up @@ -106,6 +112,19 @@ export default function ({ getService }: FtrProviderContext) {
await ml.dataFrameAnalyticsCreation.assertJobTypeSelectExists();
await ml.dataFrameAnalyticsCreation.selectJobType(testData.jobType);

await ml.testExecution.logTestStep('displays the runtime mappings editor switch');
await ml.dataFrameAnalyticsCreation.assertRuntimeMappingSwitchExists();

await ml.testExecution.logTestStep('enables the runtime mappings editor');
await ml.dataFrameAnalyticsCreation.toggleRuntimeMappingsEditorSwitch(true);
await ml.dataFrameAnalyticsCreation.assertRuntimeMappingsEditorContent(['']);

await ml.testExecution.logTestStep('sets runtime mappings');
await ml.dataFrameAnalyticsCreation.setRuntimeMappingsEditorContent(
JSON.stringify(testData.runtimeFields)
);
await ml.dataFrameAnalyticsCreation.applyRuntimeMappings();

await ml.testExecution.logTestStep('does not display the dependent variable input');
await ml.dataFrameAnalyticsCreation.assertDependentVariableInputMissing();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export default function ({ getService }: FtrProviderContext) {
get destinationIndex(): string {
return `user-${this.jobId}`;
},
runtimeFields: {
uppercase_stab: {
type: 'keyword',
script: 'emit(params._source.stabf.toUpperCase())',
},
},
dependentVariable: 'stab',
trainingPercent: 20,
modelMemory: '20mb',
Expand Down Expand Up @@ -85,6 +91,19 @@ export default function ({ getService }: FtrProviderContext) {
await ml.dataFrameAnalyticsCreation.assertJobTypeSelectExists();
await ml.dataFrameAnalyticsCreation.selectJobType(testData.jobType);

await ml.testExecution.logTestStep('displays the runtime mappings editor switch');
await ml.dataFrameAnalyticsCreation.assertRuntimeMappingSwitchExists();

await ml.testExecution.logTestStep('enables the runtime mappings editor');
await ml.dataFrameAnalyticsCreation.toggleRuntimeMappingsEditorSwitch(true);
await ml.dataFrameAnalyticsCreation.assertRuntimeMappingsEditorContent(['']);

await ml.testExecution.logTestStep('sets runtime mappings');
await ml.dataFrameAnalyticsCreation.setRuntimeMappingsEditorContent(
JSON.stringify(testData.runtimeFields)
);
await ml.dataFrameAnalyticsCreation.applyRuntimeMappings();

await ml.testExecution.logTestStep('inputs the dependent variable');
await ml.dataFrameAnalyticsCreation.assertDependentVariableInputExists();
await ml.dataFrameAnalyticsCreation.selectDependentVariable(testData.dependentVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function MachineLearningDataFrameAnalyticsCreationProvider(
const testSubjects = getService('testSubjects');
const comboBox = getService('comboBox');
const retry = getService('retry');
const aceEditor = getService('aceEditor');

return {
async assertJobTypeSelectExists() {
Expand Down Expand Up @@ -236,6 +237,76 @@ export function MachineLearningDataFrameAnalyticsCreationProvider(
);
},

async assertRuntimeMappingSwitchExists() {
await testSubjects.existOrFail('mlDataFrameAnalyticsRuntimeMappingsEditorSwitch');
},

async assertRuntimeMappingEditorExists() {
await testSubjects.existOrFail('mlDataFrameAnalyticsAdvancedRuntimeMappingsEditor');
},

// async enableRuntimeMappingsEditor() {
// await retry.tryForTime(5000, async () => {
// await testSubjects.clickWhenNotDisabled('mlDataFrameAnalyticsRuntimeMappingsEditorSwitch');
// await this.assertRuntimeMappingEditorExists();
// });
// },

async assertRuntimeMappingsEditorSwitchCheckState(expectedCheckState: boolean) {
const actualCheckState = await this.getRuntimeMappingsEditorSwitchCheckedState();
expect(actualCheckState).to.eql(
expectedCheckState,
`Advanced runtime mappings editor switch check state should be '${expectedCheckState}' (got '${actualCheckState}')`
);
},

async getRuntimeMappingsEditorSwitchCheckedState(): Promise<boolean> {
const subj = 'mlDataFrameAnalyticsRuntimeMappingsEditorSwitch';
const isSelected = await testSubjects.getAttribute(subj, 'aria-checked');
return isSelected === 'true';
},

async toggleRuntimeMappingsEditorSwitch(toggle: boolean) {
const subj = 'mlDataFrameAnalyticsRuntimeMappingsEditorSwitch';
if ((await this.getRuntimeMappingsEditorSwitchCheckedState()) !== toggle) {
await retry.tryForTime(5 * 1000, async () => {
await testSubjects.clickWhenNotDisabled(subj);
await this.assertRuntimeMappingsEditorSwitchCheckState(toggle);
});
}
},

async setRuntimeMappingsEditorContent(input: string) {
await aceEditor.setValue('mlDataFrameAnalyticsAdvancedRuntimeMappingsEditor', input);
},

async assertRuntimeMappingsEditorContent(expectedContent: string[]) {
await this.assertRuntimeMappingEditorExists();

const runtimeMappingsEditorString = await aceEditor.getValue(
'mlDataFrameAnalyticsAdvancedRuntimeMappingsEditor'
);
// Not all lines may be visible in the editor and thus aceEditor may not return all lines.
// This means we might not get back valid JSON so we only test against the first few lines
// and see if the string matches.
const splicedAdvancedEditorValue = runtimeMappingsEditorString.split('\n').splice(0, 3);
expect(splicedAdvancedEditorValue).to.eql(
expectedContent,
`Expected the first editor lines to be '${expectedContent}' (got '${splicedAdvancedEditorValue}')`
);
},

async applyRuntimeMappings() {
const subj = 'mlDataFrameAnalyticsRuntimeMappingsApplyButton';
await testSubjects.existOrFail(subj);
await testSubjects.clickWhenNotDisabled(subj);
const isEnabled = await testSubjects.isEnabled(subj);
expect(isEnabled).to.eql(
false,
`Expected runtime mappings 'Apply changes' button to be disabled, got enabled.`
);
},

async assertDependentVariableSelection(expectedSelection: string[]) {
await this.waitForDependentVariableInputLoaded();
const actualSelection = await comboBox.getComboBoxSelectedOptions(
Expand Down

0 comments on commit a7f7905

Please sign in to comment.