Skip to content

Commit

Permalink
Add tests for saved search creation and loading for query enhancement (
Browse files Browse the repository at this point in the history
…opensearch-project#9112)

* Add tests for saved search creation and loading for query enhancement
* click on a random element before typing on query editor to bypass the popover that appears
* address pr comments - refactor to create saved search as an api call
* address pr comments - specifically navigate to discover page before setting datasource

Signed-off-by: Justin Kim <[email protected]>

---------

Signed-off-by: Justin Kim <[email protected]>
Signed-off-by: Anan Zhuang <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Co-authored-by: Anan Zhuang <[email protected]>
  • Loading branch information
3 people authored Jan 6, 2025
1 parent 15a19fa commit 47a2351
Show file tree
Hide file tree
Showing 15 changed files with 941 additions and 101 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/9112.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
- Add tests for saving search and loading it ([#9112](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9112))
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
START_TIME,
END_TIME,
} from '../../../../../utils/apps/constants';
import { BASE_PATH, SECONDARY_ENGINE } from '../../../../../utils/constants';
import { SECONDARY_ENGINE } from '../../../../../utils/constants';

const randomString = Math.random().toString(36).substring(7);
const workspace = `${WORKSPACE_NAME}-${randomString}`;
Expand Down Expand Up @@ -38,6 +38,11 @@ describe('dataset selector', { scrollBehavior: false }, () => {
cy.deleteWorkspaceByName(`${workspace}`);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(`${DATASOURCE_NAME}`, `${workspace}`);
cy.navigateToWorkSpaceSpecificPage({
workspaceName: workspace,
page: 'discover',
isEnhancement: true,
});
});

afterEach(() => {
Expand All @@ -46,21 +51,7 @@ describe('dataset selector', { scrollBehavior: false }, () => {

describe('select indices', () => {
it('with SQL as default language', function () {
cy.getElementByTestId(`datasetSelectorButton`).click();
cy.getElementByTestId(`datasetSelectorAdvancedButton`).click();
cy.get(`[title="Indexes"]`).click();
cy.get(`[title=${DATASOURCE_NAME}]`).click();
cy.get(`[title="data_logs_small_time_1"]`).click(); // Updated to match loaded data
cy.getElementByTestId('datasetSelectorNext').click();

cy.get(`[class="euiModalHeader__title"]`).should('contain', 'Step 2: Configure data');

// Select SQL
cy.getElementByTestId('advancedSelectorLanguageSelect').select('OpenSearch SQL');
cy.getElementByTestId(`advancedSelectorTimeFieldSelect`).select('timestamp');
cy.getElementByTestId('advancedSelectorConfirmButton').click();

cy.waitForLoader(true);
cy.setIndexAsDataset('data_logs_small_time_1', DATASOURCE_NAME, 'OpenSearch SQL');

// SQL should already be selected
cy.getElementByTestId('queryEditorLanguageSelector').should('contain', 'OpenSearch SQL');
Expand All @@ -78,22 +69,7 @@ describe('dataset selector', { scrollBehavior: false }, () => {
});

it('with PPL as default language', function () {
cy.getElementByTestId(`datasetSelectorButton`).click();
cy.getElementByTestId(`datasetSelectorAdvancedButton`).click();
cy.get(`[title="Indexes"]`).click();
cy.get(`[title=${DATASOURCE_NAME}]`).click();
cy.get(`[title="data_logs_small_time_1"]`).click(); // Updated to match loaded data
cy.getElementByTestId('datasetSelectorNext').click();

cy.get(`[class="euiModalHeader__title"]`).should('contain', 'Step 2: Configure data');

// Select PPL
cy.getElementByTestId('advancedSelectorLanguageSelect').select('PPL');

cy.getElementByTestId(`advancedSelectorTimeFieldSelect`).select('timestamp');
cy.getElementByTestId('advancedSelectorConfirmButton').click();

cy.waitForLoader(true);
cy.setIndexAsDataset('data_logs_small_time_1', DATASOURCE_NAME, 'PPL');

// PPL should already be selected
cy.getElementByTestId('queryEditorLanguageSelector').should('contain', 'PPL');
Expand All @@ -119,22 +95,20 @@ describe('dataset selector', { scrollBehavior: false }, () => {
it('create index pattern and select it', function () {
// Create and select index pattern for data_logs_small_time_1*
cy.createWorkspaceIndexPatterns({
url: `${BASE_PATH}`,
workspaceName: `${workspace}`,
workspaceName: workspace,
indexPattern: 'data_logs_small_time_1',
timefieldName: 'timestamp',
indexPatternHasTimefield: true,
dataSource: DATASOURCE_NAME,
isEnhancement: true,
});

cy.navigateToWorkSpaceHomePage(`${BASE_PATH}`, `${workspace}`);
cy.navigateToWorkSpaceHomePage(workspace);

cy.waitForLoader(true);
cy.getElementByTestId(`datasetSelectorButton`).click();
cy.getElementByTestId(`datasetSelectorAdvancedButton`).click();
cy.get(`[title="Index Patterns"]`).click();
cy.get(`[title="${DATASOURCE_NAME}::data_logs_small_time_1*"]`).should('exist');
cy.setIndexPatternAsDataset('data_logs_small_time_1*', DATASOURCE_NAME);
// setting OpenSearch SQL as the code following it does not work if this test is isolated
cy.setQueryLanguage('OpenSearch SQL');

cy.waitForLoader(true);
cy.waitForSearch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
START_TIME,
END_TIME,
} from '../../../../../utils/apps/constants';
import { BASE_PATH, SECONDARY_ENGINE } from '../../../../../utils/constants';
import { SECONDARY_ENGINE } from '../../../../../utils/constants';

const randomString = Math.random().toString(36).substring(7);
const workspace = `${WORKSPACE_NAME}-${randomString}`;
Expand Down Expand Up @@ -37,22 +37,24 @@ describe('query enhancement queries', { scrollBehavior: false }, () => {

// Create and select index pattern for data_logs_small_time_1*
cy.createWorkspaceIndexPatterns({
url: `${BASE_PATH}`,
workspaceName: `${workspace}`,
workspaceName: workspace,
indexPattern: 'data_logs_small_time_1',
timefieldName: 'timestamp',
indexPatternHasTimefield: true,
dataSource: DATASOURCE_NAME,
isEnhancement: true,
});

// Go to workspace home
cy.navigateToWorkSpaceHomePage(`${BASE_PATH}`, `${workspace}`);
cy.waitForLoader(true);
// Go to discover page
cy.navigateToWorkSpaceSpecificPage({
workspaceName: workspace,
page: 'discover',
isEnhancement: true,
});
});

after(() => {
cy.deleteWorkspaceByName(`${workspace}`);
cy.deleteWorkspaceByName(workspace);
cy.deleteDataSourceByName(`${DATASOURCE_NAME}`);
cy.deleteIndex('data_logs_small_time_1');
});
Expand All @@ -63,7 +65,7 @@ describe('query enhancement queries', { scrollBehavior: false }, () => {
cy.setTopNavDate(START_TIME, END_TIME);

const query = `_id:1`;
cy.setSingleLineQueryEditor(query);
cy.setQueryEditor(query);
cy.waitForLoader(true);
cy.waitForSearch();
cy.verifyHitCount(1);
Expand All @@ -78,7 +80,7 @@ describe('query enhancement queries', { scrollBehavior: false }, () => {
cy.setTopNavDate(START_TIME, END_TIME);

const query = `_id:1`;
cy.setSingleLineQueryEditor(query);
cy.setQueryEditor(query);
cy.waitForLoader(true);
cy.waitForSearch();
cy.verifyHitCount(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import {
INDEX_PATTERN_WITH_TIME,
INDEX_WITH_TIME_1,
INDEX_WITH_TIME_2,
QueryLanguages,
SECONDARY_ENGINE,
} from '../../../../../utils/constants';
import {
workspaceName,
datasourceName,
generateAllTestConfigurations,
setDatePickerDatesAndSearchIfRelevant,
setSearchConfigurations,
verifyDiscoverPageState,
verifySavedSearchInAssetsPage,
postRequestSaveSearch,
} from './utils/saved_search';

export const runSavedSearchTests = () => {
describe('saved search', () => {
beforeEach(() => {
// Load test data
cy.setupTestData(
SECONDARY_ENGINE.url,
[
`cypress/fixtures/query_enhancements/data-logs-1/${INDEX_WITH_TIME_1}.mapping.json`,
`cypress/fixtures/query_enhancements/data-logs-2/${INDEX_WITH_TIME_2}.mapping.json`,
],
[
`cypress/fixtures/query_enhancements/data-logs-1/${INDEX_WITH_TIME_1}.data.ndjson`,
`cypress/fixtures/query_enhancements/data-logs-2/${INDEX_WITH_TIME_2}.data.ndjson`,
]
);
// Add data source
cy.addDataSource({
name: datasourceName,
url: SECONDARY_ENGINE.url,
authType: 'no_auth',
});

// Create workspace
cy.deleteWorkspaceByName(workspaceName);
cy.visit('/app/home');
cy.createInitialWorkspaceWithDataSource(datasourceName, workspaceName);
cy.createWorkspaceIndexPatterns({
workspaceName: workspaceName,
indexPattern: INDEX_PATTERN_WITH_TIME.replace('*', ''),
timefieldName: 'timestamp',
dataSource: datasourceName,
isEnhancement: true,
});
});

afterEach(() => {
cy.deleteWorkspaceByName(workspaceName);
// // TODO: Modify deleteIndex to handle an array of index and remove hard code
cy.deleteDataSourceByName(datasourceName);
cy.deleteIndex(INDEX_WITH_TIME_1);
cy.deleteIndex(INDEX_WITH_TIME_2);
});

generateAllTestConfigurations().forEach((config) => {
it(`should successfully create a saved search for ${config.testName}`, () => {
cy.navigateToWorkSpaceSpecificPage({
workspaceName,
page: 'discover',
isEnhancement: true,
});

cy.setDataset(config.dataset, datasourceName, config.datasetType);

cy.setQueryLanguage(config.language);
setDatePickerDatesAndSearchIfRelevant(config.language);

setSearchConfigurations(config);
verifyDiscoverPageState(config);
cy.saveSearch(config.saveName);

// There is a small chance where if we go to assets page,
// the saved search does not appear. So adding this wait
cy.wait(2000);

verifySavedSearchInAssetsPage(config);
});

// We are starting from various languages
// to guard against: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/9078
Object.values(QueryLanguages)
.map((queryLanguage) => queryLanguage.name)
.forEach((startingLanguage) => {
// TODO: Remove this line once bugs are fixed
// https://github.com/opensearch-project/OpenSearch-Dashboards/issues/9078
if (startingLanguage !== config.language) return;

it(`should successfully load a saved search for ${config.testName} starting from ${startingLanguage}`, () => {
postRequestSaveSearch(config);

cy.navigateToWorkSpaceSpecificPage({
workspaceName,
page: 'discover',
isEnhancement: true,
});
cy.getElementByTestId('discoverNewButton').click();

// Intentionally setting INDEX_PATTERN dataset here so that
// we have access to all four languages that INDEX_PATTERN allows.
// This means that we are only testing loading a saved search
// starting from an INDEX_PATTERN dataset, but I think testing where the
// start is a permutation of other dataset is overkill
cy.setIndexPatternAsDataset(INDEX_PATTERN_WITH_TIME, datasourceName);

cy.setQueryLanguage(startingLanguage);
cy.loadSaveSearch(config.saveName, false);
setDatePickerDatesAndSearchIfRelevant(config.language);
verifyDiscoverPageState(config);
});
});
});
});
};

runSavedSearchTests();
Loading

0 comments on commit 47a2351

Please sign in to comment.