Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UTs for Saved queries new UI #8798

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

riysaxen-amzn
Copy link
Contributor

@riysaxen-amzn riysaxen-amzn commented Nov 3, 2024

Description

Created UTs for Saved queries new UI(#8469)

Issues Resolved

Screenshot

Testing the changes

yarn test:jest src/plugins/data/public/query/saved_query/saved_query_service.test.ts
yarn run v1.22.22
$ scripts/use_node scripts/jest src/plugins/data/public/query/saved_query/saved_query_service.test.ts
jest-haste-map: duplicate manual mock found: index
  The following files share their name; please delete one of them:
    * <rootDir>/src/plugins/vis_type_vega/public/expressions/__mocks__/index.ts
    * <rootDir>/src/core/server/saved_objects/import/__mocks__/index.ts

 PASS  src/plugins/data/public/query/saved_query/saved_query_service.test.ts (5.786 s)
  saved query service
    saveQuery
      ✓ should create a saved object for the given attributes (3 ms)
      ✓ should allow overwriting an existing saved query (1 ms)
      ✓ should optionally accept filters and timefilters in object format (1 ms)
      ✓ should throw an error when saved objects client returns error
      ✓ should throw an error if the saved query does not have a title (1 ms)
      ✓ should include dataset in the query when query enhancement is enabled and dataset exists (1 ms)
      ✓ should include isTemplate in the query object when new saved queries UI is enabled and isTemplate is true
    findSavedQueries
      ✓ should find and return saved queries without search text or pagination parameters (1 ms)
      ✓ should return the total count along with the requested queries
      ✓ should find and return saved queries with search text matching the title field (1 ms)
      ✓ should find and return parsed filters and timefilters items
      ✓ should return an array of saved queries
      ✓ should accept perPage and page properties (1 ms)
      ✓ should correctly parse a json query string
      ✓ should correctly parse a json object
      ✓ should handle null string with single quote
      ✓ should handle null string with double quote
      ✓ should handle null quoted string
      ✓ should not lose double quotes
    getSavedQuery
      ✓ should retrieve a saved query by id (1 ms)
      ✓ should only return saved queries
    deleteSavedQuery
      ✓ should delete the saved query for the given ID
    getAllSavedQueries
      ✓ should return all the saved queries (1 ms)
    getSavedQueryCount
      ✓ should return the total number of saved queries

yarn test:jest src/plugins/data/public/plugin.*                                     
yarn run v1.22.22
$ scripts/use_node scripts/jest src/plugins/data/public/plugin.test.ts src/plugins/data/public/plugin.ts
jest-haste-map: duplicate manual mock found: index
  The following files share their name; please delete one of them:
    * <rootDir>/src/plugins/vis_type_vega/public/expressions/__mocks__/index.ts
    * <rootDir>/src/core/server/saved_objects/import/__mocks__/index.ts

 PASS  src/plugins/data/public/plugin.test.ts (9.515 s)
  #DataPublicPlugin setup
    ✓ should setup the plugin and set useNewSavedQueriesUI (17 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        10.377 s, estimated 14 s
Ran all test suites matching /src\/plugins\/data\/public\/plugin.test.ts|src\/plugins\/data\/public\/plugin.ts/i.
✨  Done in 16.33s.

Changelog

Check List

  • All tests pass
    • yarn test:jest
    • yarn test:jest_integration
  • New functionality includes testing.
  • New functionality has been documented.
  • Update CHANGELOG.md
  • Commits are signed per the DCO using --signoff

Signed-off-by: Riya Saxena <riysaxen@amazon.com>
Signed-off-by: Riya Saxena <riysaxen@amazon.com>
Signed-off-by: Riya Saxena <riysaxen@amazon.com>
Copy link
Contributor

github-actions bot commented Nov 3, 2024

ℹ️ Manual Changeset Creation Reminder

Please ensure manual commit for changeset file 8798.yml under folder changelogs/fragments to complete this PR.

If you want to use the available OpenSearch Changeset Bot App to avoid manual creation of changeset file you can install it in your forked repository following this link.

For more information about formatting of changeset files, please visit OpenSearch Auto Changeset and Release Notes Tool.

Copy link
Contributor

github-actions bot commented Nov 3, 2024

❌ Changeset File Not Added Yet

Please ensure manual commit for changeset file 8798.yml under folder changelogs/fragments to complete this PR. File still missing.

Copy link

codecov bot commented Nov 3, 2024

Codecov Report

Attention: Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 60.83%. Comparing base (78204a0) to head (557e0ce).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...s/data/public/autocomplete/autocomplete_service.ts 0.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8798      +/-   ##
==========================================
+ Coverage   60.78%   60.83%   +0.05%     
==========================================
  Files        3798     3798              
  Lines       90701    90701              
  Branches    14284    14284              
==========================================
+ Hits        55135    55181      +46     
+ Misses      32067    32021      -46     
  Partials     3499     3499              
Flag Coverage Δ
Linux_1 29.05% <0.00%> (ø)
Linux_2 56.39% <ø> (ø)
Linux_3 37.74% <33.33%> (+0.08%) ⬆️
Linux_4 29.82% <0.00%> (ø)
Windows_1 29.06% <0.00%> (ø)
Windows_2 56.34% <ø> (ø)
Windows_3 37.74% <33.33%> (+0.08%) ⬆️
Windows_4 29.82% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

riysaxen-amzn and others added 2 commits November 4, 2024 09:14
Signed-off-by: Riya Saxena <riysaxen@amazon.com>
@goyamegh
Copy link
Contributor

goyamegh commented Nov 4, 2024

Can you add the changeset file and check codecov, failing.

@virajsanghvi
Copy link
Collaborator

Callouts:

  • it looks like saved query unit test is failing
  • Haven't looked yet, but while I assume this is just UTs, patch cov check is failing at 33%

Signed-off-by: Riya Saxena <riysaxen@amazon.com>
@riysaxen-amzn
Copy link
Contributor Author

Callouts:

  • it looks like saved query unit test is failing
  • Haven't looked yet, but while I assume this is just UTs, patch cov check is failing at 33%

yes, i observed UTs failing for saved queries, revised the Pr to fix them

@@ -51,7 +51,7 @@ export class AutocompleteService {
private getValueSuggestions?: ValueSuggestionsGetFn;

private addQuerySuggestionProvider = (language: string, provider: QuerySuggestionGetFn): void => {
if (language && provider && this.autocompleteConfig.querySuggestions.enabled) {
if (language && provider && this.autocompleteConfig?.querySuggestions.enabled) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what led us to using optional chaining if we're not exercising this through testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like we don't have any coverage on autocomplete_service.ts,

While writing unit tests for src/plugins/data/public/plugin, we initialize all the services, including

   this.searchService = new SearchService(initializerContext);
    this.uiService = new UiService(initializerContext);
    this.queryService = new QueryService();
    this.fieldFormatsService = new FieldFormatsService();
    this.autocomplete = new AutocompleteService(initializerContext);

The optional chaining ensures that if autocompleteConfig or querySuggestions is not defined (i.e., null or undefined), it won’t cause a runtime error when trying to access enabled.
In short, while the autocomplete_service.ts might not have direct test coverage, the use of optional chaining here is a safety feature that prevents errors when properties or objects are not available during execution. If the service were better covered in the tests, we’d be able to verify if this behavior is actually necessary or if there is a more predictable initialization pattern that could be tested directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you add it unprompted as a safety measure, or because you saw it failing in some cases? I am curious about the other cases as well, but using this as an example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why would querySuggestions be undefined? it's enforced by type and config-schema which provides a default value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i added it because the UTs were failing with Cannot read property of undefined for the following lines

Copy link
Collaborator

@AMoo-Miki AMoo-Miki Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer for the UTs to be fixed to call the stack correctly. Do you know which ones were failing? Not that this change is unsafe to have... just that those tests that are not creating the proper context could be missing stuff.

Comment on lines 57 to 60
if (setting === UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED) {
return true;
}
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: return setting === UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

Signed-off-by: Riya Saxena <riysaxen@amazon.com>
describe('#DataPublicPlugin setup', () => {
let coreSetup: any;
let coreStart: any;
let plugin;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. avoid any if possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

@@ -183,7 +183,7 @@ export class DataPublicPlugin

const useNewSavedQueriesUI =
core.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED) &&
this.config.savedQueriesNewUI.enabled;
this.config.savedQueriesNewUI?.enabled;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these files aren't covered by any UTs, so when writing tests for data/public/plugin since all these services are initialized, i was getting the error Cannot read property of undefined for all these lies [ref: https://github.com//pull/8798#discussion_r1828538194]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we're just mocking things to not have to change the source code. It's mainly problematic if we fail in an unexpected way (there are times where failing fast and throwing is preferable to going down a code path that we didn't intend, and patch coverage % points to that being a concern here), but its usages in this CR don't seem that problematic, so I'm fine approving. Please do review however, and if the optional chaining presents a risk, let's fix in tests vs source.

@@ -51,7 +51,7 @@ export class AutocompleteService {
private getValueSuggestions?: ValueSuggestionsGetFn;

private addQuerySuggestionProvider = (language: string, provider: QuerySuggestionGetFn): void => {
if (language && provider && this.autocompleteConfig.querySuggestions.enabled) {
if (language && provider && this.autocompleteConfig?.querySuggestions.enabled) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why would querySuggestions be undefined? it's enforced by type and config-schema which provides a default value

Signed-off-by: Riya Saxena <riysaxen@amazon.com>
@@ -183,7 +183,7 @@ export class DataPublicPlugin

const useNewSavedQueriesUI =
core.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED) &&
this.config.savedQueriesNewUI.enabled;
this.config.savedQueriesNewUI?.enabled;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we're just mocking things to not have to change the source code. It's mainly problematic if we fail in an unexpected way (there are times where failing fast and throwing is preferable to going down a code path that we didn't intend, and patch coverage % points to that being a concern here), but its usages in this CR don't seem that problematic, so I'm fine approving. Please do review however, and if the optional chaining presents a risk, let's fix in tests vs source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants