Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Onweek] Property-based test for SearchSource serialization #119154

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@
"expose-loader": "^0.7.5",
"faker": "^5.1.0",
"fancy-log": "^1.3.2",
"fast-check": "^2.19.0",
"fast-glob": "2.2.7",
"fetch-mock": "^7.3.9",
"file-loader": "^4.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { Filter, FilterStateStore } from '@kbn/es-query';
import { Fields } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import fc from 'fast-check';
import { of } from 'rxjs';

import { IndexPattern } from 'src/plugins/data/public';
import { IAggConfigs } from '../aggs';
import { Query } from '../..';
import { IndexPatternsContract } from '../..';
import type { SearchSourceDependencies } from './search_source';
import { SearchSource } from './search_source';
import {
EsQuerySearchAfter,
EsQuerySortValue,
SearchFieldValue,
SearchSourceFields,
SortDirection,
SortDirectionFormat,
SortDirectionNumeric,
} from './types';
import { createSearchSource } from './create_search_source';

fc.configureGlobal({
numRuns: 1000,
});

const option = <T>(arb: fc.Arbitrary<T>) => fc.option(arb, { nil: undefined });

function arbitraryQuery(): fc.Arbitrary<Query> {
return fc.record({ query: fc.oneof(fc.string(), fc.object()), language: fc.string() });
}

function arbitraryFilter(): fc.Arbitrary<Filter> {
return fc.record({
$state: option(
fc.record({
store: fc.oneof(
fc.constant(FilterStateStore.APP_STATE),
fc.constant(FilterStateStore.GLOBAL_STATE)
),
})
),
meta: fc.record({
alias: fc.option(fc.string()),
disabled: option(fc.boolean()),
negate: option(fc.boolean()),
controlledBy: option(fc.string()),
group: option(fc.string()),
index: option(fc.string()),
isMultiIndex: option(fc.boolean()),
type: option(fc.string()),
key: option(fc.string()),
params: fc.anything(),
value: option(fc.string()),
}),
query: option(fc.object()),
});
}

function arbitraryDataView(): fc.Arbitrary<IndexPattern> {
return fc.record({
id: fc.string({ minLength: 1 }),
title: fc.string(),
fieldFormatMap: fc.object(),
allowNoIndex: fc.boolean(),
}) as unknown as fc.Arbitrary<IndexPattern>;
}

function arbitraryAggConfigs(): fc.Arbitrary<IAggConfigs> {
return fc.record({
id: fc.string(),
enabled: fc.boolean(),
params: fc.anything(),
brandNew: option(fc.boolean()),
indexPattern: arbitraryDataView(),
}) as unknown as fc.Arbitrary<IAggConfigs>;
}

const sortDirectionsData: Array<fc.Arbitrary<SortDirection>> = [
fc.constant(SortDirection.asc),
fc.constant(SortDirection.desc),
];

const sortNumericTypeData: Array<fc.Arbitrary<SortDirectionNumeric['numeric_type']>> = [
fc.constant('double'),
fc.constant('long'),
fc.constant('date'),
fc.constant('date_nanos'),
];

function arbitrarySortDirectionsNumeric(): fc.Arbitrary<SortDirectionNumeric> {
return fc.record({
order: fc.oneof(...sortDirectionsData),
numeric_type: option(fc.oneof(...sortNumericTypeData)),
});
}

function arbitraryQuerySortValue(): fc.Arbitrary<EsQuerySortValue> {
return fc.object({
values: [
fc.oneof(...sortDirectionsData, arbitraryDirectionFormat(), arbitrarySortDirectionsNumeric()),
],
}) as fc.Arbitrary<EsQuerySortValue>;
}

function arbitraryDirectionFormat(): fc.Arbitrary<SortDirectionFormat> {
return fc.record({
order: fc.oneof(...sortDirectionsData),
format: option(fc.string()),
});
}

function arbitraryFields(): fc.Arbitrary<Fields> {
return fc.oneof(fc.string(), fc.array(fc.string()));
}

const arbitrarySearchField = fc.memo((n) => {
return fc.object({ values: [arbitrarySearchFieldValue(n)] });
});

const arbitrarySearchFieldValue: fc.Memo<SearchFieldValue> = fc.memo((n) =>
n > 1
? (fc.object({ values: [arbitrarySearchField()] }) as fc.Arbitrary<SearchFieldValue>)
: fc.string()
);

function artbitraryEsQuerySearchAfter(): fc.Arbitrary<EsQuerySearchAfter> {
return fc.array(fc.oneof(fc.string(), fc.nat()), {
maxLength: 2,
}) as fc.Arbitrary<EsQuerySearchAfter>;
}

