forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Uptime] Add unit tests for QueryContext time calculation (elastic#56671
) Add Unit tests for the QueryContext class that was missing testing. This would have caught elastic#56612
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
.../plugins/uptime/server/lib/adapters/monitor_states/search/__tests__/query_context.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |