Skip to content

Commit 50f9e06

Browse files
authored
ESQL: Pluggable tests for Operator status (#132876)
Makes it possible to plug in tests for operator status. The `CollectOperator` has a fairly "unique" status so we need the ability to modify the tests.
1 parent 581da4d commit 50f9e06

File tree

12 files changed

+79
-47
lines changed

12 files changed

+79
-47
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.elasticsearch.compute.data.Page;
2222
import org.elasticsearch.compute.operator.Driver;
2323
import org.elasticsearch.compute.operator.DriverContext;
24-
import org.elasticsearch.compute.test.AnyOperatorTestCase;
2524
import org.elasticsearch.compute.test.OperatorTestCase;
25+
import org.elasticsearch.compute.test.SourceOperatorTestCase;
2626
import org.elasticsearch.compute.test.TestDriverFactory;
2727
import org.elasticsearch.compute.test.TestResultPageSinkOperator;
2828
import org.elasticsearch.core.IOUtils;
@@ -41,7 +41,7 @@
4141
import static org.hamcrest.Matchers.lessThanOrEqualTo;
4242
import static org.hamcrest.Matchers.matchesRegex;
4343

44-
public class LuceneCountOperatorTests extends AnyOperatorTestCase {
44+
public class LuceneCountOperatorTests extends SourceOperatorTestCase {
4545
private Directory directory = newDirectory();
4646
private IndexReader reader;
4747

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import org.elasticsearch.compute.data.Page;
2323
import org.elasticsearch.compute.operator.Driver;
2424
import org.elasticsearch.compute.operator.DriverContext;
25-
import org.elasticsearch.compute.test.AnyOperatorTestCase;
2625
import org.elasticsearch.compute.test.OperatorTestCase;
26+
import org.elasticsearch.compute.test.SourceOperatorTestCase;
2727
import org.elasticsearch.compute.test.TestDriverFactory;
2828
import org.elasticsearch.compute.test.TestResultPageSinkOperator;
2929
import org.elasticsearch.core.IOUtils;
@@ -43,7 +43,7 @@
4343
import static org.hamcrest.Matchers.lessThanOrEqualTo;
4444
import static org.hamcrest.Matchers.matchesRegex;
4545

46-
public abstract class LuceneMaxOperatorTestCase extends AnyOperatorTestCase {
46+
public abstract class LuceneMaxOperatorTestCase extends SourceOperatorTestCase {
4747

4848
protected interface NumberTypeTest {
4949

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import org.elasticsearch.compute.data.Page;
2323
import org.elasticsearch.compute.operator.Driver;
2424
import org.elasticsearch.compute.operator.DriverContext;
25-
import org.elasticsearch.compute.test.AnyOperatorTestCase;
2625
import org.elasticsearch.compute.test.OperatorTestCase;
26+
import org.elasticsearch.compute.test.SourceOperatorTestCase;
2727
import org.elasticsearch.compute.test.TestDriverFactory;
2828
import org.elasticsearch.compute.test.TestResultPageSinkOperator;
2929
import org.elasticsearch.core.IOUtils;
@@ -43,7 +43,7 @@
4343
import static org.hamcrest.Matchers.lessThanOrEqualTo;
4444
import static org.hamcrest.Matchers.matchesRegex;
4545

46-
public abstract class LuceneMinOperatorTestCase extends AnyOperatorTestCase {
46+
public abstract class LuceneMinOperatorTestCase extends SourceOperatorTestCase {
4747

4848
protected interface NumberTypeTest {
4949

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.elasticsearch.compute.operator.PageConsumerOperator;
3232
import org.elasticsearch.compute.operator.SinkOperator;
3333
import org.elasticsearch.compute.operator.SourceOperator;
34-
import org.elasticsearch.compute.test.AnyOperatorTestCase;
3534
import org.elasticsearch.compute.test.OperatorTestCase;
35+
import org.elasticsearch.compute.test.SourceOperatorTestCase;
3636
import org.elasticsearch.compute.test.TestDriverFactory;
3737
import org.elasticsearch.compute.test.TestResultPageSinkOperator;
3838
import org.elasticsearch.core.IOUtils;
@@ -64,7 +64,7 @@
6464
import static org.hamcrest.Matchers.lessThanOrEqualTo;
6565
import static org.hamcrest.Matchers.matchesRegex;
6666

67-
public class LuceneSourceOperatorTests extends AnyOperatorTestCase {
67+
public class LuceneSourceOperatorTests extends SourceOperatorTestCase {
6868
private static final MappedFieldType S_FIELD = new NumberFieldMapper.NumberFieldType("s", NumberFieldMapper.NumberType.LONG);
6969

7070
@ParametersFactory(argumentFormatting = "%s %s")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.elasticsearch.compute.lucene.read.ValuesSourceReaderOperatorTests;
2828
import org.elasticsearch.compute.operator.DriverContext;
2929
import org.elasticsearch.compute.operator.Operator;
30-
import org.elasticsearch.compute.test.AnyOperatorTestCase;
3130
import org.elasticsearch.compute.test.OperatorTestCase;
31+
import org.elasticsearch.compute.test.SourceOperatorTestCase;
3232
import org.elasticsearch.compute.test.TestDriverFactory;
3333
import org.elasticsearch.compute.test.TestResultPageSinkOperator;
3434
import org.elasticsearch.core.IOUtils;
@@ -52,7 +52,7 @@
5252
import static org.hamcrest.Matchers.hasSize;
5353
import static org.hamcrest.Matchers.matchesRegex;
5454

55-
public class LuceneTopNSourceOperatorTests extends AnyOperatorTestCase {
55+
public class LuceneTopNSourceOperatorTests extends SourceOperatorTestCase {
5656
private static final MappedFieldType S_FIELD = new NumberFieldMapper.NumberFieldType("s", NumberFieldMapper.NumberType.LONG);
5757
private Directory directory = newDirectory();
5858
private IndexReader reader;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import org.elasticsearch.compute.operator.DriverContext;
4242
import org.elasticsearch.compute.operator.DriverStatus;
4343
import org.elasticsearch.compute.operator.Operator;
44-
import org.elasticsearch.compute.test.AnyOperatorTestCase;
4544
import org.elasticsearch.compute.test.OperatorTestCase;
45+
import org.elasticsearch.compute.test.SourceOperatorTestCase;
4646
import org.elasticsearch.compute.test.TestDriverFactory;
4747
import org.elasticsearch.compute.test.TestResultPageSinkOperator;
4848
import org.elasticsearch.core.CheckedFunction;
@@ -74,7 +74,7 @@
7474
import static org.hamcrest.Matchers.hasSize;
7575
import static org.hamcrest.Matchers.lessThanOrEqualTo;
7676

77-
public class TimeSeriesSourceOperatorTests extends AnyOperatorTestCase {
77+
public class TimeSeriesSourceOperatorTests extends SourceOperatorTestCase {
7878

7979
private IndexReader reader;
8080
private final Directory directory = newDirectory();

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/ChangePointOperatorTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Map;
2223

2324
import static org.hamcrest.Matchers.equalTo;
2425
import static org.hamcrest.Matchers.hasSize;
26+
import static org.hamcrest.Matchers.nullValue;
2527

2628
public class ChangePointOperatorTests extends OperatorTestCase {
2729

@@ -83,4 +85,9 @@ protected Matcher<String> expectedDescriptionOfSimple() {
8385
protected Matcher<String> expectedToStringOfSimple() {
8486
return equalTo("ChangePointOperator[channel=0]");
8587
}
88+
89+
@Override
90+
protected void assertEmptyStatus(Map<String, Object> map) {
91+
assertThat(map, nullValue());
92+
}
8693
}

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/OutputOperatorTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import org.hamcrest.Matcher;
1212

1313
import java.util.List;
14+
import java.util.Map;
1415
import java.util.stream.IntStream;
1516

1617
import static org.hamcrest.Matchers.equalTo;
18+
import static org.hamcrest.Matchers.nullValue;
1719

1820
public class OutputOperatorTests extends AnyOperatorTestCase {
1921
@Override
@@ -48,4 +50,9 @@ public void testBigToString() {
4850
public void testBigDescription() {
4951
assertThat(big().describe(), equalTo(expectedDescriptionOfBig()));
5052
}
53+
54+
@Override
55+
protected void assertEmptyStatus(Map<String, Object> map) {
56+
assertThat(map, nullValue());
57+
}
5158
}

x-pack/plugin/esql/compute/test/src/main/java/org/elasticsearch/compute/test/AnyOperatorTestCase.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
import org.elasticsearch.compute.data.BlockFactory;
1515
import org.elasticsearch.compute.operator.DriverContext;
1616
import org.elasticsearch.compute.operator.Operator;
17-
import org.elasticsearch.compute.operator.SinkOperator;
1817
import org.elasticsearch.compute.operator.SourceOperator;
18+
import org.elasticsearch.core.Nullable;
19+
import org.elasticsearch.test.MapMatcher;
1920
import org.elasticsearch.xcontent.ToXContent;
2021
import org.elasticsearch.xcontent.XContentBuilder;
2122
import org.elasticsearch.xcontent.XContentType;
2223
import org.hamcrest.Matcher;
2324

2425
import java.io.IOException;
26+
import java.util.Map;
2527

26-
import static org.hamcrest.Matchers.both;
27-
import static org.hamcrest.Matchers.either;
28-
import static org.hamcrest.Matchers.hasKey;
28+
import static org.elasticsearch.test.MapMatcher.assertMap;
29+
import static org.elasticsearch.test.MapMatcher.matchesMap;
2930
import static org.hamcrest.Matchers.matchesPattern;
3031

3132
/**
@@ -107,39 +108,44 @@ public final void testSimpleToString() {
107108
/**
108109
* Ensures that the Operator.Status of this operator has the standard fields.
109110
*/
110-
public void testOperatorStatus() throws IOException {
111+
public final void testOperatorStatus() throws IOException {
111112
DriverContext driverContext = driverContext();
112113
try (var operator = simple().get(driverContext)) {
113114
Operator.Status status = operator.status();
115+
if (status == null) {
116+
assertEmptyStatus(null);
117+
return;
118+
}
114119

115-
assumeTrue("Operator does not provide a status", status != null);
116-
117-
var xContent = XContentType.JSON.xContent();
118-
try (var xContentBuilder = XContentBuilder.builder(xContent)) {
120+
try (var xContentBuilder = XContentBuilder.builder(XContentType.JSON.xContent())) {
119121
status.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
120122

121123
var bytesReference = BytesReference.bytes(xContentBuilder);
122124
var map = XContentHelper.convertToMap(bytesReference, false, xContentBuilder.contentType()).v2();
123125

124-
if (operator instanceof SourceOperator) {
125-
assertThat(map, hasKey("pages_emitted"));
126-
assertThat(map, hasKey("rows_emitted"));
127-
} else if (operator instanceof SinkOperator) {
128-
assertThat(map, hasKey("pages_received"));
129-
assertThat(map, hasKey("rows_received"));
130-
} else {
131-
assertThat(map, either(hasKey("pages_processed")).or(both(hasKey("pages_received")).and(hasKey("pages_emitted"))));
132-
assertThat(map, hasKey("rows_received"));
133-
assertThat(map, hasKey("rows_emitted"));
134-
}
126+
assertEmptyStatus(map);
135127
}
136128
}
137129
}
138130

131+
/**
132+
* Assert that the status is sane.
133+
*/
134+
protected void assertEmptyStatus(@Nullable Map<String, Object> map) {
135+
MapMatcher matcher = matchesMap().extraOk();
136+
if (map.containsKey("pages_processed")) {
137+
matcher = matcher.entry("pages_processed", 0);
138+
} else {
139+
matcher = matcher.entry("pages_received", 0).entry("pages_emitted", 0);
140+
}
141+
matcher = matcher.entry("rows_received", 0).entry("rows_emitted", 0);
142+
assertMap(map, matcher);
143+
}
144+
139145
/**
140146
* A {@link DriverContext} with a nonBreakingBigArrays.
141147
*/
142-
protected DriverContext driverContext() { // TODO make this final once all operators support memory tracking
148+
protected final DriverContext driverContext() {
143149
BlockFactory blockFactory = blockFactory();
144150
return new DriverContext(blockFactory.bigArrays(), blockFactory);
145151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.compute.test;
9+
10+
import java.util.Map;
11+
12+
import static org.elasticsearch.test.MapMatcher.assertMap;
13+
import static org.elasticsearch.test.MapMatcher.matchesMap;
14+
15+
public abstract class SourceOperatorTestCase extends AnyOperatorTestCase {
16+
@Override
17+
protected void assertEmptyStatus(Map<String, Object> map) {
18+
assertMap(map, matchesMap().extraOk().entry("pages_emitted", 0).entry("rows_emitted", 0));
19+
}
20+
}

0 commit comments

Comments
 (0)