grafana/public/app/features/transformers/extractFields/fieldExtractor.test.ts
Andreas Christou d0b41f882e
Transformations: Support escaped characters in key-value pair parsing (#47901)
* Additional logic to handle quoted values

* Simple test with quoted values that include spaces

* Update test description

* Updating logic to account for nested quotes

- Adding additional test for nested quotes

* Strip out line breaks and carriage returns pre-processing

* Fix typo in test result

* Update key-value logic to avoid regexp

- Minor changes to account for null values

* Correct escaping on test

* Additional tests

- Test for null values
- Test for nested separator characters
- Update quoting
2022-04-20 15:46:54 +01:00

114 lines
3.0 KiB
TypeScript

import { fieldExtractors, FieldExtractorID } from './fieldExtractors';
describe('Extract fields from text', () => {
it('JSON extractor', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.JSON);
const out = extractor.parse('{"a":"148.1672","av":41923755,"c":148.25}');
expect(out).toMatchInlineSnapshot(`
Object {
"a": "148.1672",
"av": 41923755,
"c": 148.25,
}
`);
});
it('Test key-values with single/double quotes', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
const out = extractor.parse('a="1", "b"=\'2\',c=3 x:y ;\r\nz="d and 4"');
expect(out).toMatchInlineSnapshot(`
Object {
"a": "1",
"b": "2",
"c": "3",
"x": "y",
"z": "d and 4",
}
`);
});
it('Test key-values with nested single/double quotes', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
const out = extractor.parse(
`a="1", "b"=\'2\',c=3 x:y ;\r\nz="dbl_quotes=\\"Double Quotes\\" sgl_quotes='Single Quotes'"`
);
expect(out).toMatchInlineSnapshot(`
Object {
"a": "1",
"b": "2",
"c": "3",
"x": "y",
"z": "dbl_quotes=\\"Double Quotes\\" sgl_quotes='Single Quotes'",
}
`);
});
it('Test key-values with nested separator characters', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
const out = extractor.parse(`a="1", "b"=\'2\',c=3 x:y ;\r\nz="This is; testing& validating, 1=:2"`);
expect(out).toMatchInlineSnapshot(`
Object {
"a": "1",
"b": "2",
"c": "3",
"x": "y",
"z": "This is; testing& validating, 1=:2",
}
`);
});
it('Test key-values where some values are null', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
const out = extractor.parse(`a=, "b"=\'2\',c=3 x: `);
expect(out).toMatchInlineSnapshot(`
Object {
"a": "",
"b": "2",
"c": "3",
"x": "",
}
`);
});
it('Split key+values', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
const out = extractor.parse('a="1", "b"=\'2\',c=3 x:y ;\r\nz="7"');
expect(out).toMatchInlineSnapshot(`
Object {
"a": "1",
"b": "2",
"c": "3",
"x": "y",
"z": "7",
}
`);
});
it('Split URL style parameters', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
const out = extractor.parse('a=b&c=d&x=123');
expect(out).toMatchInlineSnapshot(`
Object {
"a": "b",
"c": "d",
"x": "123",
}
`);
});
it('Prometheus labels style (not really supported)', async () => {
const extractor = fieldExtractors.get(FieldExtractorID.KeyValues);
const out = extractor.parse('{foo="bar", baz="42"}');
expect(out).toMatchInlineSnapshot(`
Object {
"baz": "42",
"foo": "bar",
}
`);
});
});