simplify mocking field type in SuggestionBuilderTests

This commit is contained in:
Areek Zillur 2016-03-09 11:37:15 -05:00
parent 55c58c56a8
commit e7cffa5e9f
2 changed files with 26 additions and 37 deletions

View File

@ -213,20 +213,12 @@ public abstract class AbstractSuggestionBuilderTestCase<SB extends SuggestionBui
}
}
protected Tuple<MapperService, SB> mockMapperServiceAndSuggestionBuilder(
IndexSettings idxSettings, AnalysisService mockAnalysisService, SB suggestBuilder) {
final MapperService mapperService = new MapperService(idxSettings, mockAnalysisService, null,
new IndicesModule().getMapperRegistry(), null) {
@Override
public MappedFieldType fullName(String fullName) {
StringFieldType type = new StringFieldType();
if (randomBoolean()) {
type.setSearchAnalyzer(new NamedAnalyzer("foo", new WhitespaceAnalyzer()));
}
return type;
}
};
return new Tuple<>(mapperService, suggestBuilder);
protected Tuple<MappedFieldType, SB> randomFieldTypeAndSuggestionBuilder() {
StringFieldType type = new StringFieldType();
if (randomBoolean()) {
type.setSearchAnalyzer(new NamedAnalyzer("foo", new WhitespaceAnalyzer()));
}
return new Tuple<>(type, randomTestBuilder());
}
/**
@ -246,12 +238,17 @@ public abstract class AbstractSuggestionBuilderTestCase<SB extends SuggestionBui
for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
SuggestBuilder suggestBuilder = new SuggestBuilder();
SB suggestionBuilder = randomTestBuilder();
Tuple<MapperService, SB> mapperServiceSBTuple =
mockMapperServiceAndSuggestionBuilder(idxSettings, mockAnalysisService, suggestionBuilder);
suggestionBuilder = mapperServiceSBTuple.v2();
final Tuple<MappedFieldType, SB> mappedFieldTypeSBTuple = randomFieldTypeAndSuggestionBuilder();
final MapperService mapperService = new MapperService(idxSettings, mockAnalysisService, null,
new IndicesModule().getMapperRegistry(), null) {
@Override
public MappedFieldType fullName(String fullName) {
return mappedFieldTypeSBTuple.v1();
}
};
SB suggestionBuilder = mappedFieldTypeSBTuple.v2();
QueryShardContext mockShardContext = new QueryShardContext(idxSettings,
null, null, mapperServiceSBTuple.v1(), null, scriptService, null) {
null, null, mapperService, null, scriptService, null) {
@Override
public MappedFieldType fieldMapper(String name) {
StringFieldMapper.Builder builder = new StringFieldMapper.Builder(name);

View File

@ -122,25 +122,17 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe
}
@Override
protected Tuple<MapperService, CompletionSuggestionBuilder> mockMapperServiceAndSuggestionBuilder(
IndexSettings idxSettings, AnalysisService mockAnalysisService, CompletionSuggestionBuilder suggestBuilder) {
protected Tuple<MappedFieldType, CompletionSuggestionBuilder> randomFieldTypeAndSuggestionBuilder() {
final BuilderAndInfo builderAndInfo = randomSuggestionBuilderWithContextInfo();
final MapperService mapperService = new MapperService(idxSettings, mockAnalysisService, null,
new IndicesModule().getMapperRegistry(), null) {
@Override
public MappedFieldType fullName(String fullName) {
CompletionFieldMapper.CompletionFieldType type = new CompletionFieldMapper.CompletionFieldType();
List<ContextMapping> contextMappings = builderAndInfo.catContexts.stream()
.map(catContext -> new CategoryContextMapping.Builder(catContext).build())
.collect(Collectors.toList());
contextMappings.addAll(builderAndInfo.geoContexts.stream()
.map(geoContext -> new GeoContextMapping.Builder(geoContext).build())
.collect(Collectors.toList()));
type.setContextMappings(new ContextMappings(contextMappings));
return type;
}
};
return new Tuple<>(mapperService, builderAndInfo.builder);
CompletionFieldMapper.CompletionFieldType type = new CompletionFieldMapper.CompletionFieldType();
List<ContextMapping> contextMappings = builderAndInfo.catContexts.stream()
.map(catContext -> new CategoryContextMapping.Builder(catContext).build())
.collect(Collectors.toList());
contextMappings.addAll(builderAndInfo.geoContexts.stream()
.map(geoContext -> new GeoContextMapping.Builder(geoContext).build())
.collect(Collectors.toList()));
type.setContextMappings(new ContextMappings(contextMappings));
return new Tuple<>(type, builderAndInfo.builder);
}
@Override