Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammad Qureshi <[email protected]>
  • Loading branch information
qreshi committed Mar 20, 2024
1 parent b41f6ed commit 16ef291
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ protected boolean supportsOrIgnoresBoost() {
return false;
}

protected boolean supportsMeta() {
return false;
}

// Overriding fieldMapping to make it create derived mappings by default.
// This way, the parent tests are checking the right behavior for this Mapper.
@Override
Expand All @@ -43,8 +47,11 @@ protected void registerParameters(ParameterChecker checker) throws IOException {
// supported for DerivedFieldMapper (we explicitly set these values on initialization)
}

// TESTCASE: testDefaults() (no script provided and assuming field is not in the source either)
// TODO: Might not need this (no default that really deviates it from testSerialization()
// TODO: Can update this once the query implementation is completed
// This is also being left blank because the super assertExistsQuery is trying to parse
// an empty source and fails.
@Override
protected void assertExistsQuery(MapperService mapperService) {}

public void testSerialization() throws IOException {

Expand All @@ -60,10 +67,12 @@ public void testSerialization() throws IOException {

public void testParsesScript() throws IOException {

String scriptString = "{\"source\": \"doc['test'].value\"}";
String scriptString = "doc['test'].value";
DocumentMapper defaultMapper = createDocumentMapper(fieldMapping(b -> {
b.field("type", "keyword");
b.field("script", scriptString);
b.startObject("script");
b.field("source", scriptString);
b.endObject();
}));
Mapper mapper = defaultMapper.mappers().getMapper("field");
assertTrue(mapper instanceof DerivedFieldMapper);
Expand All @@ -73,11 +82,11 @@ public void testParsesScript() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
mapper.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
assertEquals("{\"type\": \"keyword\",\"script\": {\"source\": \"emit(doc['test'].value)\"}}", builder.toString());
assertEquals("{\"field\":{\"type\":\"keyword\",\"script\":{\"source\":\"doc['test'].value\",\"lang\":\"painless\"}}}", builder.toString());
}

// TODO: Is there a case where we want to allow the field to be defined in both 'derived' and 'properties'?
// If the user wants to move a field they were testing as derived to be indexed they should be able to update
// If the user wants to move a field they were testing as derived to be indexed, they should be able to update
// the mappings in index template to move the field from 'derived' to 'properties' and it should take affect
// during the next index rollover (so even in this case, the field is only defined in one or the other).
public void testFieldInDerivedAndProperties() throws IOException {
Expand All @@ -96,8 +105,11 @@ public void testFieldInDerivedAndProperties() throws IOException {
b.endObject();
}))
);
// TODO: Do we want to handle this as a different error? As it stands, it fails as a merge conflict which makes sense.
// If it didn't fail here, it would hit the MapperParsingException for the field being defined more than once
// when MappingLookup is initialized
assertEquals(
"Field [field] is defined more than once",
"Failed to parse mapping [_doc]: mapper [field] cannot be changed from type [derived_field] to [keyword]",
ex.getMessage()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ public void testDerivedFields() throws Exception {
.endObject()
.startObject("derived_field_name2")
.field("type", "keyword")
.field("script", "{\"source\": \"doc['test'].value\"}")
.startObject("script")
.field("source", "doc['test'].value")
.endObject()
.endObject()
.endObject()
.startObject("properties")
Expand All @@ -478,7 +480,7 @@ public void testDerivedFields() throws Exception {
assertTrue(mapper instanceof DerivedFieldMapper);
derivedFieldMapper = (DerivedFieldMapper) mapper;
assertEquals("keyword", derivedFieldMapper.getType());
assertEquals(Script.parse("{\"source\": \"doc['test'].value\"}"), derivedFieldMapper.getScript());
assertEquals(Script.parse("doc['test'].value"), derivedFieldMapper.getScript());

// Check that field in properties was parsed correctly as well
mapper = documentMapper.root().getMapper("field_name");
Expand Down

0 comments on commit 16ef291

Please sign in to comment.