Skip to content

Commit

Permalink
Add API test
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Mar 11, 2020
1 parent 797dd1e commit 63256d5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import sinon from 'sinon';
import moment from 'moment';
import { SearchSource } from '../../../../../kibana_services';
import { SearchSource } from '../../../../../../../../../plugins/data/public';

export function createIndexPatternsStub() {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
* under the License.
*/

import expect from '@kbn/expect';
import ngMock from 'ng_mock';
import moment from 'moment';
import * as _ from 'lodash';
import { pluginInstance } from 'plugins/kibana/discover/legacy';

import { createIndexPatternsStub, createContextSearchSourceStub } from './_stubs';

import { fetchContextProvider } from '../context';
import { fetchContextProvider } from './context';

const MS_PER_DAY = 24 * 60 * 60 * 1000;
const ANCHOR_TIMESTAMP = new Date(MS_PER_DAY).toJSON();
Expand All @@ -34,46 +29,41 @@ const ANCHOR_TIMESTAMP_1000 = new Date(MS_PER_DAY * 1000).toJSON();
const ANCHOR_TIMESTAMP_3000 = new Date(MS_PER_DAY * 3000).toJSON();

describe('context app', function() {
beforeEach(() => pluginInstance.initializeInnerAngular());
beforeEach(ngMock.module('app/discover'));

describe('function fetchPredecessors', function() {
let fetchPredecessors;
let searchSourceStub;

beforeEach(
ngMock.inject(function createPrivateStubs() {
searchSourceStub = createContextSearchSourceStub([], '@timestamp', MS_PER_DAY * 8);
fetchPredecessors = (
beforeEach(() => {
searchSourceStub = createContextSearchSourceStub([], '@timestamp', MS_PER_DAY * 8);
fetchPredecessors = (
indexPatternId,
timeField,
sortDir,
timeValIso,
timeValNr,
tieBreakerField,
tieBreakerValue,
size
) => {
const anchor = {
_source: {
[timeField]: timeValIso,
},
sort: [timeValNr, tieBreakerValue],
};

return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs(
'predecessors',
indexPatternId,
anchor,
timeField,
sortDir,
timeValIso,
timeValNr,
tieBreakerField,
tieBreakerValue,
size
) => {
const anchor = {
_source: {
[timeField]: timeValIso,
},
sort: [timeValNr, tieBreakerValue],
};

return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs(
'predecessors',
indexPatternId,
anchor,
timeField,
tieBreakerField,
sortDir,
size,
[]
);
};
})
);
sortDir,
size,
[]
);
};
});

afterEach(() => {
searchSourceStub._restore();
Expand All @@ -99,8 +89,8 @@ describe('context app', function() {
3,
[]
).then(hits => {
expect(searchSourceStub.fetch.calledOnce).to.be(true);
expect(hits).to.eql(searchSourceStub._stubHits.slice(0, 3));
expect(searchSourceStub.fetch.calledOnce).toBe(true);
expect(hits).toEqual(searchSourceStub._stubHits.slice(0, 3));
});
});

Expand Down Expand Up @@ -132,14 +122,14 @@ describe('context app', function() {

expect(
intervals.every(({ gte, lte }) => (gte && lte ? moment(gte).isBefore(lte) : true))
).to.be(true);
).toBe(true);
// should have started at the given time
expect(intervals[0].gte).to.eql(moment(MS_PER_DAY * 3000).toISOString());
expect(intervals[0].gte).toEqual(moment(MS_PER_DAY * 3000).toISOString());
// should have ended with a half-open interval
expect(_.last(intervals)).to.only.have.keys('gte', 'format');
expect(intervals.length).to.be.greaterThan(1);
expect(Object.keys(_.last(intervals))).toEqual(['format', 'gte']);
expect(intervals.length).toBeGreaterThan(1);

expect(hits).to.eql(searchSourceStub._stubHits.slice(0, 3));
expect(hits).toEqual(searchSourceStub._stubHits.slice(0, 3));
});
});

Expand Down Expand Up @@ -169,11 +159,11 @@ describe('context app', function() {
);

// should have started at the given time
expect(intervals[0].gte).to.eql(moment(MS_PER_DAY * 1000).toISOString());
expect(intervals[0].gte).toEqual(moment(MS_PER_DAY * 1000).toISOString());
// should have stopped before reaching MS_PER_DAY * 1700
expect(moment(_.last(intervals).lte).valueOf()).to.be.lessThan(MS_PER_DAY * 1700);
expect(intervals.length).to.be.greaterThan(1);
expect(hits).to.eql(searchSourceStub._stubHits.slice(-3));
expect(moment(_.last(intervals).lte).valueOf()).toBeLessThan(MS_PER_DAY * 1700);
expect(intervals.length).toBeGreaterThan(1);
expect(hits).toEqual(searchSourceStub._stubHits.slice(-3));
});
});

