Skip to content

Commit

Permalink
Update esql error message
Browse files Browse the repository at this point in the history
This change update esql comparison error message to also include expected and actual values
in order to make it easier to spot the incorrect actual data.
  • Loading branch information
idegtiarenko committed Dec 9, 2024
1 parent d411ad8 commit a50a67f
Showing 1 changed file with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import static org.elasticsearch.xpack.esql.core.util.NumericUtils.unsignedLongAsNumber;
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.CARTESIAN;
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.GEO;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public final class CsvAssert {
Expand Down Expand Up @@ -197,11 +197,7 @@ public static void assertData(
for (int row = 0; row < expectedValues.size(); row++) {
try {
if (row >= actualValues.size()) {
if (dataFailures.isEmpty()) {
fail("Expected more data but no more entries found after [" + row + "]");
} else {
dataFailure(dataFailures, "Expected more data but no more entries found after [" + row + "]\n");
}
dataFailure("Expected more data but no more entries found after [" + row + "]", dataFailures, expected, actualValues);
}

if (logger != null) {
Expand Down Expand Up @@ -250,7 +246,7 @@ public static void assertData(
dataFailures.add(new DataFailure(row, column, transformedExpected, transformedActual));
}
if (dataFailures.size() > 10) {
dataFailure(dataFailures);
dataFailure("", dataFailures, expected, actualValues);
}
}

Expand All @@ -267,7 +263,7 @@ public static void assertData(
}
}
if (dataFailures.isEmpty() == false) {
dataFailure(dataFailures);
dataFailure("", dataFailures, expected, actualValues);
}
if (expectedValues.size() < actualValues.size()) {
fail(
Expand All @@ -276,12 +272,49 @@ public static void assertData(
}
}

private static void dataFailure(List<DataFailure> dataFailures) {
dataFailure(dataFailures, "");
private static void dataFailure(
String description,
List<DataFailure> dataFailures,
ExpectedResults expectedValues,
List<List<Object>> actualValues
) {
var expected = pipeTable("Expected:", expectedValues.columnNames(), expectedValues.values(), 25);
var actual = pipeTable("Actual:", expectedValues.columnNames(), actualValues, 25);
fail(description + System.lineSeparator() + describeFailures(dataFailures) + actual + expected);
}

private static String pipeTable(String description, List<String> headers, List<List<Object>> values, int maxRows) {
int[] width = new int[headers.size()];
for (int i = 0; i < width.length; i++) {
width[i] = headers.get(i).length();
for (List<Object> row : values) {
width[i] = Math.max(width[i], String.valueOf(row.get(i)).length());
}
}

var result = new StringBuilder().append(System.lineSeparator()).append(description).append(System.lineSeparator());
for (int c = 0; c < width.length; c++) {
appendValue(result, headers.get(c), width[c]);
}
result.append('|').append(System.lineSeparator());
for (int r = 0; r < Math.min(maxRows, values.size()); r++) {
for (int c = 0; c < width.length; c++) {
appendValue(result, values.get(r).get(c), width[c]);
}
result.append('|').append(System.lineSeparator());
}
return result.toString();
}

private static void appendValue(StringBuilder result, Object value, int width) {
result.append('|').append(value);
for (int i = 0; i < width - String.valueOf(value).length(); i++) {
result.append(' ');
}
}

private static void dataFailure(List<DataFailure> dataFailures, String prefixError) {
fail(prefixError + "Data mismatch:\n" + dataFailures.stream().map(f -> {
private static String describeFailures(List<DataFailure> dataFailures) {
return "Data mismatch:" + System.lineSeparator() + dataFailures.stream().map(f -> {
Description description = new StringDescription();
ListMatcher expected;
if (f.expected instanceof List<?> e) {
Expand All @@ -298,8 +331,8 @@ private static void dataFailure(List<DataFailure> dataFailures, String prefixErr
}
expected.describeMismatch(actualList, description);
String prefix = "row " + f.row + " column " + f.column + ":";
return prefix + description.toString().replace("\n", "\n" + prefix);
}).collect(Collectors.joining("\n")));
return prefix + description.toString();//.replace("\n", "\n" + prefix);
}).collect(Collectors.joining(System.lineSeparator()));
}

private static Comparator<List<Object>> resultRowComparator(List<Type> types) {
Expand Down

0 comments on commit a50a67f

Please sign in to comment.