Skip to content

Commit a177f8a

Browse files
committed
fix mocking in unit tests
1 parent f33dabd commit a177f8a

File tree

7 files changed

+24
-5
lines changed

7 files changed

+24
-5
lines changed

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldTypeTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
import static org.mockito.Mockito.doReturn;
6161
import static org.mockito.Mockito.mock;
62+
import static org.mockito.Mockito.when;
6263

6364
public class MatchOnlyTextFieldTypeTests extends FieldTypeTestCase {
6465

@@ -315,6 +316,7 @@ public void testBlockLoaderDoesNotUseSyntheticSourceDelegateWhenIgnoreAboveIsSet
315316
// when
316317
MappedFieldType.BlockLoaderContext blContext = mock(MappedFieldType.BlockLoaderContext.class);
317318
doReturn(FieldNamesFieldMapper.FieldNamesFieldType.get(false)).when(blContext).fieldNames();
319+
when(blContext.indexSettings()).thenReturn(indexSettings);
318320
BlockLoader blockLoader = ft.blockLoader(blContext);
319321

320322
// then
@@ -362,6 +364,7 @@ public void testBlockLoaderDoesNotUseSyntheticSourceDelegateWhenIgnoreAboveIsSet
362364

363365
// when
364366
MappedFieldType.BlockLoaderContext blContext = mock(MappedFieldType.BlockLoaderContext.class);
367+
when(blContext.indexSettings()).thenReturn(indexSettings);
365368
doReturn(FieldNamesFieldMapper.FieldNamesFieldType.get(false)).when(blContext).fieldNames();
366369
BlockLoader blockLoader = ft.blockLoader(blContext);
367370

server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,8 +1446,9 @@ public void testEmpty() throws Exception {
14461446
throw new IllegalArgumentException("Can't load source in scripts in synthetic mode");
14471447
} : SourceProvider.fromLookup(mapperService.mappingLookup(), null, mapperService.getMapperMetrics().sourceFieldMetrics());
14481448
SearchLookup searchLookup = new SearchLookup(null, null, sourceProvider);
1449+
var indexSettings = mapperService.getIndexSettings();
14491450
IndexFieldData<?> sfd = ft.fielddataBuilder(
1450-
new FieldDataContext("", null, () -> searchLookup, Set::of, MappedFieldType.FielddataOperation.SCRIPT)
1451+
new FieldDataContext("", indexSettings, () -> searchLookup, Set::of, MappedFieldType.FielddataOperation.SCRIPT)
14511452
).build(null, null);
14521453
LeafFieldData lfd = sfd.load(getOnlyLeafReader(searcher.getIndexReader()).getContext());
14531454
TextDocValuesField scriptDV = (TextDocValuesField) lfd.getScriptFieldFactory("field");

server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import static org.hamcrest.Matchers.equalTo;
5656
import static org.mockito.Mockito.doReturn;
5757
import static org.mockito.Mockito.mock;
58+
import static org.mockito.Mockito.when;
5859

5960
public class TextFieldTypeTests extends FieldTypeTestCase {
6061

@@ -374,7 +375,9 @@ public void testBlockLoaderDoesNotUseSyntheticSourceDelegateWhenIgnoreAboveIsSet
374375
);
375376

376377
// when
377-
BlockLoader blockLoader = ft.blockLoader(mock(MappedFieldType.BlockLoaderContext.class));
378+
var context = mock(MappedFieldType.BlockLoaderContext.class);
379+
when(context.indexSettings()).thenReturn(indexSettings);
380+
BlockLoader blockLoader = ft.blockLoader(context);
378381

379382
// then
380383
// verify that we don't delegate anything
@@ -423,7 +426,9 @@ public void testBlockLoaderDoesNotUseSyntheticSourceDelegateWhenIgnoreAboveIsSet
423426
);
424427

425428
// when
426-
BlockLoader blockLoader = ft.blockLoader(mock(MappedFieldType.BlockLoaderContext.class));
429+
var context = mock(MappedFieldType.BlockLoaderContext.class);
430+
when(context.indexSettings()).thenReturn(indexSettings);
431+
BlockLoader blockLoader = ft.blockLoader(context);
427432

428433
// then
429434
// verify that we don't delegate anything

