Skip to content

Commit 3b4cbda

Browse files
idegtiarenkoomricohenn
authored andcommitted
Improve StatementParserTests error message (elastic#125568)
1 parent 973ac30 commit 3b4cbda

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,20 @@ public static <T extends Throwable> T expectThrows(Class<T> expectedType, Matche
27132713
return e;
27142714
}
27152715

2716+
/**
2717+
* Checks a specific exception class with matched message is thrown by the given runnable, and returns it.
2718+
*/
2719+
public static <T extends Throwable> T expectThrows(
2720+
String reason,
2721+
Class<T> expectedType,
2722+
Matcher<String> messageMatcher,
2723+
ThrowingRunnable runnable
2724+
) {
2725+
var e = expectThrows(expectedType, reason, runnable);
2726+
assertThat(reason, e.getMessage(), messageMatcher);
2727+
return e;
2728+
}
2729+
27162730
/**
27172731
* Same as {@link #runInParallel(int, IntConsumer)} but also attempts to start all tasks at the same time by blocking execution on a
27182732
* barrier until all threads are started and ready to execute their task.

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/IdentifierGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ public static String randomIndexPattern(Feature... features) {
9090
var cluster = maybeQuote(randomIdentifier());
9191
pattern = maybeQuote(cluster + ":" + pattern);
9292
} else if (EsqlCapabilities.Cap.INDEX_COMPONENT_SELECTORS.isEnabled() && canAdd(Features.INDEX_SELECTOR, features)) {
93-
var selector = ESTestCase.randomFrom(IndexComponentSelector.values());
94-
pattern = maybeQuote(pattern + "::" + selector.getKey());
93+
pattern = maybeQuote(pattern + "::" + randomFrom(IndexComponentSelector.values()).getKey());
9594
}
9695
return pattern;
9796
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/AbstractStatementParserTests.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,25 @@ static MapExpression mapExpression(Map<String, Object> keyValuePairs) {
140140
}
141141

142142
void expectError(String query, String errorMessage) {
143-
ParsingException e = expectThrows(ParsingException.class, "Expected syntax error for " + query, () -> statement(query));
144-
assertThat(e.getMessage(), containsString(errorMessage));
145-
}
146-
147-
void expectVerificationError(String query, String errorMessage) {
148-
VerificationException e = expectThrows(VerificationException.class, "Expected syntax error for " + query, () -> statement(query));
149-
assertThat(e.getMessage(), containsString(errorMessage));
143+
expectError(query, null, errorMessage);
150144
}
151145

152146
void expectError(String query, List<QueryParam> params, String errorMessage) {
153-
ParsingException e = expectThrows(
147+
expectThrows(
148+
"Query [" + query + "] is expected to throw " + ParsingException.class + " with message [" + errorMessage + "]",
154149
ParsingException.class,
155-
"Expected syntax error for " + query,
150+
containsString(errorMessage),
156151
() -> statement(query, new QueryParams(params))
157152
);
158-
assertThat(e.getMessage(), containsString(errorMessage));
153+
}
154+
155+
void expectVerificationError(String query, String errorMessage) {
156+
expectThrows(
157+
"Query [" + query + "] is expected to throw " + VerificationException.class + " with message [" + errorMessage + "]",
158+
VerificationException.class,
159+
containsString(errorMessage),
160+
() -> parser.createStatement(query)
161+
);
159162
}
160163

161164
void expectInvalidIndexNameErrorWithLineNumber(String query, String indexString, String lineNumber) {

0 commit comments

Comments
 (0)