const arbitrarySearchSourceFields: fc.Memo<SearchSourceFields> = fc.memo((n) => {
return fc.record<SearchSourceFields>({
type: option(fc.string()),
query: option(arbitraryQuery()),
filter: option(
fc.oneof(
fc.array(arbitraryFilter()),
arbitraryFilter(),
fc.func(fc.oneof(arbitraryFilter(), fc.array(arbitraryFilter())))
)
),
sort: option(fc.oneof(arbitraryQuerySortValue(), fc.array(arbitraryQuerySortValue()))),
highlight: option(fc.anything()),
highlightAll: option(fc.boolean()),
trackTotalHits: option(fc.oneof(fc.boolean(), fc.nat())),
aggs: option(fc.oneof(fc.object(), fc.func(fc.object()), arbitraryAggConfigs())),
from: option(fc.nat()),
size: option(fc.nat()),
source: option(fc.oneof(fc.boolean(), arbitraryFields())),
version: option(fc.boolean()),
fields: option(fc.array(arbitrarySearchFieldValue(2))),
fieldsFromSource: option(arbitraryFields()),
index: option(arbitraryDataView()),
searchAfter: option(artbitraryEsQuerySearchAfter()),
timeout: option(fc.string()),
terminate_after: option(fc.nat()),
parent: n > 1 ? arbitrarySearchSourceFields(n) : fc.constant(undefined),
});
});

describe('Search source properties', () => {
/**
* NOTES
*
* * It is kind of weird that {@link SearchSourceFields} accepts non-serializable values
* considering that the intention of the source fields is to be passed over the wire...
*/
const getConfigMock = jest
.fn()
.mockImplementation((param) => param === 'metaFields' && ['_type', '_source', '_id'])
.mockName('getConfig');

const mockSearchMethod = jest
.fn()
.mockReturnValue(
of(
{ rawResponse: { test: 1 }, isPartial: true, isRunning: true },
{ rawResponse: { test: 2 }, isPartial: false, isRunning: false }
)
);

const searchSourceDependencies: SearchSourceDependencies = {
getConfig: getConfigMock,
search: mockSearchMethod,
onResponse: (req, res) => res,
};

const stripSize = (fields: SearchSourceFields): SearchSourceFields => {
let current: SearchSourceFields = { ...fields };
while (true) {
delete current.size;
if (!current.parent) break;
else {
current.parent = { ...current.parent };
current = current.parent;
}
}
return current;
};

const prepResult = (searchSourceFields: SearchSourceFields) => {
const { filter } = searchSourceFields;

// We exclude size from serialized result (why?)
const searchSourceFieldsWithNoSize = stripSize(searchSourceFields);

const filterFinal = typeof filter === 'function' ? filter() : filter;

return {
...searchSourceFieldsWithNoSize,
// Always return an array of the filter if there were 1 or more
filter: filterFinal ? (Array.isArray(filterFinal) ? filterFinal : [filterFinal]) : undefined,
};
};

it('should recursively serialize, deserialize to the same source fields with some exceptions', async () => {
await fc.assert(
fc.asyncProperty(
arbitrarySearchSourceFields(1 /* max number of parent search sources */),
async (searchSourceFields) => {
const searchSource = new SearchSource(searchSourceFields, searchSourceDependencies);
const indexPatterns = {
get: jest.fn().mockResolvedValue(searchSourceFields.index),
} as unknown as IndexPatternsContract;
const create = createSearchSource(indexPatterns, searchSourceDependencies);
const serializedFields = searchSource.getSerializedFields(true);

expect((await create(serializedFields)).getFields()).toEqual(
prepResult(searchSourceFields)
);
}
)
);
});
});
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13841,6 +13841,13 @@ fancy-log@^1.3.2:
color-support "^1.1.3"
time-stamp "^1.0.0"

fast-check@^2.19.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/fast-check/-/fast-check-2.19.0.tgz#e87666450e417cd163a564bd546ee763d27c0f19"
integrity sha512-qY4Rc0Nljl2aJx2qgbK3o6wPBjL9QvhKdD/VqJgaKd5ewn8l4ViqgDpUHJq/JkHTBnFKomYYvkFkOYGDVTT8bw==
dependencies:
pure-rand "^5.0.0"

fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3, fast-deep-equal@~3.1.3:
version "3.1.1"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.1.tgz#545145077c501491e33b15ec408c294376e94ae4"
Expand Down Expand Up @@ -22946,6 +22953,11 @@ puppeteer@^5.3.1:
unbzip2-stream "^1.3.3"
ws "^7.2.3"

pure-rand@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/pure-rand/-/pure-rand-5.0.0.tgz#87f5bdabeadbd8904e316913a5c0b8caac517b37"
integrity sha512-lD2/y78q+7HqBx2SaT6OT4UcwtvXNRfEpzYEzl0EQ+9gZq2Qi3fa0HDnYPeqQwhlHJFBUhT7AO3mLU3+8bynHA==

q@^1.1.2, q@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
Expand Down