Skip to content

Commit be1badf

Browse files
committed
Fix esql tests to allow is_partial in result
1 parent 8825fe5 commit be1badf

File tree

9 files changed

+271
-439
lines changed

9 files changed

+271
-439
lines changed

docs/reference/esql/esql-across-clusters.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ which clusters have slower response times than others.
287287
<6> The shard details for the search on that cluster, including a count of shards that were
288288
skipped due to the can-match phase results. Shards are skipped when they cannot have any matching data
289289
and therefore are not included in the full ES|QL query.
290-
<7> The `is_partial` field is set to `true` if the search was interrupted before finishing using
291-
<<esql-async-query-stop-api,async stop API>>.
290+
<7> The `is_partial` field is set to `true` if the search has partial results for any reason,
291+
for example if it was interrupted before finishing using <<esql-async-query-stop-api,async stop API>>.
292292

293293

294294
The cross-cluster metadata can be used to determine whether any data came back from a cluster.

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.elasticsearch.rest.RestStatus;
7777
import org.elasticsearch.test.AbstractBroadcastResponseTestCase;
7878
import org.elasticsearch.test.ESTestCase;
79+
import org.elasticsearch.test.MapMatcher;
7980
import org.elasticsearch.xcontent.ConstructingObjectParser;
8081
import org.elasticsearch.xcontent.DeprecationHandler;
8182
import org.elasticsearch.xcontent.NamedXContentRegistry;
@@ -84,6 +85,7 @@
8485
import org.elasticsearch.xcontent.XContentParserConfiguration;
8586
import org.elasticsearch.xcontent.XContentType;
8687
import org.elasticsearch.xcontent.json.JsonXContent;
88+
import org.hamcrest.Matcher;
8789
import org.hamcrest.Matchers;
8890
import org.junit.After;
8991
import org.junit.AfterClass;
@@ -133,14 +135,12 @@
133135
import static org.elasticsearch.client.RestClient.IGNORE_RESPONSE_CODES_PARAM;
134136
import static org.elasticsearch.cluster.ClusterState.VERSION_INTRODUCING_TRANSPORT_VERSIONS;
135137
import static org.elasticsearch.core.Strings.format;
138+
import static org.elasticsearch.test.MapMatcher.assertMap;
139+
import static org.elasticsearch.test.MapMatcher.matchesMap;
136140
import static org.elasticsearch.test.rest.TestFeatureService.ALL_FEATURES;
137141
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
138-
import static org.hamcrest.Matchers.anyOf;
139-
import static org.hamcrest.Matchers.containsString;
140-
import static org.hamcrest.Matchers.equalTo;
141-
import static org.hamcrest.Matchers.everyItem;
142-
import static org.hamcrest.Matchers.in;
143-
import static org.hamcrest.Matchers.notNullValue;
142+
import static org.hamcrest.Matchers.*;
143+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
144144

145145
/**
146146
* Superclass for tests that interact with an external test cluster using Elasticsearch's {@link RestClient}.
@@ -2570,4 +2570,46 @@ public static Request newXContentRequest(HttpMethod method, String endpoint, ToX
25702570
addXContentBody(request, body);
25712571
return request;
25722572
}
2573+
2574+
protected static MapMatcher getResultMatcher(boolean includeMetadata, boolean includePartial) {
2575+
MapMatcher mapMatcher = matchesMap();
2576+
if (includeMetadata) {
2577+
mapMatcher = mapMatcher.entry("took", greaterThanOrEqualTo(0));
2578+
}
2579+
// Older version may not have is_partial
2580+
if (includePartial) {
2581+
mapMatcher = mapMatcher.entry("is_partial", false);
2582+
}
2583+
return mapMatcher;
2584+
}
2585+
2586+
/**
2587+
* Create empty result matcher from result, taking into account all metadata items.
2588+
*/
2589+
protected static MapMatcher getResultMatcher(Map<String, Object> result) {
2590+
return getResultMatcher(result.containsKey("took"), result.containsKey("is_partial"));
2591+
}
2592+
2593+
/**
2594+
* Match result columns and values, with default matchers for metadata.
2595+
*/
2596+
protected static void assertResultMap(Map<String, Object> result, Matcher<?> columnMatcher, Matcher<?> valuesMatcher) {
2597+
assertMap(result, getResultMatcher(result).entry("columns", columnMatcher).entry("values", valuesMatcher));
2598+
}
2599+
2600+
protected static void assertResultMap(Map<String, Object> result, Object columnMatcher, Object valuesMatcher) {
2601+
assertMap(result, getResultMatcher(result).entry("columns", columnMatcher).entry("values", valuesMatcher));
2602+
}
2603+
2604+
/**
2605+
* Match result columns and values, with default matchers for metadata.
2606+
*/
2607+
protected static void assertResultMap(
2608+
Map<String, Object> result,
2609+
MapMatcher mapMatcher,
2610+
Matcher<?> columnMatcher,
2611+
Matcher<?> valuesMatcher
2612+
) {
2613+
assertMap(result, mapMatcher.entry("columns", columnMatcher).entry("values", valuesMatcher));
2614+
}
25732615
}

x-pack/plugin/esql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/esql/EsqlAsyncSecurityIT.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.util.Locale;
24+
import java.util.Map;
2425

2526
import static org.elasticsearch.core.TimeValue.timeValueNanos;
2627
import static org.hamcrest.Matchers.allOf;
@@ -50,8 +51,9 @@ protected Response runESQLCommand(String user, String command) throws IOExceptio
5051
}
5152

