Skip to content

Commit 2041ca6

Browse files
committed
Convert ExpressionTests to a unit test.
1 parent 3b10d4c commit 2041ca6

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTests.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,51 @@
2020
package org.elasticsearch.script.expression;
2121

2222
import org.elasticsearch.common.settings.Settings;
23-
import org.elasticsearch.index.IndexService;
24-
import org.elasticsearch.index.query.QueryShardContext;
23+
import org.elasticsearch.index.fielddata.AtomicNumericFieldData;
24+
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
25+
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
26+
import org.elasticsearch.index.mapper.MapperService;
27+
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType;
28+
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
2529
import org.elasticsearch.script.ScriptException;
2630
import org.elasticsearch.script.SearchScript;
2731
import org.elasticsearch.search.lookup.SearchLookup;
28-
import org.elasticsearch.test.ESSingleNodeTestCase;
32+
import org.elasticsearch.test.ESTestCase;
2933

34+
import java.io.IOException;
3035
import java.text.ParseException;
3136
import java.util.Collections;
3237

33-
public class ExpressionTests extends ESSingleNodeTestCase {
34-
ExpressionScriptEngine service;
35-
SearchLookup lookup;
38+
import static org.mockito.Matchers.anyInt;
39+
import static org.mockito.Matchers.anyObject;
40+
import static org.mockito.Mockito.mock;
41+
import static org.mockito.Mockito.when;
42+
43+
public class ExpressionTests extends ESTestCase {
44+
private ExpressionScriptEngine service;
45+
private SearchLookup lookup;
3646

3747
@Override
3848
public void setUp() throws Exception {
3949
super.setUp();
40-
IndexService index = createIndex("test", Settings.EMPTY, "type", "d", "type=double");
50+
51+
NumberFieldType fieldType = new NumberFieldType(NumberType.DOUBLE);
52+
MapperService mapperService = mock(MapperService.class);
53+
when(mapperService.fullName("field")).thenReturn(fieldType);
54+
55+
SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
56+
when(doubleValues.advanceExact(anyInt())).thenReturn(true);
57+
when(doubleValues.nextValue()).thenReturn(2.718);
58+
59+
AtomicNumericFieldData atomicFieldData = mock(AtomicNumericFieldData.class);
60+
when(atomicFieldData.getDoubleValues()).thenReturn(doubleValues);
61+
62+
IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
63+
when(fieldData.getFieldName()).thenReturn("field");
64+
when(fieldData.load(anyObject())).thenReturn(atomicFieldData);
65+
4166
service = new ExpressionScriptEngine(Settings.EMPTY);
42-
QueryShardContext shardContext = index.newQueryShardContext(0, null, () -> 0, null);
43-
lookup = new SearchLookup(index.mapperService(), shardContext::getForField, null);
67+
lookup = new SearchLookup(mapperService, ignored -> fieldData, null);
4468
}
4569

4670
private SearchScript.LeafFactory compile(String expression) {
@@ -50,22 +74,30 @@ private SearchScript.LeafFactory compile(String expression) {
5074

5175
public void testNeedsScores() {
5276
assertFalse(compile("1.2").needs_score());
53-
assertFalse(compile("doc['d'].value").needs_score());
77+
assertFalse(compile("doc['field'].value").needs_score());
5478
assertTrue(compile("1/_score").needs_score());
55-
assertTrue(compile("doc['d'].value * _score").needs_score());
79+
assertTrue(compile("doc['field'].value * _score").needs_score());
5680
}
5781

5882
public void testCompileError() {
5983
ScriptException e = expectThrows(ScriptException.class, () -> {
60-
compile("doc['d'].value * *@#)(@$*@#$ + 4");
84+
compile("doc['field'].value * *@#)(@$*@#$ + 4");
6185
});
6286
assertTrue(e.getCause() instanceof ParseException);
6387
}
6488

6589
public void testLinkError() {
6690
ScriptException e = expectThrows(ScriptException.class, () -> {
67-
compile("doc['e'].value * 5");
91+
compile("doc['nonexistent'].value * 5");
6892
});
6993
assertTrue(e.getCause() instanceof ParseException);
7094
}
95+
96+
public void testFieldAccess() throws IOException {
97+
SearchScript script = compile("doc['field'].value").newInstance(null);
98+
script.setDocument(1);
99+
100+
double result = script.runAsDouble();
101+
assertEquals(2.718, result, 0.0);
102+
}
71103
}

0 commit comments

Comments
 (0)