Expand All @@ -189,7 +179,7 @@ describe('context app', function() {
3,
[]
).then(hits => {
expect(hits).to.eql([]);
expect(hits).toEqual([]);
});
});

Expand All @@ -206,8 +196,8 @@ describe('context app', function() {
[]
).then(() => {
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.alwaysCalledWith(undefined)).to.be(true);
expect(setParentSpy.called).to.be(true);
expect(setParentSpy.alwaysCalledWith(undefined)).toBe(true);
expect(setParentSpy.called).toBe(true);
});
});

Expand All @@ -225,7 +215,7 @@ describe('context app', function() {
).then(() => {
expect(
searchSourceStub.setField.calledWith('sort', [{ '@timestamp': 'asc' }, { _doc: 'asc' }])
).to.be(true);
).toBe(true);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,55 @@
* under the License.
*/

import expect from '@kbn/expect';
import ngMock from 'ng_mock';
import moment from 'moment';
import * as _ from 'lodash';
import { pluginInstance } from 'plugins/kibana/discover/legacy';

import { createIndexPatternsStub, createContextSearchSourceStub } from './_stubs';

import { fetchContextProvider } from '../context';
import { fetchContextProvider } from './context';

const MS_PER_DAY = 24 * 60 * 60 * 1000;
const ANCHOR_TIMESTAMP = new Date(MS_PER_DAY).toJSON();
const ANCHOR_TIMESTAMP_3 = new Date(MS_PER_DAY * 3).toJSON();
const ANCHOR_TIMESTAMP_3000 = new Date(MS_PER_DAY * 3000).toJSON();

describe('context app', function() {
beforeEach(() => pluginInstance.initializeInnerAngular());
beforeEach(ngMock.module('app/discover'));

describe('function fetchSuccessors', function() {
let fetchSuccessors;
let searchSourceStub;

beforeEach(
ngMock.inject(function createPrivateStubs() {
searchSourceStub = createContextSearchSourceStub([], '@timestamp');
beforeEach(() => {
searchSourceStub = createContextSearchSourceStub([], '@timestamp');

fetchSuccessors = (
indexPatternId,
timeField,
sortDir,
timeValIso,
timeValNr,
tieBreakerField,
tieBreakerValue,
size
) => {
const anchor = {
_source: {
[timeField]: timeValIso,
},
sort: [timeValNr, tieBreakerValue],
};

fetchSuccessors = (
return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs(
'successors',
indexPatternId,
anchor,
timeField,
sortDir,
timeValIso,
timeValNr,
tieBreakerField,
tieBreakerValue,
size
) => {
const anchor = {
_source: {
[timeField]: timeValIso,
},
sort: [timeValNr, tieBreakerValue],
};

return fetchContextProvider(createIndexPatternsStub()).fetchSurroundingDocs(
'successors',
indexPatternId,
anchor,
timeField,
tieBreakerField,
sortDir,
size,
[]
);
};
})
);
sortDir,
size,
[]
);
};
});

afterEach(() => {
searchSourceStub._restore();
Expand All @@ -99,8 +91,8 @@ describe('context app', function() {
3,
[]
).then(hits => {
expect(searchSourceStub.fetch.calledOnce).to.be(true);
expect(hits).to.eql(searchSourceStub._stubHits.slice(-3));
expect(searchSourceStub.fetch.calledOnce).toBe(true);
expect(hits).toEqual(searchSourceStub._stubHits.slice(-3));
});
});

Expand Down Expand Up @@ -132,14 +124,14 @@ describe('context app', function() {

expect(
intervals.every(({ gte, lte }) => (gte && lte ? moment(gte).isBefore(lte) : true))
).to.be(true);
).toBe(true);
// should have started at the given time
expect(intervals[0].lte).to.eql(moment(MS_PER_DAY * 3000).toISOString());
expect(intervals[0].lte).toEqual(moment(MS_PER_DAY * 3000).toISOString());
// should have ended with a half-open interval
expect(_.last(intervals)).to.only.have.keys('lte', 'format');
expect(intervals.length).to.be.greaterThan(1);
expect(Object.keys(_.last(intervals))).toEqual(['format', 'lte']);
expect(intervals.length).toBeGreaterThan(1);

expect(hits).to.eql(searchSourceStub._stubHits.slice(-3));
expect(hits).toEqual(searchSourceStub._stubHits.slice(-3));
});
});

Expand Down Expand Up @@ -171,12 +163,12 @@ describe('context app', function() {
);

// should have started at the given time
expect(intervals[0].lte).to.eql(moment(MS_PER_DAY * 3000).toISOString());
expect(intervals[0].lte).toEqual(moment(MS_PER_DAY * 3000).toISOString());
// should have stopped before reaching MS_PER_DAY * 2200
expect(moment(_.last(intervals).gte).valueOf()).to.be.greaterThan(MS_PER_DAY * 2200);
expect(intervals.length).to.be.greaterThan(1);
expect(moment(_.last(intervals).gte).valueOf()).toBeGreaterThan(MS_PER_DAY * 2200);
expect(intervals.length).toBeGreaterThan(1);

expect(hits).to.eql(searchSourceStub._stubHits.slice(0, 4));
expect(hits).toEqual(searchSourceStub._stubHits.slice(0, 4));
});
});

Expand All @@ -192,7 +184,7 @@ describe('context app', function() {
3,
[]
).then(hits => {
expect(hits).to.eql([]);
expect(hits).toEqual([]);
});
});

