-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[Tests] Check Suggestion parsers robustness for new fields #25132
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -41,10 +41,12 @@ | |
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Predicate; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; | ||
| import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
| import static org.elasticsearch.test.XContentTestUtils.insertRandomFields; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; | ||
|
|
||
| public class SuggestionTests extends ESTestCase { | ||
|
|
@@ -98,16 +100,36 @@ public static Suggestion<? extends Entry<? extends Option>> createTestItem(Class | |
| return suggestion; | ||
| } | ||
|
|
||
| @SuppressWarnings({ "rawtypes" }) | ||
| public void testFromXContent() throws IOException { | ||
| doTestFromXContent(false); | ||
| } | ||
|
|
||
| public void testFromXContentWithRandomFields() throws IOException { | ||
| doTestFromXContent(true); | ||
| } | ||
|
|
||
| @SuppressWarnings({ "rawtypes" }) | ||
| private void doTestFromXContent(boolean addRandomFields) throws IOException { | ||
| ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true")); | ||
| for (Class<Suggestion<? extends Entry<? extends Option>>> type : SUGGESTION_TYPES) { | ||
| Suggestion suggestion = createTestItem(type); | ||
| XContentType xContentType = randomFrom(XContentType.values()); | ||
| boolean humanReadable = randomBoolean(); | ||
| BytesReference originalBytes = toXContent(suggestion, xContentType, params, humanReadable); | ||
| BytesReference originalBytes = toShuffledXContent(suggestion, xContentType, params, humanReadable); | ||
| BytesReference mutated; | ||
| if (addRandomFields) { | ||
| // - "contexts" is an object consisting of key/array pairs, we shouldn't add anything random there | ||
| // - there can be inner search hits fields inside this option where we cannot add random stuff | ||
| // - the root object should be excluded since it contains the named suggestion arrays | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the comments |
||
| Predicate<String> excludeFilter = path -> (path.isEmpty() | ||
| || path.endsWith(CompletionSuggestion.Entry.Option.CONTEXTS.getPreferredName()) || path.endsWith("highlight") | ||
| || path.endsWith("fields") || path.contains("_source") || path.contains("inner_hits")); | ||
| mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random()); | ||
| } else { | ||
| mutated = originalBytes; | ||
| } | ||
| Suggestion parsed; | ||
| try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { | ||
| try (XContentParser parser = createParser(xContentType.xContent(), mutated)) { | ||
| ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); | ||
| ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation); | ||
| parsed = Suggestion.fromXContent(parser); | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this test doesn't need any field randomization I think. The suggest element is a object containing names Suggestions, which we already test elsewhere. I think it is not possible that we add anything other inside this object unless we change the whole structure of the xContent (which is another story).