Skip to content

Commit

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

Add Unit tests for the QueryContext class that was missing testing. This would have caught elastic#56612
  • Loading branch information
andrewvc committed Feb 4, 2020
1 parent f82d44e commit 4f4430f
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 4f4430f

Please sign in to comment.