Skip to content

Commit

Permalink
Updating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed May 26, 2022
1 parent 9a6a1d8 commit 6baacd4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
Expand Up @@ -29,10 +29,10 @@ describe('ActionContext', () => {
hits: [],
link: 'link-mock',
};
const context = addMessages({ name: '[alert-name]' }, base, params);
expect(context.title).toMatchInlineSnapshot(`"rule '[alert-name]' matched query"`);
const context = addMessages({ name: '[rule-name]' }, base, params);
expect(context.title).toMatchInlineSnapshot(`"rule '[rule-name]' matched query"`);
expect(context.message).toEqual(
`rule '[alert-name]' is active:
`rule '[rule-name]' is active:
- Value: 42
- Conditions Met: count greater than 4 over 5m
Expand All @@ -41,6 +41,37 @@ describe('ActionContext', () => {
);
});

it('generates expected properties when isRecovered is true', async () => {
const params = EsQueryRuleParamsSchema.validate({
index: ['[index]'],
timeField: '[timeField]',
esQuery: `{\n \"query\":{\n \"match_all\" : {}\n }\n}`,
size: 100,
timeWindowSize: 5,
timeWindowUnit: 'm',
thresholdComparator: '>',
threshold: [4],
searchType: 'esQuery',
}) as OnlyEsQueryRuleParams;
const base: EsQueryRuleActionContext = {
date: '2020-01-01T00:00:00.000Z',
value: 42,
conditions: 'count not greater than 4',
hits: [],
link: 'link-mock',
};
const context = addMessages({ name: '[rule-name]' }, base, params, true);
expect(context.title).toMatchInlineSnapshot(`"rule '[rule-name]' recovered"`);
expect(context.message).toEqual(
`rule '[rule-name]' is recovered:
- Value: 42
- Conditions Met: count not greater than 4 over 5m
- Timestamp: 2020-01-01T00:00:00.000Z
- Link: link-mock`
);
});

it('generates expected properties if comparator is between', async () => {
const params = EsQueryRuleParamsSchema.validate({
index: ['[index]'],
Expand All @@ -60,10 +91,10 @@ describe('ActionContext', () => {
hits: [],
link: 'link-mock',
};
const context = addMessages({ name: '[alert-name]' }, base, params);
expect(context.title).toMatchInlineSnapshot(`"rule '[alert-name]' matched query"`);
const context = addMessages({ name: '[rule-name]' }, base, params);
expect(context.title).toMatchInlineSnapshot(`"rule '[rule-name]' matched query"`);
expect(context.message).toEqual(
`rule '[alert-name]' is active:
`rule '[rule-name]' is active:
- Value: 4
- Conditions Met: count between 4 and 5 over 5m
Expand Down
Expand Up @@ -64,7 +64,7 @@ export function addMessages(
window,
date: baseContext.date,
link: baseContext.link,
verb: isRecovered ? 'recovered' : 'active,',
verb: isRecovered ? 'recovered' : 'active',
},
});

Expand Down
Expand Up @@ -5,8 +5,14 @@
* 2.0.
*/

import { getSearchParams, getValidTimefieldSort, tryToParseAsDate } from './executor';
import {
getSearchParams,
getValidTimefieldSort,
tryToParseAsDate,
getContextConditionsDescription,
} from './executor';
import { OnlyEsQueryRuleParams } from './types';
import { Comparator } from '../../../common/comparator_types';

describe('es_query executor', () => {
const defaultProps = {
Expand Down Expand Up @@ -78,4 +84,21 @@ describe('es_query executor', () => {
).toThrow('invalid format for windowSize: "5r"');
});
});

describe('getContextConditionsDescription', () => {
it('should return conditions correctly', () => {
const result = getContextConditionsDescription(Comparator.GT, [10]);
expect(result).toBe(`Number of matching documents is greater than 10`);
});

it('should return conditions correctly when isRecovered is true', () => {
const result = getContextConditionsDescription(Comparator.GT, [10], true);
expect(result).toBe(`Number of matching documents is NOT greater than 10`);
});

it('should return conditions correctly when multiple thresholds provided', () => {
const result = getContextConditionsDescription(Comparator.BETWEEN, [10, 20], true);
expect(result).toBe(`Number of matching documents is NOT between 10 and 20`);
});
});
});
Expand Up @@ -197,7 +197,7 @@ export function getContextConditionsDescription(
values: {
thresholdComparator: getHumanReadableComparator(comparator),
threshold: threshold.join(' and '),
negation: isRecovered ? 'NOT ' : ' ',
negation: isRecovered ? 'NOT ' : '',
},
});
}

0 comments on commit 6baacd4

Please sign in to comment.