Skip to content

Conversation

@romseygeek
Copy link
Contributor

When we added scripts to long and double mapped fields, we added tests
for the general scripting infrastructure, and also specific tests for those two
field types. This commit extracts those type-specific tests out into a new base
test class that we can use when adding scripts to more field mappers.

@romseygeek romseygeek added >test Issues or PRs that are addressing/adding tests :Search Foundations/Mapping Index mappings, including merging and defining field types v8.0.0 v7.13.0 labels Apr 6, 2021
@romseygeek romseygeek requested review from javanna and nik9000 April 6, 2021 10:13
@romseygeek romseygeek self-assigned this Apr 6, 2021
@elasticmachine elasticmachine added the Team:Search Meta label for search team label Apr 6, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

@SuppressWarnings("unused")
public static final String[] PARAMETERS = {};

public static DoubleFieldScript.Factory factory(Consumer<DoubleFieldScript> executor) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method, in combination with making emit public, makes writing tests so much easier,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it help similarly if DoubleFieldScript implements an interface and tests didn't use the AbstractFieldScript infrastructure at all? Its kind of designed around incantations that work well in painless which isn't really a concern that the mapping infrastructure needs to have. Maybe it'd be easier to reason about with an interface and the tests just implementing that directly? I dunno.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at that, but this turns out to be simpler - the interface ends up basically duplicating all of the logic except construction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we end up executing the script in these tests? I was wondering if instead of taking a consumer of the field script, this method should take a supplier of field script, then the test would subclass the script class directly... maybe you have already tried it though. Also maybe this could be one method in the base mapper script test class with a generic type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we end up executing the script in these tests?

Yes - see for example testMultipleValues in LongScriptMapperTests

The reason I've done it this way is that the ceremony is all around construction, and returning a subclass doesn't help there because you still need to pass all the parameters to the super constructor. And you can't construct a generic class without knowing the type, so it can't be a method on the base test class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, can we then move these new static methods to their corresponding test classes, assuming they are only used there?

return randomBoolean() ? randomDoubleBetween(-Double.MAX_VALUE, Double.MAX_VALUE, true) : randomFloat();
}

public void testScriptAndPrecludedParameters() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were already on the long field tests but I think it's a good idea to have them here as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should they be in MapperScriptTestCase then? Like a final test method? I think we have a reasonable way to get the type being tested, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They only apply to double and long though. I'm sure there will be precluded params for date, keyword, etc, but they will be different. I may end up adding a getPrecludedParams method when I add the next implementations though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to keeping them separate then. I get blind to copy-and-paste differences so I try to avoid them if possible. But, shrug.

@nik9000
Copy link
Member

nik9000 commented Apr 6, 2021

commit message: duh a0b49f9

You sound like my daughter.

@romseygeek
Copy link
Contributor Author

You sound like my daughter.

She sounds like mine :)

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a couple of minor comments, LGTM otherwise

IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
assertEquals("docValuesType=SORTED_NUMERIC<field:4>", fields[0].toString());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could these three methods somehow be in the base test class, at least partially? what I am looking for is avoiding copy pasting when writing new tests, and possibly not forgetting to cover important scenarios.

return (T) compileScript(script.getIdOrCode());
}

public void testSerialization() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: call it testToXContent? I somehow think of serialization as the transport one, so I get confused with this naming.

assertThat(e.getMessage(), containsString("Cannot define script on field with index:false and doc_values:false"));
}

public void testScriptErrorParameterRequiresScript() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testOnScriptError etc. ?

@javanna javanna merged commit af3f0e5 into elastic:master Apr 8, 2021
javanna pushed a commit to javanna/elasticsearch that referenced this pull request Apr 8, 2021
When we added scripts to long and double mapped fields, we added tests
for the general scripting infrastructure, and also specific tests for those two
field types. This commit extracts those type-specific tests out into a new base
test class that we can use when adding scripts to more field mappers.
javanna added a commit that referenced this pull request Apr 8, 2021
When we added scripts to long and double mapped fields, we added tests
for the general scripting infrastructure, and also specific tests for those two
field types. This commit extracts those type-specific tests out into a new base
test class that we can use when adding scripts to more field mappers.

Co-authored-by: Alan Woodward <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Search Foundations/Mapping Index mappings, including merging and defining field types Team:Search Meta label for search team >test Issues or PRs that are addressing/adding tests v7.13.0 v8.0.0-alpha1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants