Skip to content
Merged
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
@@ -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,
},
},
});
});
});
});