Skip to content

Commit bd88335

Browse files
committed
Refactor tests
1 parent b2de161 commit bd88335

File tree

3 files changed

+37
-65
lines changed

3 files changed

+37
-65
lines changed

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/LuceneQueryEvaluatorTests.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.compute.data.BytesRefVector;
3030
import org.elasticsearch.compute.data.DocBlock;
3131
import org.elasticsearch.compute.data.DoubleBlock;
32+
import org.elasticsearch.compute.data.DoubleVector;
3233
import org.elasticsearch.compute.data.ElementType;
3334
import org.elasticsearch.compute.data.Page;
3435
import org.elasticsearch.compute.data.Vector;
@@ -71,7 +72,7 @@ public void testDenseCollectorSmall() throws IOException {
7172
collector.finish();
7273
try (T result = (T) collector.build()) {
7374
for (int i = 0; i <= 2; i++) {
74-
assertEquals(getValueAt(result, i), valueForMatch());
75+
assertCollectedResultMatch(result, i, true);
7576
}
7677
}
7778
}
@@ -86,7 +87,7 @@ public void testDenseCollectorSimple() throws IOException {
8687
collector.finish();
8788
try (T result = (T) collector.build()) {
8889
for (int i = 0; i < 11; i++) {
89-
assertEquals(getValueAt(result, i), i == 2 || i == 5 ? valueForMatch() : valueForNoMatch());
90+
assertCollectedResultMatch(result, i, i == 2 || i == 5);
9091
}
9192
}
9293
}
@@ -109,12 +110,22 @@ public void testDenseCollector() throws IOException {
109110
collector.finish();
110111
try (T result = (T) collector.build()) {
111112
for (int i = 0; i < length; i++) {
112-
assertEquals(getValueAt(result, i), expected[i] ? valueForMatch() : valueForNoMatch());
113+
assertCollectedResultMatch(result, i, expected[i]);
113114
}
114115
}
115116
}
116117
}
117118

