Skip to content

Commit

Permalink
Minor refactor for match and qstr integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHegarty committed Nov 7, 2024
1 parent 908d34d commit 7765193
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ public static List<List<Object>> getValuesList(Iterator<Iterator<Object>> values
return valuesList;
}

public static List<List<Object>> getValuesList(Iterable<Iterable<Object>> values) {
var valuesList = new ArrayList<List<Object>>();
values.iterator().forEachRemaining(row -> {
var rowValues = new ArrayList<>();
row.iterator().forEachRemaining(rowValues::add);
valuesList.add(rowValues);
});
return valuesList;
}

public static List<String> withDefaultLimitWarning(List<String> warnings) {
List<String> result = warnings == null ? new ArrayList<>() : new ArrayList<>(warnings);
result.add("No limit defined, adding default limit of [1000]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.xpack.core.esql.action.ColumnInfo;
import org.elasticsearch.xpack.esql.plugin.EsqlPlugin;
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
import org.elasticsearch.xpack.esql.plugin.TransportEsqlQueryAction;
import org.junit.After;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.getValuesList;
import static org.hamcrest.Matchers.equalTo;

@TestLogging(value = "org.elasticsearch.xpack.esql.session:DEBUG", reason = "to better understand planning")
Expand Down Expand Up @@ -204,4 +207,16 @@ protected static QueryPragmas randomPragmas() {
protected static boolean canUseQueryPragmas() {
return Build.current().isSnapshot();
}

protected static void assertColumnNames(List<? extends ColumnInfo> actualColumns, List<String> expectedNames) {
assertThat(actualColumns.stream().map(ColumnInfo::name).toList(), equalTo(expectedNames));
}

protected static void assertColumnTypes(List<? extends ColumnInfo> actualColumns, List<String> expectedTypes) {
assertThat(actualColumns.stream().map(ColumnInfo::outputType).toList(), equalTo(expectedTypes));
}

protected static void assertValues(Iterator<Iterator<Object>> actualValues, Iterable<Iterable<Object>> expectedValues) {
assertThat(getValuesList(actualValues), equalTo(getValuesList(expectedValues)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,22 @@

package org.elasticsearch.xpack.esql.plugin;

import org.elasticsearch.Build;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.xpack.esql.VerificationException;
import org.elasticsearch.xpack.esql.action.AbstractEsqlIntegTestCase;
import org.elasticsearch.xpack.esql.action.ColumnInfoImpl;
import org.elasticsearch.xpack.esql.action.EsqlCapabilities;
import org.elasticsearch.xpack.esql.action.EsqlQueryRequest;
import org.elasticsearch.xpack.esql.action.EsqlQueryResponse;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.junit.Before;

import java.util.List;

import static org.elasticsearch.test.ListMatcher.matchesList;
import static org.elasticsearch.test.MapMatcher.assertMap;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.getValuesList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.equalTo;

@TestLogging(value = "org.elasticsearch.xpack.esql:TRACE,org.elasticsearch.compute:TRACE", reason = "debug")
public class MatchOperatorIT extends AbstractEsqlIntegTestCase {
Expand All @@ -40,7 +34,7 @@ public void setupIndex() {

@Override
protected EsqlQueryResponse run(EsqlQueryRequest request) {
assumeTrue("match operator available in snapshot builds only", Build.current().isSnapshot());
assumeTrue("match operator capability not available", EsqlCapabilities.Cap.MATCH_OPERATOR_COLON.isEnabled());
return super.run(request);
}

Expand All @@ -53,11 +47,9 @@ public void testSimpleWhereMatch() {
""";

try (var resp = run(query)) {
assertThat(resp.columns().stream().map(ColumnInfoImpl::name).toList(), equalTo(List.of("id")));
assertThat(resp.columns().stream().map(ColumnInfoImpl::type).map(DataType::toString).toList(), equalTo(List.of("INTEGER")));
// values
List<List<Object>> values = getValuesList(resp);
assertMap(values, matchesList().item(List.of(1)).item(List.of(6)));
assertColumnNames(resp.columns(), List.of("id"));
assertColumnTypes(resp.columns(), List.of("integer"));
assertValues(resp.values(), List.of(List.of(1), List.of(6)));
}
}

Expand All @@ -70,11 +62,9 @@ public void testCombinedWhereMatch() {
""";

try (var resp = run(query)) {
assertThat(resp.columns().stream().map(ColumnInfoImpl::name).toList(), equalTo(List.of(("id"))));
assertThat(resp.columns().stream().map(ColumnInfoImpl::type).map(DataType::toString).toList(), equalTo(List.of(("INTEGER"))));
// values
List<List<Object>> values = getValuesList(resp);
assertMap(values, matchesList().item(List.of(6)));
assertColumnNames(resp.columns(), List.of("id"));
assertColumnTypes(resp.columns(), List.of("integer"));
assertValues(resp.values(), List.of(List.of(6)));
}
}

Expand All @@ -87,12 +77,9 @@ public void testMultipleMatch() {
""";

try (var resp = run(query)) {
assertThat(resp.columns().stream().map(ColumnInfoImpl::name).toList(), equalTo(List.of(("id"))));
assertThat(resp.columns().stream().map(ColumnInfoImpl::type).map(DataType::toString).toList(), equalTo(List.of(("INTEGER"))));
// values
List<List<Object>> values = getValuesList(resp);
assertThat(values.size(), equalTo(2));
assertMap(values, matchesList().item(List.of(1)).item(List.of(6)));
assertColumnNames(resp.columns(), List.of("id"));
assertColumnTypes(resp.columns(), List.of("integer"));
assertValues(resp.values(), List.of(List.of(1), List.of(6)));
}
}

Expand Down Expand Up @@ -121,11 +108,9 @@ public void testNotWhereMatch() {
""";

try (var resp = run(query)) {
assertThat(resp.columns().stream().map(ColumnInfoImpl::name).toList(), equalTo(List.of(("id"))));
assertThat(resp.columns().stream().map(ColumnInfoImpl::type).map(DataType::toString).toList(), equalTo(List.of(("INTEGER"))));
// values
List<List<Object>> values = getValuesList(resp);
assertMap(values, matchesList().item(List.of(5)));
assertColumnNames(resp.columns(), List.of("id"));
assertColumnTypes(resp.columns(), List.of("integer"));
assertValues(resp.values(), List.of(List.of(5)));
}
}

Expand Down

0 comments on commit 7765193

Please sign in to comment.