server/src/test/java/org/elasticsearch/index/mapper/flattened/KeyedFlattenedFieldTypeTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import org.apache.lucene.util.BytesRef;
1919
import org.elasticsearch.ElasticsearchException;
2020
import org.elasticsearch.common.lucene.search.AutomatonQueries;
21+
import org.elasticsearch.common.settings.Settings;
2122
import org.elasticsearch.common.unit.Fuzziness;
2223
import org.elasticsearch.index.mapper.FieldTypeTestCase;
2324
import org.elasticsearch.index.mapper.ValueFetcher;
2425
import org.elasticsearch.index.mapper.flattened.FlattenedFieldMapper.KeyedFlattenedFieldType;
2526
import org.elasticsearch.index.query.SearchExecutionContext;
2627
import org.elasticsearch.search.lookup.Source;
28+
import org.elasticsearch.test.IndexSettingsModule;
2729
import org.elasticsearch.xcontent.XContentType;
2830

2931
import java.io.IOException;
@@ -186,6 +188,7 @@ public void testFetchSourceValue() throws IOException {
186188
Map<String, Object> sourceValue = Map.of("key", "value");
187189

188190
SearchExecutionContext searchExecutionContext = mock(SearchExecutionContext.class);
191+
when(searchExecutionContext.getIndexSettings()).thenReturn(IndexSettingsModule.newIndexSettings("test", Settings.EMPTY));
189192
when(searchExecutionContext.isSourceEnabled()).thenReturn(true);
190193
when(searchExecutionContext.sourcePath("field.key")).thenReturn(Set.of("field.key"));
191194

server/src/test/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapperTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,6 +2403,7 @@ protected void assertFetch(MapperService mapperService, String field, Object val
24032403
MappedFieldType.FielddataOperation fdt = MappedFieldType.FielddataOperation.SEARCH;
24042404
SourceToParse source = source(b -> b.field(ft.name(), value));
24052405
SearchExecutionContext searchExecutionContext = mock(SearchExecutionContext.class);
2406+
when(searchExecutionContext.getIndexSettings()).thenReturn(mapperService.getIndexSettings());
24062407
when(searchExecutionContext.isSourceEnabled()).thenReturn(true);
24072408
when(searchExecutionContext.sourcePath(field)).thenReturn(Set.of(field));
24082409
when(searchExecutionContext.getForField(ft, fdt)).thenAnswer(inv -> fieldDataLookup(mapperService).apply(ft, () -> {

server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldFetcherTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Collections;
5151
import java.util.List;
5252
import java.util.Map;
53+
import java.util.Set;
5354
import java.util.function.BiFunction;
5455

5556
import static java.util.Collections.emptyMap;
@@ -1585,9 +1586,13 @@ public void testFetchMetadataFieldWithSourceDisabled() throws IOException {
15851586
}
15861587

15871588
public void testStoredFieldsSpec() throws IOException {
1589+
var mapperService = createMapperService();
15881590
List<FieldAndFormat> fields = List.of(new FieldAndFormat("field", null));
1589-
FieldFetcher fieldFetcher = FieldFetcher.create(newSearchExecutionContext(createMapperService()), fields);
1590-
assertEquals(StoredFieldsSpec.NEEDS_SOURCE, fieldFetcher.storedFieldsSpec());
1591+
FieldFetcher fieldFetcher = FieldFetcher.create(newSearchExecutionContext(mapperService), fields);
1592+
assertEquals(
1593+
StoredFieldsSpec.withSourcePaths(mapperService.getIndexSettings().getIgnoredSourceFormat(), Set.of("field")),
1594+
fieldFetcher.storedFieldsSpec()
1595+
);
15911596
}
15921597

15931598
private List<FieldAndFormat> fieldAndFormatList(String name, String format, boolean includeUnmapped) {

test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public static List<?> fetchSourceValue(MappedFieldType fieldType, Object sourceV
7777
public static List<?> fetchSourceValues(MappedFieldType fieldType, Object... values) throws IOException {
7878
String field = fieldType.name();
7979
SearchExecutionContext searchExecutionContext = mock(SearchExecutionContext.class);
80+
when(searchExecutionContext.getIndexSettings()).thenReturn(IndexSettingsModule.newIndexSettings("test", Settings.EMPTY));
8081
when(searchExecutionContext.isSourceEnabled()).thenReturn(true);
8182
when(searchExecutionContext.sourcePath(field)).thenReturn(Set.of(field));
8283

0 commit comments

Comments
 (0)