Skip to content

Commit

Permalink
consolidate mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Nov 14, 2023
1 parent cfc9c2a commit ab4a63c
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const paramsMock = {
index: 'the-index',
timeFieldName: 'the-time-field-name',
start: 0,
end: 50,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
includeFrozen: false,
searchQuery: '{ "match_all": {} }',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { AiopsLogRateAnalysisSchema } from '../../../../../common/api/log_rate_analysis/schema';

import { paramsMock } from './params_match_all';
import { searchQueryMock } from './search_query';

export const paramsSearchQueryMock: AiopsLogRateAnalysisSchema = {
...paramsMock,
searchQuery: searchQueryMock,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

// This is the format that gets passed on from the Kibana search bar.
export const searchQueryMock = JSON.stringify({
bool: {
filter: [],
minimum_should_match: 1,
must_not: [],
should: [{ term: { 'the-term': { value: 'the-value' } } }],
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,12 @@

import { createRandomSamplerWrapper } from '@kbn/ml-random-sampler-utils';

import { paramsMock } from './__mocks__/params_match_all';
import { getBaselineOrDeviationFilter, getCategoryRequest } from './fetch_categories';

describe('getBaselineOrDeviationFilter', () => {
it('returns a filter that matches both baseline and deviation time range', () => {
const params = {
index: 'the-index',
timeFieldName: 'the-time-field-name',
start: 0,
end: 50,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
includeFrozen: false,
searchQuery: '{ "match_all": {} }',
};

const baselineOrDeviationFilter = getBaselineOrDeviationFilter(params);
const baselineOrDeviationFilter = getBaselineOrDeviationFilter(paramsMock);

expect(baselineOrDeviationFilter).toEqual({
bool: {
Expand All @@ -47,25 +35,12 @@ describe('getBaselineOrDeviationFilter', () => {

describe('getCategoryRequest', () => {
it('returns the category request', () => {
const params = {
index: 'the-index',
timeFieldName: 'the-time-field-name',
start: 0,
end: 50,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
includeFrozen: false,
searchQuery: '{ "match_all": {} }',
};

const randomSamplerWrapper = createRandomSamplerWrapper({
probability: 0.1,
seed: 1234,
});

const query = getCategoryRequest(params, 'the-field-name', randomSamplerWrapper);
const query = getCategoryRequest(paramsMock, 'the-field-name', randomSamplerWrapper);

// Because the time range filter is covered by the should clauses that cover both
// baseline (10,20) and deviation (30,40), we expect that there is no other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,9 @@
* 2.0.
*/

import { getCategoryCountRequest, getCategoryCountMSearchRequest } from './fetch_category_counts';
import { paramsMock } from './__mocks__/params_match_all';

const params = {
index: 'the-index',
timeFieldName: 'the-time-field-name',
start: 0,
end: 50,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
includeFrozen: false,
searchQuery: '{ "match_all": {} }',
};
import { getCategoryCountRequest, getCategoryCountMSearchRequest } from './fetch_category_counts';

describe('getCategoryCountRequest', () => {
it('returns the category count request', () => {
Expand All @@ -29,11 +18,11 @@ describe('getCategoryCountRequest', () => {
};

const query = getCategoryCountRequest(
params,
paramsMock,
'the-field-name',
category,
params.baselineMin,
params.baselineMax
paramsMock.baselineMin,
paramsMock.baselineMax
);

expect(query).toEqual({
Expand Down Expand Up @@ -81,11 +70,11 @@ describe('getCategoryCountMSearchRequest', () => {
];

const query = getCategoryCountMSearchRequest(
params,
paramsMock,
'the-field-name',
categories,
params.baselineMin,
params.baselineMax
paramsMock.baselineMin,
paramsMock.baselineMax
);

expect(query).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,14 @@ import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import type { ElasticsearchClient } from '@kbn/core/server';

import type { AiopsLogRateAnalysisSchema } from '../../../../common/api/log_rate_analysis/schema';
import { paramsSearchQueryMock } from './__mocks__/params_search_query';

import { fetchIndexInfo, getRandomDocsRequest } from './fetch_index_info';

const params: AiopsLogRateAnalysisSchema = {
index: 'the-index',
timeFieldName: 'the-time-field-name',
start: 1577836800000,
end: 1609459200000,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
includeFrozen: false,
searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}',
};

describe('fetch_index_info', () => {
describe('getRandomDocsRequest', () => {
it('returns the most basic request body for a sample of random documents', () => {
const req = getRandomDocsRequest(params);
const req = getRandomDocsRequest(paramsSearchQueryMock);

expect(req).toEqual({
body: {
Expand All @@ -40,13 +27,20 @@ describe('fetch_index_info', () => {
query: {
bool: {
filter: [
{ bool: { filter: [], must: [{ match_all: {} }], must_not: [] } },
{
bool: {
filter: [],
minimum_should_match: 1,
must_not: [],
should: [{ term: { 'the-term': { value: 'the-value' } } }],
},
},
{
range: {
'the-time-field-name': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
gte: 0,
lte: 50,
},
},
},
Expand All @@ -59,7 +53,7 @@ describe('fetch_index_info', () => {
size: 1000,
track_total_hits: true,
},
index: params.index,
index: paramsSearchQueryMock.index,
ignore_throttled: undefined,
ignore_unavailable: true,
});
Expand Down Expand Up @@ -105,7 +99,10 @@ describe('fetch_index_info', () => {
search: esClientSearchMock,
} as unknown as ElasticsearchClient;

const { totalDocCount, fieldCandidates } = await fetchIndexInfo(esClientMock, params);
const { totalDocCount, fieldCandidates } = await fetchIndexInfo(
esClientMock,
paramsSearchQueryMock
);

expect(fieldCandidates).toEqual(['myIpFieldName', 'myKeywordFieldName']);
expect(totalDocCount).toEqual(5000000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,28 @@
* 2.0.
*/

import { paramsSearchQueryMock } from './__mocks__/params_search_query';

import { getFilters } from './get_filters';

describe('getFilters', () => {
it('returns an empty array with no timeFieldName and searchQuery supplied', () => {
const filters = getFilters({
index: 'the-index',
...paramsSearchQueryMock,
timeFieldName: '',
searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}',
start: 1577836800000,
end: 1609459200000,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
});
expect(filters).toEqual([]);
});

it('returns a range filter when timeFieldName is supplied', () => {
const filters = getFilters({
index: 'the-index',
timeFieldName: 'the-time-field-name',
searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}',
start: 1577836800000,
end: 1609459200000,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
});
const filters = getFilters(paramsSearchQueryMock);
expect(filters).toEqual([
{
range: {
'the-time-field-name': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
gte: 0,
lte: 50,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,30 @@
* 2.0.
*/

import { getHistogramQuery } from './get_histogram_query';
import { paramsSearchQueryMock } from './__mocks__/params_search_query';

const paramsMock = {
index: 'the-index',
timeFieldName: 'the-time-field-name',
start: 1577836800000,
end: 1609459200000,
baselineMin: 10,
baselineMax: 20,
deviationMin: 30,
deviationMax: 40,
includeFrozen: false,
searchQuery: '{"bool":{"filter":[],"must":[{"match_all":{}}],"must_not":[]}}',
};
import { getHistogramQuery } from './get_histogram_query';

describe('getHistogramQuery', () => {
it('returns histogram query without additional filters', () => {
const query = getHistogramQuery(paramsMock);
const query = getHistogramQuery(paramsSearchQueryMock);
expect(query).toEqual({
bool: {
filter: [
{ bool: { filter: [], must: [{ match_all: {} }], must_not: [] } },
{
bool: {
filter: [],
minimum_should_match: 1,
must_not: [],
should: [{ term: { 'the-term': { value: 'the-value' } } }],
},
},
{
range: {
'the-time-field-name': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
gte: 0,
lte: 50,
},
},
},
Expand All @@ -42,15 +38,22 @@ describe('getHistogramQuery', () => {
});

it('returns histogram query with additional filters', () => {
const query = getHistogramQuery(paramsMock, [
const query = getHistogramQuery(paramsSearchQueryMock, [
{
term: { ['the-filter-fieldName']: 'the-filter-fieldValue' },
},
]);
expect(query).toEqual({
bool: {
filter: [
{ bool: { filter: [], must: [{ match_all: {} }], must_not: [] } },
{
bool: {
filter: [],
minimum_should_match: 1,
must_not: [],
should: [{ term: { 'the-term': { value: 'the-value' } } }],
},
},
{
term: {
'the-filter-fieldName': 'the-filter-fieldValue',
Expand All @@ -60,8 +63,8 @@ describe('getHistogramQuery', () => {
range: {
'the-time-field-name': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
gte: 0,
lte: 50,
},
},
},
Expand Down
Loading

0 comments on commit ab4a63c

Please sign in to comment.