[Ingest pipelines] fix default paramenter for ignore_missing in csv processor (#100316) (#100396)

* fix: set ignoreMissingField to true by default
* Add missing serializer to trim fieldConfig

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Ignacio Rivas 2021-05-20 16:51:59 +02:00 committed by GitHub
parent 186fdf9733
commit f6b686490d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 209 additions and 6 deletions

View file

@ -0,0 +1,172 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { act } from 'react-dom/test-utils';
import { setup, SetupResult, getProcessorValue } from './processor.helpers';
// Default parameter values automatically added to the CSV processor when saved
const defaultCSVParameters = {
description: undefined,
if: undefined,
ignore_missing: undefined,
ignore_failure: undefined,
empty_value: undefined,
quote: undefined,
separator: undefined,
tag: undefined,
trim: undefined,
};
const CSV_TYPE = 'csv';
describe('Processor: CSV', () => {
let onUpdate: jest.Mock;
let testBed: SetupResult;
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
beforeEach(async () => {
onUpdate = jest.fn();
await act(async () => {
testBed = await setup({
value: {
processors: [],
},
onFlyoutOpen: jest.fn(),
onUpdate,
});
});
testBed.component.update();
// Open flyout to add new processor
testBed.actions.addProcessor();
// Add type (the other fields are not visible until a type is selected)
await testBed.actions.addProcessorType(CSV_TYPE);
});
test('prevents form submission if required fields are not provided', async () => {
const {
actions: { saveNewProcessor },
form,
} = testBed;
// Click submit button with only the type defined
await saveNewProcessor();
// Expect form error as "field" and "target_field" are required parameters
expect(form.getErrorsMessages()).toEqual([
'A field value is required.',
'A target fields value is required.',
]);
});
test('saves with default parameter values', async () => {
const {
actions: { saveNewProcessor },
form,
find,
component,
} = testBed;
// Add "field" value (required)
form.setInputValue('fieldNameField.input', 'field_1');
// Add "target_field" value (required)
await act(async () => {
find('targetFieldsField.input').simulate('change', [{ label: 'a_value' }]);
});
component.update();
// Save the field
await saveNewProcessor();
const processors = getProcessorValue(onUpdate, CSV_TYPE);
expect(processors[0][CSV_TYPE]).toEqual({
...defaultCSVParameters,
field: 'field_1',
target_fields: ['a_value'],
});
});
test('should send ignore_missing:false when the toggle is disabled', async () => {
const {
actions: { saveNewProcessor },
form,
find,
component,
} = testBed;
// Add "field" value (required)
form.setInputValue('fieldNameField.input', 'field_1');
// Add "target_field" value (required)
await act(async () => {
find('targetFieldsField.input').simulate('change', [{ label: 'a_value' }]);
});
component.update();
// Disable ignore missing toggle
form.toggleEuiSwitch('ignoreMissingSwitch.input');
// Save the field with new changes
await saveNewProcessor();
const processors = getProcessorValue(onUpdate, CSV_TYPE);
expect(processors[0][CSV_TYPE]).toEqual({
...defaultCSVParameters,
field: 'field_1',
target_fields: ['a_value'],
ignore_missing: false,
});
});
test('allows optional parameters to be set', async () => {
const {
actions: { saveNewProcessor },
form,
find,
component,
} = testBed;
// Add "field" value (required)
form.setInputValue('fieldNameField.input', 'field_1');
// Add "target_field" value (required)
await act(async () => {
find('targetFieldsField.input').simulate('change', [{ label: 'a_value' }]);
});
component.update();
// Set optional parameters
form.toggleEuiSwitch('trimSwitch.input');
form.toggleEuiSwitch('ignoreFailureSwitch.input');
form.toggleEuiSwitch('ignoreMissingSwitch.input');
form.setInputValue('quoteValueField.input', '"');
form.setInputValue('emptyValueField.input', ' ');
form.setInputValue('separatorValueField.input', ',');
// Save the field with new changes
await saveNewProcessor();
const processors = getProcessorValue(onUpdate, CSV_TYPE);
expect(processors[0][CSV_TYPE]).toEqual({
...defaultCSVParameters,
field: 'field_1',
target_fields: ['a_value'],
trim: true,
ignore_failure: true,
ignore_missing: false,
separator: ',',
quote: '"',
empty_value: ' ',
});
});
});

View file

@ -148,4 +148,9 @@ type TestSubject =
| 'ifField.textarea'
| 'targetField.input'
| 'keepOriginalField.input'
| 'removeIfSuccessfulField.input';
| 'removeIfSuccessfulField.input'
| 'targetFieldsField.input'
| 'separatorValueField.input'
| 'quoteValueField.input'
| 'emptyValueField.input'
| 'trimSwitch.input';

View file

@ -18,6 +18,7 @@ import {
ToggleField,
ComboBoxField,
ValidationFunc,
SerializerFunc,
} from '../../../../../../shared_imports';
import { FieldsConfig } from './shared';
@ -112,6 +113,7 @@ const fieldsConfig: FieldsConfig = {
type: FIELD_TYPES.TOGGLE,
defaultValue: false,
deserializer: to.booleanOrUndef,
serializer: from.undefinedIfValue(false),
label: i18n.translate('xpack.ingestPipelines.pipelineEditor.csvForm.trimFieldLabel', {
defaultMessage: 'Trim',
}),
@ -148,17 +150,41 @@ export const CSV: FunctionComponent = () => {
config={fieldsConfig.target_fields}
component={ComboBoxField}
path="fields.target_fields"
data-test-subj="targetFieldsField"
/>
<UseField config={fieldsConfig.separator} component={Field} path="fields.separator" />
<UseField
config={fieldsConfig.separator}
component={Field}
path="fields.separator"
data-test-subj="separatorValueField"
/>
<UseField config={fieldsConfig.quote} component={Field} path="fields.quote" />
<UseField
config={fieldsConfig.quote}
component={Field}
path="fields.quote"
data-test-subj="quoteValueField"
/>
<UseField config={fieldsConfig.trim} component={ToggleField} path="fields.trim" />
<UseField
config={fieldsConfig.trim}
component={ToggleField}
path="fields.trim"
data-test-subj="trimSwitch"
/>
<UseField config={fieldsConfig.empty_value} component={Field} path="fields.empty_value" />
<UseField
config={fieldsConfig.empty_value}
component={Field}
path="fields.empty_value"
data-test-subj="emptyValueField"
/>
<IgnoreMissingField />
<IgnoreMissingField
defaultValue={true}
serializer={from.undefinedIfValue(true) as SerializerFunc<boolean>}
/>
</>
);
};