Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { DatabaseAdapter } from '../../database';
import exampleFilter from './example_filter.json';
import monitorState from './monitor_states_docs.json';
import { ElasticsearchMonitorStatesAdapter } from '../elasticsearch_monitor_states_adapter';
import { get, set } from 'lodash';
import { assertCloseTo } from '../../../helper';

describe('ElasticsearchMonitorStatesAdapter', () => {
let database: DatabaseAdapter;
Expand Down Expand Up @@ -37,6 +39,7 @@ describe('ElasticsearchMonitorStatesAdapter', () => {
});

it('applies an appropriate filter section to the query based on filters', async () => {
expect.assertions(3);
const adapter = new ElasticsearchMonitorStatesAdapter(database);
await adapter.legacyGetMonitorStates(
{},
Expand All @@ -45,6 +48,28 @@ describe('ElasticsearchMonitorStatesAdapter', () => {
JSON.stringify(exampleFilter),
'down'
);
expect(searchMock).toHaveBeenCalledTimes(3);
const fixedInterval = parseInt(
get(
searchMock.mock.calls[2][1],
'body.aggs.by_id.aggs.histogram.date_histogram.fixed_interval',
''
).split('ms')[0],
10
);
expect(fixedInterval).not.toBeNaN();
/**
* This value can sometimes be off by 1 as a result of fuzzy calculation.
*
* It had no implications in practice, but from a test standpoint can cause flaky
* snapshot failures.
*/
assertCloseTo(fixedInterval, 36000, 100);
set(
searchMock.mock.calls[2][1],
'body.aggs.by_id.aggs.histogram.date_histogram.fixed_interval',
'36000ms'
);
expect(searchMock.mock.calls).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import { get, set } from 'lodash';
import { ElasticsearchMonitorsAdapter } from '../elasticsearch_monitors_adapter';
import { CountParams, CountResponse } from 'elasticsearch';
import mockChartsData from './monitor_charts_mock.json';

const assertCloseTo = (actual: number, expected: number, precision: number) => {
if (Math.abs(expected - actual) > precision) {
throw new Error(`expected [${actual}] to be within ${precision} of ${actual}`);
}
};
import { assertCloseTo } from '../../../helper';

// FIXME: there are many untested functions in this adapter. They should be tested.
describe('ElasticsearchMonitorsAdapter', () => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { assertCloseTo } from '../assert_close_to';

describe('assertCloseTo', () => {
it('does not throw an error when expected value is correct', () => {
assertCloseTo(10000, 10001, 100);
});

it('does not throw an error when expected value is under actual, but within precision threshold', () => {
assertCloseTo(10000, 9875, 300);
});

it('throws an error when expected value is outside of precision range', () => {
expect(() => assertCloseTo(10000, 12500, 100)).toThrowErrorMatchingSnapshot();
});
});
11 changes: 11 additions & 0 deletions x-pack/legacy/plugins/uptime/server/lib/helper/assert_close_to.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export const assertCloseTo = (actual: number, expected: number, precision: number) => {
if (Math.abs(expected - actual) > precision) {
throw new Error(`expected [${expected}] to be within ${precision} of ${actual}`);
}
};
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/uptime/server/lib/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { formatEsBucketsForHistogram } from './format_es_buckets_for_histogram';
export { getFilterClause } from './get_filter_clause';
export { getHistogramInterval } from './get_histogram_interval';
export { parseFilterQuery } from './parse_filter_query';
export { assertCloseTo } from './assert_close_to';