Skip to content

Commit e9f7c40

Browse files
committed
cleanup mapper and type tests
1 parent 2d5bfb7 commit e9f7c40

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

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

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.lucene.store.Directory;
2222
import org.apache.lucene.tests.index.RandomIndexWriter;
2323
import org.elasticsearch.common.Strings;
24+
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.compress.CompressedXContent;
2526
import org.elasticsearch.core.CheckedConsumer;
2627
import org.elasticsearch.core.Nullable;
@@ -41,6 +42,7 @@
4142
import org.elasticsearch.xcontent.ToXContent;
4243
import org.elasticsearch.xcontent.XContentBuilder;
4344
import org.elasticsearch.xcontent.XContentParseException;
45+
import org.elasticsearch.xcontent.XContentType;
4446
import org.elasticsearch.xcontent.json.JsonXContent;
4547
import org.hamcrest.Matchers;
4648
import org.junit.AssumptionViolatedException;
@@ -57,6 +59,7 @@
5759
import static org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.NEW_SPARSE_VECTOR_INDEX_VERSION;
5860
import static org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.PREVIOUS_SPARSE_VECTOR_INDEX_VERSION;
5961
import static org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION;
62+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
6063
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
6164
import static org.hamcrest.Matchers.containsString;
6265
import static org.hamcrest.Matchers.equalTo;
@@ -230,54 +233,31 @@ public void testDefaults() throws Exception {
230233
assertTrue(freq1 < freq2);
231234
}
232235

233-
private void buildDocForSparseVectorFieldMapping(XContentBuilder b, CheckedConsumer<XContentBuilder, IOException> supplier)
234-
throws IOException {
235-
b.startObject("_doc");
236-
{
237-
b.startArray("dynamic_date_formats");
238-
{
239-
b.value("strict_date_optional_time||epoch_millis");
240-
b.value("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis");
241-
}
242-
b.endArray();
243-
244-
b.startArray("dynamic_templates");
245-
b.endArray();
246-
247-
b.field("date_detection", true);
248-
b.field("numeric_detection", false);
249-
250-
b.startObject("properties");
251-
{
252-
b.startObject("field");
253-
254-
supplier.accept(b);
255-
256-
b.endObject();
257-
}
258-
b.endObject();
259-
}
260-
b.endObject();
261-
};
262-
263236
public void testDefaultsWithAndWithoutIncludeDefaults() throws Exception {
264237
XContentBuilder orig = JsonXContent.contentBuilder().startObject();
265-
createMapperService(fieldMapping(this::minimalMapping)).documentMapper().mapping().toXContent(orig, INCLUDE_DEFAULTS);
238+
createMapperService(fieldMapping(this::minimalMapping)).mappingLookup().getMapper("field").toXContent(orig, INCLUDE_DEFAULTS);
266239
orig.endObject();
267240

268241
XContentBuilder withDefaults = JsonXContent.contentBuilder().startObject();
269-
buildDocForSparseVectorFieldMapping(withDefaults, this::minimalMappingWithExplicitDefaults);
242+
withDefaults.startObject("field");
243+
minimalMappingWithExplicitDefaults(withDefaults);
244+
withDefaults.endObject();
270245
withDefaults.endObject();
271246

272-
assertEquals(Strings.toString(withDefaults), Strings.toString(orig));
247+
assertToXContentEquivalent(BytesReference.bytes(withDefaults), BytesReference.bytes(orig), XContentType.JSON);
273248

274249
XContentBuilder origWithoutDefaults = JsonXContent.contentBuilder().startObject();
275-
createMapperService(fieldMapping(this::minimalMapping)).documentMapper()
276-
.mapping()
250+
createMapperService(fieldMapping(this::minimalMapping)).mappingLookup().getMapper("field")
277251
.toXContent(origWithoutDefaults, ToXContent.EMPTY_PARAMS);
278252
origWithoutDefaults.endObject();
279253

280-
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), Strings.toString(origWithoutDefaults));
254+
XContentBuilder withoutDefaults = JsonXContent.contentBuilder().startObject();
255+
withoutDefaults.startObject("field");
256+
minimalMapping(withoutDefaults);
257+
withoutDefaults.endObject();
258+
withoutDefaults.endObject();
259+
260+
assertToXContentEquivalent(BytesReference.bytes(withoutDefaults), BytesReference.bytes(origWithoutDefaults), XContentType.JSON);
281261
}
282262

