From 870d63d0cd4cb004e8141292423401be00fa645e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 7 Jul 2017 12:53:06 +0200 Subject: [PATCH] [Tests] Add tests for PhraseSuggestionBuilder#build() (#25571) This adds a unit test that checks the PhraseSuggestionContext output of PhraseSuggestionBuilder#build. --- .../phrase/PhraseSuggestionBuilder.java | 8 +++- .../AbstractSuggestionBuilderTestCase.java | 5 ++- .../CompletionSuggesterBuilderTests.java | 14 ++++++- .../phrase/PhraseSuggestionBuilderTests.java | 38 +++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java index e6110ec28b1..9cad283c936 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilder.java @@ -311,6 +311,13 @@ public class PhraseSuggestionBuilder extends SuggestionBuilder> getCandidateGenerators() { + return this.generators; + } + /** * If set to true the phrase suggester will fail if the analyzer only * produces ngrams. the default it true. @@ -607,7 +614,6 @@ public class PhraseSuggestionBuilder extends SuggestionBuilder new NamedAnalyzer((String) invocation.getArguments()[0], AnalyzerScope.INDEX, new SimpleAnalyzer())); - when(scriptService.compile(any(Script.class), any())).thenReturn(mock(TemplateScript.Factory.class)); + when(scriptService.compile(any(Script.class), any())).then(invocation -> new TestTemplateService.MockTemplateScript.Factory( + ((Script) invocation.getArguments()[0]).getIdOrCode())); QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, mapperService, null, scriptService, xContentRegistry(), null, null, System::currentTimeMillis); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java index 68db5251593..dda9574c27b 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java @@ -46,7 +46,7 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe private static final Map> contextMap = new HashMap<>(); private static String categoryContextName; private static String geoQueryContextName; - private static List contextMappings; + private static List contextMappings = new ArrayList<>(); @Override protected CompletionSuggestionBuilder randomSuggestionBuilder() { @@ -66,6 +66,18 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe contextMappings = Arrays.asList(new ContextMapping[] { ContextBuilder.category(categoryContextName).build(), ContextBuilder.geo(geoQueryContextName).build() }); } + // lazy initialization of context names and mappings, cannot be done in some init method because other test + // also create random CompletionSuggestionBuilder instances + if (categoryContextName == null) { + categoryContextName = randomAlphaOfLength(10); + } + if (geoQueryContextName == null) { + geoQueryContextName = randomAlphaOfLength(10); + } + if (contextMappings.isEmpty()) { + contextMappings.add(ContextBuilder.category(categoryContextName).build()); + contextMappings.add(ContextBuilder.geo(geoQueryContextName).build()); + } CompletionSuggestionBuilder testBuilder = new CompletionSuggestionBuilder(randomAlphaOfLengthBetween(2, 20)); setCommonPropertiesOnRandomBuilder(testBuilder); switch (randomIntBetween(0, 3)) { diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java index 26a008d756c..4bfc7a1c8c9 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionBuilderTests.java @@ -27,6 +27,8 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import static org.hamcrest.Matchers.instanceOf; + public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestCase { @Override protected PhraseSuggestionBuilder randomSuggestionBuilder() { @@ -187,5 +189,41 @@ public class PhraseSuggestionBuilderTests extends AbstractSuggestionBuilderTestC @Override protected void assertSuggester(PhraseSuggestionBuilder builder, SuggestionContext context) { + assertThat(context, instanceOf(PhraseSuggestionContext.class)); + assertThat(context.getSuggester(), instanceOf(PhraseSuggester.class)); + PhraseSuggestionContext phraseSuggesterCtx = (PhraseSuggestionContext) context; + assertOptionalEquals(builder.confidence(), phraseSuggesterCtx.confidence(), PhraseSuggestionContext.DEFAULT_CONFIDENCE); + assertOptionalEquals(builder.collatePrune(), phraseSuggesterCtx.collatePrune(), PhraseSuggestionContext.DEFAULT_COLLATE_PRUNE); + assertEquals(builder.separator(), phraseSuggesterCtx.separator().utf8ToString()); + assertOptionalEquals(builder.realWordErrorLikelihood(), phraseSuggesterCtx.realworldErrorLikelyhood(), + PhraseSuggestionContext.DEFAULT_RWE_ERRORLIKELIHOOD); + assertOptionalEquals(builder.maxErrors(), phraseSuggesterCtx.maxErrors(), PhraseSuggestionContext.DEFAULT_MAX_ERRORS); + assertOptionalEquals(builder.forceUnigrams(), phraseSuggesterCtx.getRequireUnigram(), + PhraseSuggestionContext.DEFAULT_REQUIRE_UNIGRAM); + assertOptionalEquals(builder.tokenLimit(), phraseSuggesterCtx.getTokenLimit(), NoisyChannelSpellChecker.DEFAULT_TOKEN_LIMIT); + assertEquals(builder.preTag(), phraseSuggesterCtx.getPreTag() != null ? phraseSuggesterCtx.getPreTag().utf8ToString() : null); + assertEquals(builder.postTag(), phraseSuggesterCtx.getPostTag() != null ? phraseSuggesterCtx.getPostTag().utf8ToString() : null); + assertOptionalEquals(builder.gramSize(), phraseSuggesterCtx.gramSize(), PhraseSuggestionContext.DEFAULT_GRAM_SIZE); + if (builder.collateQuery() != null) { + assertEquals(builder.collateQuery().getIdOrCode(), phraseSuggesterCtx.getCollateQueryScript().newInstance(null).execute()); + } + if (builder.collateParams() != null) { + assertEquals(builder.collateParams(), phraseSuggesterCtx.getCollateScriptParams()); + } + if (builder.smoothingModel() != null) { + assertEquals(builder.smoothingModel().buildWordScorerFactory().getClass(), phraseSuggesterCtx.model().getClass()); + } + if (builder.getCandidateGenerators().isEmpty() == false) { + // currently, "direct_generator" is the only one available. Only compare size of the lists + assertEquals(builder.getCandidateGenerators().get("direct_generator").size(), phraseSuggesterCtx.generators().size()); + } + } + + private static void assertOptionalEquals(T optional, T actual, T defaultValue) { + if (optional != null) { + assertEquals(optional, actual); + } else { + assertEquals(defaultValue, actual); + } } }