Skip to content

Commit

Permalink
fix unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jul 8, 2021
1 parent 8250dd0 commit 509a02d
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { fetchTransactionDurationFieldCandidates } from './query_field_candidate
import { fetchTransactionDurationFieldValuePairs } from './query_field_value_pairs';
import { fetchTransactionDurationPercentiles } from './query_percentiles';
import { fetchTransactionDurationCorrelation } from './query_correlation';
import { fetchTransactionDurationHistogramRangesteps } from './query_histogram_rangesteps';
import { fetchTransactionDurationHistogramRangeSteps } from './query_histogram_range_steps';
import { fetchTransactionDurationRanges, HistogramItem } from './query_ranges';
import type {
AsyncSearchProviderProgress,
Expand Down Expand Up @@ -78,7 +78,7 @@ export const asyncSearchServiceProvider = (
return;
}

const histogramRangeSteps = await fetchTransactionDurationHistogramRangesteps(
const histogramRangeSteps = await fetchTransactionDurationHistogramRangeSteps(
esClient,
params
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ import { getQueryWithParams } from './get_query_with_params';
describe('correlations', () => {
describe('getQueryWithParams', () => {
it('returns the most basic query filtering on processor.event=transaction', () => {
const query = getQueryWithParams({ params: { index: 'apm-*' } });
const query = getQueryWithParams({
params: { index: 'apm-*', start: '2020', end: '2021' },
});
expect(query).toEqual({
bool: {
filter: [{ term: { 'processor.event': 'transaction' } }],
filter: [
{ term: { 'processor.event': 'transaction' } },
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
],
},
});
});
Expand All @@ -24,31 +37,26 @@ describe('correlations', () => {
index: 'apm-*',
serviceName: 'actualServiceName',
transactionName: 'actualTransactionName',
start: '01-01-2021',
end: '31-01-2021',
start: '2020',
end: '2021',
environment: 'dev',
percentileThresholdValue: 75,
},
});
expect(query).toEqual({
bool: {
filter: [
{ term: { 'processor.event': 'transaction' } },
{
term: {
'service.name': 'actualServiceName',
},
},
{
term: {
'transaction.name': 'actualTransactionName',
'processor.event': 'transaction',
},
},
{
range: {
'@timestamp': {
gte: '01-01-2021',
lte: '31-01-2021',
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
Expand All @@ -57,6 +65,16 @@ describe('correlations', () => {
'service.environment': 'dev',
},
},
{
term: {
'service.name': 'actualServiceName',
},
},
{
term: {
'transaction.name': 'actualTransactionName',
},
},
{
range: {
'transaction.duration.us': {
Expand All @@ -71,14 +89,23 @@ describe('correlations', () => {

it('returns a query considering a custom field/value pair', () => {
const query = getQueryWithParams({
params: { index: 'apm-*' },
params: { index: 'apm-*', start: '2020', end: '2021' },
fieldName: 'actualFieldName',
fieldValue: 'actualFieldValue',
});
expect(query).toEqual({
bool: {
filter: [
{ term: { 'processor.event': 'transaction' } },
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
{
term: {
actualFieldName: 'actualFieldValue',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
BucketCorrelation,
} from './query_correlation';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };
const expectations = [1, 3, 5];
const ranges = [{ to: 1 }, { from: 1, to: 3 }, { from: 3, to: 5 }, { from: 5 }];
const fractions = [1, 2, 4, 5];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
shouldBeExcluded,
} from './query_field_candidates';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };

describe('query_field_candidates', () => {
describe('shouldBeExcluded', () => {
Expand Down Expand Up @@ -61,6 +61,15 @@ describe('query_field_candidates', () => {
'processor.event': 'transaction',
},
},
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
getTermsAggRequest,
} from './query_field_value_pairs';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };

describe('query_field_value_pairs', () => {
describe('getTermsAggRequest', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getTransactionDurationRangesRequest,
} from './query_fractions';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };
const ranges = [{ to: 1 }, { from: 1, to: 3 }, { from: 3, to: 5 }, { from: 5 }];

describe('query_fractions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getTransactionDurationHistogramRequest,
} from './query_histogram';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };
const interval = 100;

describe('query_histogram', () => {
Expand All @@ -40,6 +40,15 @@ describe('query_histogram', () => {
'processor.event': 'transaction',
},
},
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getHistogramIntervalRequest,
} from './query_histogram_interval';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };

describe('query_histogram_interval', () => {
describe('getHistogramIntervalRequest', () => {
Expand Down Expand Up @@ -43,6 +43,15 @@ describe('query_histogram_interval', () => {
'processor.event': 'transaction',
},
},
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import type { estypes } from '@elastic/elasticsearch';
import type { ElasticsearchClient } from 'src/core/server';

import {
fetchTransactionDurationHistogramRangesteps,
fetchTransactionDurationHistogramRangeSteps,
getHistogramIntervalRequest,
} from './query_histogram_rangesteps';
} from './query_histogram_range_steps';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };

describe('query_histogram_rangesteps', () => {
describe('query_histogram_range_steps', () => {
describe('getHistogramIntervalRequest', () => {
it('returns the request body for the histogram interval request', () => {
const req = getHistogramIntervalRequest(params);
Expand All @@ -43,6 +43,15 @@ describe('query_histogram_rangesteps', () => {
'processor.event': 'transaction',
},
},
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
],
},
},
Expand All @@ -53,13 +62,14 @@ describe('query_histogram_rangesteps', () => {
});
});

describe('fetchTransactionDurationHistogramRangesteps', () => {
describe('fetchTransactionDurationHistogramRangeSteps', () => {
it('fetches the range steps for the log histogram', async () => {
const esClientSearchMock = jest.fn((req: estypes.SearchRequest): {
body: estypes.SearchResponse;
} => {
return {
body: ({
hits: { total: { value: 10 } },
aggregations: {
transaction_duration_max: {
value: 10000,
Expand All @@ -76,7 +86,7 @@ describe('query_histogram_rangesteps', () => {
search: esClientSearchMock,
} as unknown) as ElasticsearchClient;

const resp = await fetchTransactionDurationHistogramRangesteps(
const resp = await fetchTransactionDurationHistogramRangeSteps(
esClientMock,
params
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const getHistogramIntervalRequest = (
},
});

export const fetchTransactionDurationHistogramRangesteps = async (
export const fetchTransactionDurationHistogramRangeSteps = async (
esClient: ElasticsearchClient,
params: SearchServiceParams
): Promise<number[]> => {
Expand All @@ -59,7 +59,7 @@ export const fetchTransactionDurationHistogramRangesteps = async (

if (resp.body.aggregations === undefined) {
throw new Error(
'fetchTransactionDurationHistogramRangesteps failed, did not return aggregations.'
'fetchTransactionDurationHistogramRangeSteps failed, did not return aggregations.'
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getTransactionDurationPercentilesRequest,
} from './query_percentiles';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };

describe('query_percentiles', () => {
describe('getTransactionDurationPercentilesRequest', () => {
Expand All @@ -41,10 +41,20 @@ describe('query_percentiles', () => {
'processor.event': 'transaction',
},
},
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
],
},
},
size: 0,
track_total_hits: true,
},
index: params.index,
});
Expand All @@ -68,6 +78,7 @@ describe('query_percentiles', () => {
} => {
return {
body: ({
hits: { total: { value: 10 } },
aggregations: {
transaction_duration_percentiles: {
values: percentilesValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getTransactionDurationRangesRequest,
} from './query_ranges';

const params = { index: 'apm-*' };
const params = { index: 'apm-*', start: '2020', end: '2021' };
const rangeSteps = [1, 3, 5];

describe('query_ranges', () => {
Expand Down Expand Up @@ -59,6 +59,15 @@ describe('query_ranges', () => {
'processor.event': 'transaction',
},
},
{
range: {
'@timestamp': {
format: 'epoch_millis',
gte: 1577836800000,
lte: 1609459200000,
},
},
},
],
},
},
Expand Down

0 comments on commit 509a02d

Please sign in to comment.