Skip to content

Commit

Permalink
[Tests] Add tests for TermSuggestionBuilder#build() (elastic#25558)
Browse files Browse the repository at this point in the history
Adds a unit test that checks the TermSuggestionContext contents that is the result 
of TermSuggestionBuilder#build vs. the values the original builder contains.
  • Loading branch information
cbuescher authored Jul 7, 2017
1 parent 1f67d07 commit d71fece
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,15 @@ public void testBuild() throws IOException {
} else {
assertEquals("mapperServiceSearchAnalyzer", ((NamedAnalyzer) suggestionContext.getAnalyzer()).name());
}
assertSuggester(suggestionBuilder, suggestionContext);
}
}

/**
* put suggester dependent assertions in the sub type test
*/
protected abstract void assertSuggester(SB builder, SuggestionContext context);

protected MappedFieldType mockFieldType() {
return mock(MappedFieldType.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.index.mapper.CompletionFieldMapper.CompletionFieldType;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.search.suggest.AbstractSuggestionBuilderTestCase;
import org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext;
import org.elasticsearch.search.suggest.completion.context.CategoryQueryContext;
import org.elasticsearch.search.suggest.completion.context.ContextBuilder;
import org.elasticsearch.search.suggest.completion.context.ContextMapping;
Expand Down Expand Up @@ -150,4 +151,8 @@ protected MappedFieldType mockFieldType() {
completionFieldType.setContextMappings(new ContextMappings(contextMappings));
return completionFieldType;
}

@Override
protected void assertSuggester(CompletionSuggestionBuilder builder, SuggestionContext context) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.script.Script;
import org.elasticsearch.search.suggest.AbstractSuggestionBuilderTestCase;
import org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -184,4 +185,7 @@ public void testInvalidParameters() {
assertEquals("Pre and post tag must both be null or both not be null.", e.getMessage());
}

@Override
protected void assertSuggester(PhraseSuggestionBuilder builder, SuggestionContext context) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.search.suggest.AbstractSuggestionBuilderTestCase;
import org.elasticsearch.search.suggest.SortBy;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestionSearchContext.SuggestionContext;
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder.StringDistanceImpl;
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder.SuggestMode;

Expand All @@ -40,6 +41,7 @@
import static org.elasticsearch.search.suggest.DirectSpellcheckerSettings.DEFAULT_MIN_WORD_LENGTH;
import static org.elasticsearch.search.suggest.DirectSpellcheckerSettings.DEFAULT_PREFIX_LENGTH;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;

/**
* Test the {@link TermSuggestionBuilder} class.
Expand Down Expand Up @@ -223,4 +225,24 @@ public void testMalformedJson() {
assertThat(e.getMessage(), containsString("parsing failed"));
}
}

@Override
protected void assertSuggester(TermSuggestionBuilder builder, SuggestionContext context) {
assertThat(context, instanceOf(TermSuggestionContext.class));
assertThat(context.getSuggester(), instanceOf(TermSuggester.class));
TermSuggestionContext termSuggesterCtx = (TermSuggestionContext) context;
assertEquals(builder.accuracy(), termSuggesterCtx.getDirectSpellCheckerSettings().accuracy(), 0.0);
assertEquals(builder.maxTermFreq(), termSuggesterCtx.getDirectSpellCheckerSettings().maxTermFreq(), 0.0);
assertEquals(builder.minDocFreq(), termSuggesterCtx.getDirectSpellCheckerSettings().minDocFreq(), 0.0);
assertEquals(builder.maxEdits(), termSuggesterCtx.getDirectSpellCheckerSettings().maxEdits());
assertEquals(builder.maxInspections(), termSuggesterCtx.getDirectSpellCheckerSettings().maxInspections());
assertEquals(builder.minWordLength(), termSuggesterCtx.getDirectSpellCheckerSettings().minWordLength());
assertEquals(builder.prefixLength(), termSuggesterCtx.getDirectSpellCheckerSettings().prefixLength());
assertEquals(builder.prefixLength(), termSuggesterCtx.getDirectSpellCheckerSettings().prefixLength());
assertEquals(builder.suggestMode().toLucene(), termSuggesterCtx.getDirectSpellCheckerSettings().suggestMode());
assertEquals(builder.sort(), termSuggesterCtx.getDirectSpellCheckerSettings().sort());
// distance implementations don't implement equals() and have little to compare, so we only check class
assertEquals(builder.stringDistance().toLucene().getClass(),
termSuggesterCtx.getDirectSpellCheckerSettings().stringDistance().getClass());
}
}

0 comments on commit d71fece

Please sign in to comment.