5253
@Override
53-
protected MapMatcher responseMatcher() {
54-
return super.responseMatcher().entry("is_running", equalTo(false)).entry("id", allOf(notNullValue(), instanceOf(String.class)));
54+
protected MapMatcher responseMatcher(Map<String, Object> result) {
55+
return super.responseMatcher(result).entry("is_running", equalTo(false))
56+
.entry("id", allOf(notNullValue(), instanceOf(String.class)));
5557
}
5658

5759
@Override

x-pack/plugin/esql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/esql/EsqlSecurityIT.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public void indexDocuments() throws IOException {
165165
}
166166
}
167167

168-
protected MapMatcher responseMatcher() {
169-
return matchesMap();
168+
protected MapMatcher responseMatcher(Map<String, Object> result) {
169+
return getResultMatcher(result);
170170
}
171171

172172
public void testAllowedIndices() throws Exception {
@@ -182,10 +182,7 @@ public void testAllowedIndices() throws Exception {
182182
Response resp = runESQLCommand(user, "from index-user1 | stats sum=sum(value)");
183183
assertOK(resp);
184184
Map<String, Object> responseMap = entityAsMap(resp);
185-
MapMatcher mapMatcher = responseMatcher();
186-
if (responseMap.get("took") != null) {
187-
mapMatcher = mapMatcher.entry("took", ((Integer) responseMap.get("took")).intValue());
188-
}
185+
MapMatcher mapMatcher = responseMatcher(responseMap);
189186
MapMatcher matcher = mapMatcher.entry("columns", List.of(Map.of("name", "sum", "type", "double")))
190187
.entry("values", List.of(List.of(43.0d)));
191188
assertMap(responseMap, matcher);
@@ -195,10 +192,7 @@ public void testAllowedIndices() throws Exception {
195192
Response resp = runESQLCommand(user, "from index-user2 | stats sum=sum(value)");
196193
assertOK(resp);
197194
Map<String, Object> responseMap = entityAsMap(resp);
198-
MapMatcher mapMatcher = responseMatcher();
199-
if (responseMap.get("took") != null) {
200-
mapMatcher = mapMatcher.entry("took", ((Integer) responseMap.get("took")).intValue());
201-
}
195+
MapMatcher mapMatcher = responseMatcher(responseMap);
202196
MapMatcher matcher = mapMatcher.entry("columns", List.of(Map.of("name", "sum", "type", "double")))
203197
.entry("values", List.of(List.of(72.0d)));
204198
assertMap(responseMap, matcher);
@@ -208,10 +202,7 @@ public void testAllowedIndices() throws Exception {
208202
Response resp = runESQLCommand("metadata1_read2", "from " + index + " | stats sum=sum(value)");
209203
assertOK(resp);
210204
Map<String, Object> responseMap = entityAsMap(resp);
211-
MapMatcher mapMatcher = responseMatcher();
212-
if (responseMap.get("took") != null) {
213-
mapMatcher = mapMatcher.entry("took", ((Integer) responseMap.get("took")).intValue());
214-
}
205+
MapMatcher mapMatcher = responseMatcher(responseMap);
215206
MapMatcher matcher = mapMatcher.entry("columns", List.of(Map.of("name", "sum", "type", "double")))
216207
.entry("values", List.of(List.of(72.0d)));
217208
assertMap(responseMap, matcher);
@@ -226,9 +217,10 @@ public void testAliases() throws Exception {
226217
);
227218
assertOK(resp);
228219
Map<String, Object> responseMap = entityAsMap(resp);
229-
MapMatcher matcher = responseMatcher().entry("took", ((Integer) responseMap.get("took")).intValue())
230-
.entry("columns", List.of(Map.of("name", "sum", "type", "double"), Map.of("name", "index", "type", "keyword")))
231-
.entry("values", List.of(List.of(72.0d, "index-user2")));
220+
MapMatcher matcher = responseMatcher(responseMap).entry(
221+
"columns",
222+
List.of(Map.of("name", "sum", "type", "double"), Map.of("name", "index", "type", "keyword"))
223+
).entry("values", List.of(List.of(72.0d, "index-user2")));
232224
assertMap(responseMap, matcher);
233225
}
234226
}
@@ -238,16 +230,14 @@ public void testAliasFilter() throws Exception {
238230
Response resp = runESQLCommand("alias_user1", "from " + index + " METADATA _index" + "| KEEP _index, org, value | LIMIT 10");
239231
assertOK(resp);
240232
Map<String, Object> responseMap = entityAsMap(resp);
241-
MapMatcher matcher = responseMatcher().entry("took", ((Integer) responseMap.get("took")).intValue())
242-
.entry(
243-
"columns",
244-
List.of(
245-
Map.of("name", "_index", "type", "keyword"),
246-
Map.of("name", "org", "type", "keyword"),
247-
Map.of("name", "value", "type", "double")
248-
)
233+
MapMatcher matcher = responseMatcher(responseMap).entry(
234+
"columns",
235+
List.of(
236+
Map.of("name", "_index", "type", "keyword"),
237+
Map.of("name", "org", "type", "keyword"),
238+
Map.of("name", "value", "type", "double")
249239
)
250-
.entry("values", List.of(List.of("index-user1", "sales", 31.0d)));
240+
).entry("values", List.of(List.of("index-user1", "sales", 31.0d)));
251241
assertMap(responseMap, matcher);
252242
}
253243
}

0 commit comments

Comments
 (0)