119+
/**
120+
* Create a dense collector for the given range.
121+
*/
122+
protected abstract LuceneQueryEvaluator.DenseCollector<U> createDenseCollector(int min, int max);
123+
124+
/**
125+
* Chceks that the collected results at the given position corresponds to a match or no match
126+
*/
127+
protected abstract void assertCollectedResultMatch(T resultVector, int position, boolean isMatch);
128+
118129
public void testTermQuery() throws IOException {
119130
Set<String> values = values();
120131
String term = values.iterator().next();
@@ -162,7 +173,7 @@ protected void assertTermsQuery(List<Page> results, Set<String> matching, int ex
162173
for (int i = 0; i < page.getPositionCount(); i++) {
163174
BytesRef termAtPosition = terms.getBytesRef(i, new BytesRef());
164175
boolean isMatch = matching.contains(termAtPosition.utf8ToString());
165-
assertResultMatch(resultVector, i, isMatch);
176+
assertTermResultMatch(resultVector, i, isMatch);
166177
if (isMatch) {
167178
matchCount++;
168179
}
@@ -171,7 +182,10 @@ protected void assertTermsQuery(List<Page> results, Set<String> matching, int ex
171182
assertThat(matchCount, equalTo(expectedMatchCount));
172183
}
173184

174-
protected abstract void assertResultMatch(T resultVector, int position, boolean isMatch);
185+
/**
186+
* Checks that the result at the given position corresponds to a term match or no match
187+
*/
188+
protected abstract void assertTermResultMatch(T resultVector, int position, boolean isMatch);
175189

176190
private List<Page> runQuery(Set<String> values, Query query, boolean shuffleDocs) throws IOException {
177191
DriverContext driverContext = driverContext();
@@ -278,31 +292,11 @@ private static LuceneOperator.Factory luceneOperatorFactory(IndexReader reader,
278292
// Returns the block index for the results to check
279293
protected abstract int resultsBlockIndex(Page page);
280294

281-
/**
282-
* Create a dense collector for the given range.
283-
*/
284-
protected abstract LuceneQueryEvaluator.DenseCollector<U> createDenseCollector(int min, int max);
285-
286295
/**
287296
* Returns a test scorer to use for scoring docs. Can be null
288297
*/
289298
protected abstract Scorable getScorer();
290299

291-
/**
292-
* Retrieves the value at a given index from the vector. Need to do this as Vector does not export a generic get() method
293-
*/
294-
protected abstract Object getValueAt(T vector, int i);
295-
296-
/**
297-
* Value that should be returned for a matching doc from the resulting vector
298-
*/
299-
protected abstract Object valueForMatch();
300-
301-
/**
302-
* Value that should be returned for a non-matching doc from the resulting vector
303-
*/
304-
protected abstract Object valueForNoMatch();
305-
306300
/**
307301
* Create the operator to test
308302
*/

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/LuceneQueryExpressionEvaluatorTests.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@
88
package org.elasticsearch.compute.lucene;
99

1010
import org.apache.lucene.search.Scorable;
11-
import org.apache.lucene.util.BytesRef;
1211
import org.elasticsearch.compute.data.BlockFactory;
13-
import org.elasticsearch.compute.data.BooleanBlock;
1412
import org.elasticsearch.compute.data.BooleanVector;
15-
import org.elasticsearch.compute.data.BytesRefBlock;
16-
import org.elasticsearch.compute.data.BytesRefVector;
1713
import org.elasticsearch.compute.data.Page;
1814
import org.elasticsearch.compute.lucene.LuceneQueryEvaluator.DenseCollector;
1915
import org.elasticsearch.compute.operator.EvalOperator;
2016
import org.elasticsearch.compute.operator.Operator;
2117

22-
import java.util.List;
23-
import java.util.Set;
24-
2518
import static org.hamcrest.Matchers.equalTo;
2619

2720
public class LuceneQueryExpressionEvaluatorTests extends LuceneQueryEvaluatorTests<BooleanVector, BooleanVector.Builder> {
@@ -43,21 +36,6 @@ protected Scorable getScorer() {
4336
return null;
4437
}
4538

46-
@Override
47-
protected Object getValueAt(BooleanVector vector, int i) {
48-
return vector.getBoolean(i);
49-
}
50-
51-
@Override
52-
protected Object valueForMatch() {
53-
return true;
54-
}
55-
56-
@Override
57-
protected Object valueForNoMatch() {
58-
return false;
59-
}
60-
6139
@Override
6240
protected Operator createOperator(BlockFactory blockFactory, LuceneQueryEvaluator.ShardConfig[] shards) {
6341
return new EvalOperator(blockFactory, new LuceneQueryExpressionEvaluator(
@@ -78,7 +56,12 @@ protected int resultsBlockIndex(Page page) {
7856
}
7957

8058
@Override
81-
protected void assertResultMatch(BooleanVector resultVector, int position, boolean isMatch) {
59+
protected void assertCollectedResultMatch(BooleanVector resultVector, int position, boolean isMatch) {
60+
assertThat(resultVector.getBoolean(position), equalTo(isMatch));
61+
}
62+
63+
@Override
64+
protected void assertTermResultMatch(BooleanVector resultVector, int position, boolean isMatch) {
8265
assertThat(resultVector.getBoolean(position), equalTo(isMatch));
8366
}
8467
}

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/LuceneQueryScoreEvaluatorTests.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,6 @@ public float score() throws IOException {
4545
};
4646
}
4747

48-
@Override
49-
protected Object getValueAt(DoubleVector vector, int i) {
50-
return vector.getDouble(i);
51-
}
52-
53-
@Override
54-
protected Object valueForMatch() {
55-
return (double) TEST_SCORE;
56-
}
57-
58-
@Override
59-
protected Object valueForNoMatch() {
60-
return NO_MATCH_SCORE;
61-
}
62-
6348
@Override
6449
protected Operator createOperator(BlockFactory blockFactory, LuceneQueryEvaluator.ShardConfig[] shards) {
6550
return new ScoreOperator(blockFactory, new LuceneQueryScoreEvaluator(blockFactory, shards), 1);
@@ -72,12 +57,22 @@ protected boolean usesScoring() {
7257

7358
@Override
7459
protected int resultsBlockIndex(Page page) {
75-
// Uses the score block
60+
// Reuses the score block
7661
return 1;
7762
}
7863

7964
@Override
80-
protected void assertResultMatch(DoubleVector resultVector, int position, boolean isMatch) {
65+
protected void assertCollectedResultMatch(DoubleVector resultVector, int position, boolean isMatch) {
66+
if (isMatch) {
67+
assertThat(resultVector.getDouble(position), equalTo((double)TEST_SCORE));
68+
} else {
69+
// All docs have a default score coming from Lucene
70+
assertThat(resultVector.getDouble(position), equalTo(NO_MATCH_SCORE));
71+
}
72+
}
73+
74+
@Override
75+
protected void assertTermResultMatch(DoubleVector resultVector, int position, boolean isMatch) {
8176
if (isMatch) {
8277
assertThat(resultVector.getDouble(position), greaterThan(DEFAULT_SCORE));
8378
} else {

0 commit comments

Comments
 (0)