Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify user-visible message in assertQueryFails #20661

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.trino.cache.SafeCaches;
import io.trino.client.ErrorInfo;
import io.trino.client.FailureException;
Expand All @@ -30,7 +31,6 @@
import io.trino.testing.QueryFailedException;
import org.assertj.core.api.AbstractThrowableAssert;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.internal.Failures;
import org.assertj.core.util.CheckReturnValue;

import java.util.Optional;
Expand All @@ -42,7 +42,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.assertj.core.error.ShouldHaveMessageMatchingRegex.shouldHaveMessageMatchingRegex;

public final class TrinoExceptionAssert
extends AbstractThrowableAssert<TrinoExceptionAssert, Throwable>
Expand Down Expand Up @@ -114,6 +113,7 @@ private TrinoExceptionAssert(Throwable actual, FailureInfo failureInfo)
this.failureInfo = requireNonNull(failureInfo, "failureInfo is null");
}

@CanIgnoreReturnValue
public TrinoExceptionAssert hasErrorCode(ErrorCodeSupplier... errorCodeSupplier)
{
ErrorCode errorCode = null;
Expand All @@ -135,6 +135,7 @@ public TrinoExceptionAssert hasErrorCode(ErrorCodeSupplier... errorCodeSupplier)
return myself;
}

@CanIgnoreReturnValue
public TrinoExceptionAssert hasLocation(int lineNumber, int columnNumber)
{
try {
Expand All @@ -148,16 +149,4 @@ public TrinoExceptionAssert hasLocation(int lineNumber, int columnNumber)
}
return myself;
}

public TrinoExceptionAssert hasCauseMessageMatching(String regex)
{
Throwable cause = actual;
while (cause != null) {
if (cause.getMessage().matches(regex)) {
return myself;
}
cause = cause.getCause();
}
throw Failures.instance().failure(info, shouldHaveMessageMatchingRegex(actual, regex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ protected static void assertQueryFails(QueryRunner queryRunner, Session session,
catch (RuntimeException exception) {
exception.addSuppressed(new Exception("Query: " + sql));
assertThatTrinoException(exception)
.hasCauseMessageMatching(expectedMessageRegExp);
.hasMessageMatching(expectedMessageRegExp);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static com.google.common.base.Throwables.getCausalChain;
import static io.airlift.testing.Closeables.closeAllSuppress;
import static io.trino.plugin.jdbc.JdbcMetadataSessionProperties.AGGREGATION_PUSHDOWN_ENABLED;
import static io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME;
Expand Down Expand Up @@ -101,6 +103,78 @@ public void testMatches()
.matches("VALUES CAST('CANADA' AS varchar(25))");
}

@Test
public void testQueryFails()
{
// Accept failure
assertQueryFails("SELECT CAST('123a' AS integer)", "Cannot cast '123a' to INT");

// Report wrong message
assertThatThrownBy(() -> assertQueryFails("SELECT CAST('123a' AS integer)", "Different expected message"))
.isInstanceOf(AssertionError.class)
.hasMessageStartingWith("""

Expecting message:
"Cannot cast '123a' to INT"
to match regex:
"Different expected message"
but did not.

Throwable that failed the check:""");

// Match message with regular expression
assertQueryFails("SELECT CAST('123a' AS integer)", "Cannot cast '\\w+' (to)? INT");
assertQueryFails("SELECT fail('Some (message|with).pattern-likes++')", Pattern.quote("Some (message|with).pattern-likes++"));

// Verify message full match
assertQueryFails("SELECT CAST('123a' AS integer)", "^Cannot cast '123a' to INT$");
assertThatThrownBy(() -> assertQueryFails("SELECT CAST('123a' AS integer)", "Cannot cast"))
.isInstanceOf(AssertionError.class)
.hasMessageStartingWith("""

Expecting message:
"Cannot cast '123a' to INT"
to match regex:
"Cannot cast"
but did not.

Throwable that failed the check:""");

// Report query success
assertThatThrownBy(() -> assertQueryFails("SELECT 1", "Foo bar"))
.isInstanceOf(AssertionError.class)
.hasMessageMatching("Expected query to fail: SELECT 1 \\[QueryId: \\w+]");
}

@Test
// assertQueryFails should verify top level exception message, since this is what gets reported to the user by default.
public void testQueryFailsVerifiesEndUserVisibleMessage()
{
String sql = "SELECT CAST('abc' AS date)";
String lastMessage;
try {
// Any query that results in a TrinoException with message A and cause with message B != A.
computeActual(sql);
throw new IllegalStateException("Expected query failure");
}
catch (Exception expected) {
lastMessage = getCausalChain(expected).getLast().getMessage();
assertThat(lastMessage).isEqualTo("Invalid format: \"abc\"");
}

assertThatThrownBy(() -> assertQueryFails(sql, Pattern.quote(lastMessage)))
.isInstanceOf(AssertionError.class)
.hasMessageStartingWith("""

Expecting message:
"Value cannot be cast to date: abc"
to match regex:
"\\QInvalid format: "abc"\\E"
but did not.

Throwable that failed the check:""");
}

@Test
public void testWrongType()
{
Expand Down Expand Up @@ -148,16 +222,16 @@ public void testVarbinaryResult()
assertThatThrownBy(() -> queryAssert.matches("VALUES X'001299'"))
.hasMessageMatching(
"(?s).*" +
"\\Q" +
"Expecting actual:\n" +
" ([0, 18, 52])\n" +
"to contain exactly in any order:\n" +
" [([0, 18, -103])]\n" +
"elements not found:\n" +
" ([0, 18, -103])\n" +
"and elements not expected:\n" +
" ([0, 18, 52])" +
"\\E.*");
"\\Q" +
"Expecting actual:\n" +
" ([0, 18, 52])\n" +
"to contain exactly in any order:\n" +
" [([0, 18, -103])]\n" +
"elements not found:\n" +
" ([0, 18, -103])\n" +
"and elements not expected:\n" +
" ([0, 18, 52])" +
"\\E.*");
}

@Test
Expand All @@ -170,16 +244,16 @@ public void testNestedVarbinaryResult()
assertThatThrownBy(() -> queryAssert.matches("SELECT CAST(ROW(X'001299') AS ROW(foo varbinary))"))
.hasMessageMatching(
"(?s).*" +
"\\Q" +
"Expecting actual:\n" +
" ([X'00 12 34'])\n" +
"to contain exactly in any order:\n" +
" [([X'00 12 99'])]\n" +
"elements not found:\n" +
" ([X'00 12 99'])\n" +
"and elements not expected:\n" +
" ([X'00 12 34'])" +
"\\E.*");
"\\Q" +
"Expecting actual:\n" +
" ([X'00 12 34'])\n" +
"to contain exactly in any order:\n" +
" [([X'00 12 99'])]\n" +
"elements not found:\n" +
" ([X'00 12 99'])\n" +
"and elements not expected:\n" +
" ([X'00 12 34'])" +
"\\E.*");
}

/**
Expand Down
Loading