283263
public void testDefaultsWithAndWithoutIncludeDefaultsOlderIndexVersion() throws Exception {
@@ -288,26 +268,30 @@ public void testDefaultsWithAndWithoutIncludeDefaultsOlderIndexVersion() throws
288268
);
289269

290270
XContentBuilder orig = JsonXContent.contentBuilder().startObject();
291-
createMapperService(indexVersion, fieldMapping(this::minimalMapping)).documentMapper().mapping().toXContent(orig, INCLUDE_DEFAULTS);
271+
createMapperService(indexVersion, fieldMapping(this::minimalMapping)).mappingLookup().getMapper("field")
272+
.toXContent(orig, INCLUDE_DEFAULTS);
292273
orig.endObject();
293274

294275
XContentBuilder withDefaults = JsonXContent.contentBuilder().startObject();
295-
buildDocForSparseVectorFieldMapping(withDefaults, this::minimalFieldMappingPreviousIndexVersion);
276+
withDefaults.startObject("field");
277+
minimalFieldMappingPreviousIndexVersion(withDefaults);
278+
withDefaults.endObject();
296279
withDefaults.endObject();
297280

298-
assertEquals(Strings.toString(withDefaults), Strings.toString(orig));
281+
assertToXContentEquivalent(BytesReference.bytes(withDefaults), BytesReference.bytes(orig), XContentType.JSON);
299282

300283
XContentBuilder origWithoutDefaults = JsonXContent.contentBuilder().startObject();
301-
createMapperService(indexVersion, fieldMapping(this::minimalMapping)).documentMapper()
302-
.mapping()
303-
.toXContent(origWithoutDefaults, INCLUDE_DEFAULTS);
284+
createMapperService(indexVersion, fieldMapping(this::minimalMapping)).mappingLookup().getMapper("field")
285+
.toXContent(origWithoutDefaults, ToXContent.EMPTY_PARAMS);
304286
origWithoutDefaults.endObject();
305287

306288
XContentBuilder withoutDefaults = JsonXContent.contentBuilder().startObject();
307-
buildDocForSparseVectorFieldMapping(withoutDefaults, this::minimalFieldMappingPreviousIndexVersion);
289+
withoutDefaults.startObject("field");
290+
minimalMapping(withoutDefaults);
291+
withoutDefaults.endObject();
308292
withoutDefaults.endObject();
309293

310-
assertEquals(Strings.toString(withoutDefaults), Strings.toString(origWithoutDefaults));
294+
assertToXContentEquivalent(BytesReference.bytes(withoutDefaults), BytesReference.bytes(origWithoutDefaults), XContentType.JSON);
311295
}
312296

313297
public void testMappingWithExplicitIndexOptions() throws Exception {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class SparseVectorFieldTypeTests extends FieldTypeTestCase {
2323
public void testDocValuesDisabled() {
2424
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(
2525
random(),
26-
IndexVersions.SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT,
26+
IndexVersions.NEW_SPARSE_VECTOR,
2727
IndexVersion.current()
2828
);
2929
MappedFieldType fieldType = new SparseVectorFieldMapper.SparseVectorFieldType(indexVersion, "field", false, Collections.emptyMap());
@@ -34,7 +34,7 @@ public void testDocValuesDisabled() {
3434
public void testIsNotAggregatable() {
3535
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(
3636
random(),
37-
IndexVersions.SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT,
37+
IndexVersions.NEW_SPARSE_VECTOR,
3838
IndexVersion.current()
3939
);
4040
MappedFieldType fieldType = new SparseVectorFieldMapper.SparseVectorFieldType(indexVersion, "field", false, Collections.emptyMap());

0 commit comments

Comments
 (0)