Skip to content

Commit d76559d

Browse files
committed
Add tests ensuring metric_query integrity
1 parent 9d654f7 commit d76559d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { MetricExpressionParams } from '../types';
7+
import { getElasticsearchMetricQuery } from './metric_query';
8+
9+
describe("The Metric Threshold Alert's getElasticsearchMetricQuery", () => {
10+
const expressionParams = {
11+
metric: 'system.is.a.good.puppy.dog',
12+
aggType: 'avg',
13+
timeUnit: 'm',
14+
timeSize: 1,
15+
} as MetricExpressionParams;
16+
17+
const timefield = '@timestamp';
18+
const groupBy = 'host.doggoname';
19+
20+
describe('when passed no filterQuery', () => {
21+
const searchBody = getElasticsearchMetricQuery(expressionParams, timefield, groupBy);
22+
test('includes a range filter', () => {
23+
expect(
24+
searchBody.query.bool.filter.find((filter) => filter.hasOwnProperty('range'))
25+
).toBeTruthy();
26+
});
27+
28+
test('includes a metric field filter', () => {
29+
expect(searchBody.query.bool.filter).toMatchObject(
30+
expect.arrayContaining([{ exists: { field: 'system.is.a.good.puppy.dog' } }])
31+
);
32+
});
33+
});
34+
35+
describe('when passed a filterQuery', () => {
36+
const filterQuery =
37+
// This is adapted from a real-world query that previously broke alerts
38+
// We want to make sure it doesn't override any existing filters
39+
'{"bool":{"filter":[{"bool":{"filter":[{"bool":{"must_not":[{"bool":{"should":[{"query_string":{"query":"bark*","fields":["host.name^1.0"],"type":"best_fields","default_operator":"or","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1}}],"adjust_pure_negative":true,"minimum_should_match":"1","boost":1}}],"adjust_pure_negative":true,"boost":1}},{"bool":{"must_not":[{"bool":{"should":[{"query_string":{"query":"woof*","fields":["host.name^1.0"],"type":"best_fields","default_operator":"or","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1}}],"adjust_pure_negative":true,"minimum_should_match":"1","boost":1}}],"adjust_pure_negative":true,"boost":1}}],"adjust_pure_negative":true,"boost":1}}],"adjust_pure_negative":true,"boost":1}}';
40+
41+
const searchBody = getElasticsearchMetricQuery(
42+
expressionParams,
43+
timefield,
44+
groupBy,
45+
filterQuery
46+
);
47+
test('includes a range filter', () => {
48+
expect(
49+
searchBody.query.bool.filter.find((filter) => filter.hasOwnProperty('range'))
50+
).toBeTruthy();
51+
});
52+
53+
test('includes a metric field filter', () => {
54+
expect(searchBody.query.bool.filter).toMatchObject(
55+
expect.arrayContaining([{ exists: { field: 'system.is.a.good.puppy.dog' } }])
56+
);
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)