Skip to content

Commit

Permalink
[Uptime] Add unit tests for QueryContext time calculation (#56671) (#…
Browse files Browse the repository at this point in the history
…56718)

Add Unit tests for the QueryContext class that was missing testing. This would have caught #56612

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
andrewvc and elasticmachine committed Feb 5, 2020
1 parent f0194db commit abcc6b7
Showing 1 changed file with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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 { QueryContext } from '../query_context';
import { CursorPagination } from '../..';
import { CursorDirection, SortOrder } from '../../../../../../common/graphql/types';

describe(QueryContext, () => {
// 10 minute range
const rangeStart = '2019-02-03T19:06:54.939Z';
const rangeEnd = '2019-02-03T19:16:54.939Z';

const pagination: CursorPagination = {
cursorDirection: CursorDirection.AFTER,
sortOrder: SortOrder.DESC,
};

let qc: QueryContext;
beforeEach(() => (qc = new QueryContext({}, rangeStart, rangeEnd, pagination, null, 10)));

describe('dateRangeFilter()', () => {
const expectedRange = {
range: {
'@timestamp': {
gte: rangeStart,
lte: rangeEnd,
},
},
};
describe('when hasTimespan() is true', () => {
it('should create a date range filter including the timespan', async () => {
const mockHasTimespan = jest.fn();
mockHasTimespan.mockReturnValue(true);
qc.hasTimespan = mockHasTimespan;

expect(await qc.dateRangeFilter()).toEqual({
bool: {
filter: [
expectedRange,
{
bool: {
should: [
qc.timespanClause(),
{ bool: { must_not: { exists: { field: 'monitor.timespan' } } } },
],
},
},
],
},
});
});
});

describe('when hasTimespan() is false', () => {
it('should only use the timestamp fields in the returned filter', async () => {
const mockHasTimespan = jest.fn();
mockHasTimespan.mockReturnValue(false);
qc.hasTimespan = mockHasTimespan;

expect(await qc.dateRangeFilter()).toEqual(expectedRange);
});
});
});

describe('timespanClause()', () => {
it('should always cover the last 5m', () => {
// 5m expected range between GTE and LTE in the response
// since timespan is hardcoded to 5m
expect(qc.timespanClause()).toEqual({
range: {
'monitor.timespan': {
// end date minus 5m
gte: new Date(Date.parse(rangeEnd) - 5 * 60 * 1000).toISOString(),
lte: rangeEnd,
},
},
});
});
});
});

0 comments on commit abcc6b7

Please sign in to comment.