-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix preview API not considering rules and imputation options (#898)
This commit resolves a bug where the preview API did not account for suppression rules and imputation options. The fix involves passing additional parameters to handle these configurations correctly. Testing: * e2e testing completed. * UT added to cover the new logic. Signed-off-by: Kaituo Li <[email protected]>
- Loading branch information
Showing
6 changed files
with
174 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
public/pages/ConfigureModel/containers/__tests__/SampleAnomalies.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { | ||
HashRouter as Router, | ||
RouteComponentProps, | ||
Route, | ||
Switch, | ||
} from 'react-router-dom'; | ||
import { render, fireEvent, waitFor } from '@testing-library/react'; | ||
import { SampleAnomalies } from '../SampleAnomalies'; | ||
import configureStore from '../../../../redux/configureStore'; | ||
import { httpClientMock, coreServicesMock } from '../../../../../test/mocks'; | ||
import { mockedStore } from '../../../../redux/utils/testUtils'; | ||
import { CoreServicesContext } from '../../../../components/CoreServices/CoreServices'; | ||
import { prepareDetector, focusOnFirstWrongFeature } from '../../utils/helpers'; | ||
import { createMemoryHistory } from 'history'; | ||
import { | ||
FeaturesFormikValues, | ||
ImputationFormikValues, | ||
RuleFormikValues, | ||
} from '../../../../pages/ConfigureModel/models/interfaces'; | ||
import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils'; | ||
import { FEATURE_TYPE } from '../../../../models/interfaces'; | ||
|
||
// Mock the helper functions | ||
jest.mock('../../utils/helpers', () => ({ | ||
...jest.requireActual('../../utils/helpers'), | ||
prepareDetector: jest.fn(() => ({ | ||
// mock a detector object with at least an id, preventing the undefined error when detector.id is accessed in getSampleAdResult. | ||
id: 'test-detector-id', | ||
name: 'test-detector', | ||
description: 'test-detector-description', | ||
})), | ||
focusOnFirstWrongFeature: jest.fn(() => false), | ||
})); | ||
|
||
// Mock the Redux actions if necessary | ||
jest.mock('../../../../redux/reducers/previewAnomalies', () => ({ | ||
previewDetector: jest.fn(() => async () => Promise.resolve()), | ||
})); | ||
|
||
describe('<SampleAnomalies /> spec', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
console.error = jest.fn(); | ||
console.warn = jest.fn(); | ||
}); | ||
|
||
test('calls prepareDetector with imputationOption and suppressionRules when previewDetector button is clicked', async () => { | ||
// Set up the mock props | ||
const mockDetector = getRandomDetector(); | ||
|
||
const mockFeatureList: FeaturesFormikValues[] = [ | ||
{ | ||
featureId: 'feature1', | ||
featureName: 'Feature 1', | ||
featureEnabled: true, | ||
aggregationBy: 'sum', | ||
aggregationOf: [{ label: 'bytes' }], | ||
featureType: FEATURE_TYPE.SIMPLE, | ||
aggregationQuery: '', | ||
}, | ||
]; | ||
|
||
const mockImputationOption: ImputationFormikValues = { | ||
imputationMethod: 'zero', | ||
}; | ||
|
||
const mockSuppressionRules: RuleFormikValues[] = [ | ||
{ | ||
featureName: '', | ||
absoluteThreshold: undefined, | ||
relativeThreshold: undefined, | ||
aboveBelow: 'above', | ||
}, | ||
]; | ||
|
||
const props = { | ||
detector: mockDetector, | ||
featureList: mockFeatureList, | ||
shingleSize: 8, | ||
categoryFields: ['category1'], | ||
errors: {}, | ||
setFieldTouched: jest.fn(), | ||
imputationOption: mockImputationOption, | ||
suppressionRules: mockSuppressionRules, | ||
}; | ||
|
||
// Create a mock history and store | ||
const history = createMemoryHistory(); | ||
const initialState = { | ||
anomalies: { | ||
anomaliesResult: { | ||
anomalies: [], | ||
}, | ||
}, | ||
}; | ||
const store = mockedStore(); | ||
|
||
const { getByText } = render( | ||
<Provider store={store}> | ||
<Router history={history}> | ||
<CoreServicesContext.Provider value={coreServicesMock}> | ||
<SampleAnomalies {...props} /> | ||
</CoreServicesContext.Provider> | ||
</Router> | ||
</Provider> | ||
); | ||
|
||
// Simulate clicking the previewDetector button | ||
fireEvent.click(getByText('Preview anomalies')); | ||
|
||
// Wait for async actions to complete | ||
await waitFor(() => { | ||
expect(prepareDetector).toHaveBeenCalledWith( | ||
props.featureList, | ||
props.shingleSize, | ||
props.categoryFields, | ||
expect.anything(), // newDetector (could be the same as props.detector or modified) | ||
true, | ||
props.imputationOption, | ||
props.suppressionRules | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters