Skip to content

Commit 19f9746

Browse files
committed
Unit tests
1 parent c0a65ba commit 19f9746

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@
5050
import org.elasticsearch.index.fielddata.LeafFieldData;
5151
import org.elasticsearch.index.fielddata.ScriptDocValues;
5252
import org.elasticsearch.index.fielddata.plain.AbstractLeafOrdinalsFieldData;
53+
import org.elasticsearch.index.mapper.FieldMapper;
5354
import org.elasticsearch.index.mapper.IndexFieldMapper;
5455
import org.elasticsearch.index.mapper.KeywordFieldMapper;
5556
import org.elasticsearch.index.mapper.MappedFieldType;
5657
import org.elasticsearch.index.mapper.Mapper;
58+
import org.elasticsearch.index.mapper.Mapper.BuilderContext;
59+
import org.elasticsearch.index.mapper.Mapper.TypeParser;
5760
import org.elasticsearch.index.mapper.MapperService;
5861
import org.elasticsearch.index.mapper.NumberFieldMapper;
62+
import org.elasticsearch.index.mapper.ParseContext;
5963
import org.elasticsearch.index.mapper.TextFieldMapper;
64+
import org.elasticsearch.index.mapper.TextSearchInfo;
65+
import org.elasticsearch.index.mapper.ValueFetcher;
6066
import org.elasticsearch.indices.IndicesModule;
67+
import org.elasticsearch.plugins.MapperPlugin;
6168
import org.elasticsearch.search.lookup.LeafDocLookup;
6269
import org.elasticsearch.search.lookup.LeafSearchLookup;
6370
import org.elasticsearch.search.lookup.SearchLookup;
@@ -66,8 +73,10 @@
6673
import java.io.IOException;
6774
import java.util.ArrayList;
6875
import java.util.Collections;
76+
import java.util.HashMap;
6977
import java.util.List;
7078
import java.util.Map;
79+
import java.util.Set;
7180
import java.util.function.BiFunction;
7281
import java.util.function.Supplier;
7382

@@ -305,12 +314,20 @@ public void testFielddataLookupOneFieldManyReferences() throws IOException {
305314
}
306315

307316
public void testRuntimeFields() throws IOException {
308-
MapperService mapperService = mockMapperService("test");
309-
Map<String, Object> runtimeFields = Map.ofEntries(
310-
Map.entry("cat", Map.of("type", "keyword", "script", "emit('cat')")),
311-
Map.entry("dog", Map.of("type", "keyword", "script", "emit('dog')")),
312-
Map.entry("catdog", Map.of("type", "keyword", "script", "emit(doc['cat'].value + doc['dog'].value)"))
313-
);
317+
MapperService mapperService = mockMapperService("test", List.of(new MapperPlugin() {
318+
@Override
319+
public Map<String, TypeParser> getMappers() {
320+
return Map.of("runtime", (name, node, parserContext) -> new Mapper.Builder(name) {
321+
@Override
322+
public Mapper build(BuilderContext context) {
323+
return new DummyMapper(name, new DummyMappedFieldType(name));
324+
}
325+
});
326+
}
327+
}));
328+
Map<String, Object> runtimeFields = new HashMap<>();
329+
runtimeFields.put("cat", new HashMap<>(Map.of("type", "keyword")));
330+
runtimeFields.put("dog", new HashMap<>(Map.of("type", "keyword")));
314331
QueryShardContext qsc = new QueryShardContext(
315332
0,
316333
mapperService.getIndexSettings(),
@@ -321,7 +338,7 @@ public void testRuntimeFields() throws IOException {
321338
null,
322339
null,
323340
NamedXContentRegistry.EMPTY,
324-
new NamedWriteableRegistry(Collections.emptyList()),
341+
new NamedWriteableRegistry(List.of()),
325342
null,
326343
null,
327344
() -> 0,
@@ -332,7 +349,12 @@ public void testRuntimeFields() throws IOException {
332349
runtimeFields
333350
);
334351
assertTrue(qsc.isFieldMapped("cat"));
335-
assertThat(qsc.getFieldType(name))
352+
assertThat(qsc.getFieldType("cat"), instanceOf(DummyMappedFieldType.class));
353+
assertThat(qsc.simpleMatchToIndexNames("cat"), equalTo(Set.of("cat")));
354+
assertTrue(qsc.isFieldMapped("dog"));
355+
assertThat(qsc.getFieldType("dog"), instanceOf(DummyMappedFieldType.class));
356+
assertThat(qsc.simpleMatchToIndexNames("dog"), equalTo(Set.of("dog")));
357+
assertThat(qsc.simpleMatchToIndexNames("*"), equalTo(Set.of("cat", "dog")));
336358
}
337359

338360
public static QueryShardContext createQueryShardContext(String indexUuid, String clusterAlias) {
@@ -344,7 +366,7 @@ private static QueryShardContext createQueryShardContext(
344366
String clusterAlias,
345367
TriFunction<String, LeafSearchLookup, Integer, String> runtimeDocValues
346368
) {
347-
MapperService mapperService = mockMapperService(indexUuid);
369+
MapperService mapperService = mockMapperService(indexUuid, List.of());
348370
if (runtimeDocValues != null) {
349371
when(mapperService.fieldType(any())).thenAnswer(fieldTypeInv -> {
350372
String fieldName = (String)fieldTypeInv.getArguments()[0];
@@ -359,7 +381,7 @@ mapperService, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegist
359381
null, null, () -> nowInMillis, clusterAlias, null, () -> true, null);
360382
}
361383

362-
private static MapperService mockMapperService(String indexUuid) {
384+
private static MapperService mockMapperService(String indexUuid, List<MapperPlugin> mapperPlugins) {
363385
IndexMetadata.Builder indexMetadataBuilder = new IndexMetadata.Builder("index");
364386
indexMetadataBuilder.settings(Settings.builder().put("index.version.created", Version.CURRENT)
365387
.put("index.number_of_shards", 1)
@@ -377,7 +399,7 @@ private static MapperService mockMapperService(String indexUuid) {
377399
when(mapperService.getIndexSettings()).thenReturn(indexSettings);
378400
when(mapperService.index()).thenReturn(indexMetadata.getIndex());
379401
when(mapperService.getIndexAnalyzers()).thenReturn(indexAnalyzers);
380-
Map<String, Mapper.TypeParser> typeParserMap = IndicesModule.getMappers(Collections.emptyList());
402+
Map<String, Mapper.TypeParser> typeParserMap = IndicesModule.getMappers(mapperPlugins);
381403
Mapper.TypeParser.ParserContext parserContext = new Mapper.TypeParser.ParserContext(name -> null, typeParserMap::get,
382404
Version.CURRENT, () -> null, null, null, mapperService.getIndexAnalyzers(), mapperService.getIndexSettings(),
383405
() -> {
@@ -482,4 +504,45 @@ public void collect(int doc) throws IOException {
482504
}
483505
}
484506

507+
private static class DummyMapper extends FieldMapper {
508+
protected DummyMapper(String simpleName, MappedFieldType mappedFieldType) {
509+
super(simpleName, mappedFieldType, Map.of(), MultiFields.empty(), CopyTo.empty());
510+
}
511+
512+
@Override
513+
protected void parseCreateField(ParseContext context) throws IOException {
514+
throw new UnsupportedOperationException();
515+
}
516+
517+
@Override
518+
public Builder getMergeBuilder() {
519+
throw new UnsupportedOperationException();
520+
}
521+
522+
@Override
523+
protected String contentType() {
524+
throw new UnsupportedOperationException();
525+
}
526+
}
527+
528+
private static class DummyMappedFieldType extends MappedFieldType {
529+
public DummyMappedFieldType(String name) {
530+
super(name, true, false, true, TextSearchInfo.SIMPLE_MATCH_ONLY, null);
531+
}
532+
533+
@Override
534+
public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) {
535+
throw new UnsupportedOperationException();
536+
}
537+
538+
@Override
539+
public String typeName() {
540+
return "runtime";
541+
}
542+
543+
@Override
544+
public Query termQuery(Object value, QueryShardContext context) {
545+
throw new UnsupportedOperationException();
546+
}
547+
}
485548
}

x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/10_keyword.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ setup:
368368
index: sensor
369369
body:
370370
runtime_mappings:
371-
voltage.rating:
371+
voltage_rating:
372372
type: keyword
373373
script: |
374374
double v = doc['voltage'].value;
@@ -379,11 +379,11 @@ setup:
379379
} else {
380380
emit('ok');
381381
}
382-
fields: [voltage.rating]
382+
fields: [voltage_rating]
383383
sort: timestamp
384384
- match: {hits.total.value: 6}
385385
- match: {hits.hits.0._source.voltage: 4.0}
386-
- match: {hits.hits.0.fields.voltage.rating: low}
386+
- match: {hits.hits.0.fields.voltage_rating: [low]}
387387

388388

389389
---

0 commit comments

Comments
 (0)