Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <[email protected]>
  • Loading branch information
raintygao committed Oct 25, 2024
1 parent f2114ff commit 26d1cf5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
12 changes: 9 additions & 3 deletions public/components/incontext_insight/generate_popover_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ export const GeneratePopoverBody: React.FC<{
monitorType === 'query_level_monitor' || monitorType === 'bucket_level_monitor';
let hasTimeRangeFilter = false;
if (dsl) {
const dslObject = JSON.parse(dsl);
let dslObject;
try {
dslObject = JSON.parse(dsl);
} catch (e) {
console.error('Invalid DSL', e);
return;
}
const filters = dslObject?.query?.bool?.filter;
// Filters contains time range filter,if no filters, return.
if (!filters?.length) return;
Expand Down Expand Up @@ -93,7 +99,7 @@ export const GeneratePopoverBody: React.FC<{
defaultMessage: 'Generate summary error',
})
);
closePopover();
// closePopover();
return;
}
const contextContent = contextObj?.context || '';
Expand Down Expand Up @@ -152,7 +158,7 @@ export const GeneratePopoverBody: React.FC<{
defaultMessage: 'Generate summary error',
})
);
closePopover();
// closePopover();
});
};

Expand Down
2 changes: 1 addition & 1 deletion public/utils/alerting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const buildUrlQuery = async (
return hash;
};

export const validateToTimeRange = (time: string) => {
const validateToTimeRange = (time: string) => {
// Alerting uses this format in to field of time range filter.
const TO_TIME_FORMAT = 'YYYY-MM-DDTHH:mm:ssZ';
return moment.utc(time, TO_TIME_FORMAT, true).isValid();
Expand Down
66 changes: 66 additions & 0 deletions public/utils/tests/alerting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { extractTimeRangeDSL } from '../alerting';

describe('extractTimeRangeDSL', () => {
it('should only extract utc time range filter', () => {
expect(extractTimeRangeDSL([{ range: { timestamp: { to: 'now' } } }]).timeRangeDSL).toEqual(
undefined
);
});

it('should return undefined timeFiledName if no time range filter', () => {
expect(
extractTimeRangeDSL([
{
bool: {},
},
]).timeRangeDSL
).toBe(undefined);
});

it('should extract timeFiledName normally', () => {
expect(
extractTimeRangeDSL([
{
range: {
timestamp: {
from: '2024-10-09T17:40:47+00:00||-1h',
to: '2024-10-09T17:40:47+00:00',
include_lower: true,
include_upper: true,
boost: 1,
},
},
},
{
bool: {
must_not: [
{
match_phrase: {
response: {
query: '200',
slop: 0,
zero_terms_query: 'NONE',
boost: 1,
},
},
},
],
adjust_pure_negative: true,
boost: 1,
},
},
]).timeRangeDSL
).toStrictEqual({
from: '2024-10-09T17:40:47+00:00||-1h',
to: '2024-10-09T17:40:47+00:00',
include_lower: true,
include_upper: true,
boost: 1,
});
});
});

0 comments on commit 26d1cf5

Please sign in to comment.