Expand All @@ -209,8 +201,8 @@ describe('context app', function() {
[]
).then(() => {
const setParentSpy = searchSourceStub.setParent;
expect(setParentSpy.alwaysCalledWith(undefined)).to.be(true);
expect(setParentSpy.called).to.be(true);
expect(setParentSpy.alwaysCalledWith(undefined)).toBe(true);
expect(setParentSpy.called).toBe(true);
});
});

Expand All @@ -228,7 +220,7 @@ describe('context app', function() {
).then(() => {
expect(
searchSourceStub.setField.calledWith('sort', [{ '@timestamp': 'desc' }, { _doc: 'desc' }])
).to.be(true);
).toBe(true);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
* under the License.
*/

import { IndexPattern, SearchSource } from '../../../../kibana_services';
import { reverseSortDir, SortDirection } from './utils/sorting';
import { extractNanos, convertIsoToMillis } from './utils/date_conversion';
import { fetchHitsInInterval } from './utils/fetch_hits_in_interval';
import { generateIntervals } from './utils/generate_intervals';
import { getEsQuerySearchAfter } from './utils/get_es_query_search_after';
import { getEsQuerySort } from './utils/get_es_query_sort';
import { Filter, IndexPatternsContract } from '../../../../../../../../../plugins/data/public';
import {
Filter,
IndexPatternsContract,
IndexPattern,
SearchSource,
} from '../../../../../../../../../plugins/data/public';

export type SurrDocType = 'successors' | 'predecessors';
export interface EsHitRecord {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
import { EsQuerySortValue, SortDirection, ISearchSource } from '../../../../../kibana_services';
import {
ISearchSource,
EsQuerySortValue,
SortDirection,
} from '../../../../../../../../../../plugins/data/public';
import { convertTimeValueToIso } from './date_conversion';
import { EsHitRecordList } from '../context';
import { IntervalValue } from './generate_intervals';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { SortDirection } from '../../../../../kibana_services';
import { SortDirection } from '../../../../../../../../../../plugins/data/public';

export type IntervalValue = number | null;

Expand Down

0 comments on commit 63256d5

Please sign in to comment.