Skip to content

Commit

Permalink
Add unit tests for terms expression function.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Apr 14, 2020
1 parent b9d7ea8 commit b028380
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
144 changes: 144 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/terms_fn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { functionWrapper } from '../test_helpers';
import { aggTypeTerms } from './terms_fn';

describe('agg_expression_functions', () => {
describe('aggTypeTerms', () => {
const fn = functionWrapper(aggTypeTerms());

it('fills in defaults when only required args are provided', () => {
const actual = fn({
field: 'machine.os.keyword',
order: 'asc',
orderBy: '1',
});
expect(actual).toMatchInlineSnapshot(`
Object {
"type": "agg_type",
"value": Object {
"enabled": true,
"id": undefined,
"params": Object {
"exclude": undefined,
"field": "machine.os.keyword",
"include": undefined,
"missingBucket": false,
"missingBucketLabel": "Missing",
"order": "asc",
"orderAgg": undefined,
"orderBy": "1",
"otherBucket": false,
"otherBucketLabel": "Other",
"size": 5,
},
"schema": undefined,
"type": "terms",
},
}
`);
});

it('includes optional params when they are provided', () => {
const actual = fn({
id: '1',
enabled: false,
schema: 'whatever',
field: 'machine.os.keyword',
order: 'desc',
orderBy: '2',
size: 6,
missingBucket: true,
missingBucketLabel: 'missing',
otherBucket: true,
otherBucketLabel: 'other',
exclude: 'ios',
});

expect(actual.value).toMatchInlineSnapshot(`
Object {
"enabled": false,
"id": "1",
"params": Object {
"exclude": "ios",
"field": "machine.os.keyword",
"include": undefined,
"missingBucket": true,
"missingBucketLabel": "missing",
"order": "desc",
"orderAgg": undefined,
"orderBy": "2",
"otherBucket": true,
"otherBucketLabel": "other",
"size": 6,
},
"schema": "whatever",
"type": "terms",
}
`);
});

it('handles orderAgg as a subexpression', () => {
const actual = fn({
field: 'machine.os.keyword',
order: 'asc',
orderBy: '1',
orderAgg: fn({ field: 'name', order: 'asc', orderBy: '1' }),
});

expect(actual.value.params).toMatchInlineSnapshot(`
Object {
"exclude": undefined,
"field": "machine.os.keyword",
"include": undefined,
"missingBucket": false,
"missingBucketLabel": "Missing",
"order": "asc",
"orderAgg": Object {
"type": "agg_type",
"value": Object {
"enabled": true,
"id": undefined,
"params": Object {
"exclude": undefined,
"field": "name",
"include": undefined,
"missingBucket": false,
"missingBucketLabel": "Missing",
"order": "asc",
"orderAgg": undefined,
"orderBy": "1",
"otherBucket": false,
"otherBucketLabel": "Other",
"size": 5,
},
"schema": undefined,
"type": "terms",
},
},
"orderBy": "1",
"otherBucket": false,
"otherBucketLabel": "Other",
"size": 5,
}
`);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { mapValues } from 'lodash';
import {
AnyExpressionFunctionDefinition,
ExpressionFunctionDefinition,
ExecutionContext,
} from '../../../../../../plugins/expressions/public';

/**
* Takes a function spec and passes in default args,
* overriding with any provided args.
*
* Similar to the test helper used in Expressions & Canvas,
* however in this case we are ignoring the input & execution
* context, as they are not applicable to the agg type
* expression functions.
*/
export const functionWrapper = <T extends AnyExpressionFunctionDefinition>(spec: T) => {
const defaultArgs = mapValues(spec.args, argSpec => argSpec.default);
return (
args: T extends ExpressionFunctionDefinition<
infer Name,
infer Input,
infer Arguments,
infer Output,
infer Context
>
? Arguments
: never
) => spec.fn(null, { ...defaultArgs, ...args }, {} as ExecutionContext);
};
1 change: 1 addition & 0 deletions src/plugins/data/public/search/aggs/test_helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
* under the License.
*/

export { functionWrapper } from './function_wrapper';
export { mockAggTypesRegistry } from './mock_agg_types_registry';
export { mockDataServices } from './mock_data_services';

0 comments on commit b028380

Please sign in to comment.