Skip to content

Commit

Permalink
SQL: [Test] Fix and enable test with randomness (#34850)
Browse files Browse the repository at this point in the history
Increase minimum number of elements for List<> ctor arguments
for specific classes that validate the size of the list.

Fixes: #34754
  • Loading branch information
Marios Trivyzas committed Oct 29, 2018
1 parent c9ae192 commit 731e48b
Showing 1 changed file with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
import org.elasticsearch.xpack.sql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Avg;
import org.elasticsearch.xpack.sql.expression.function.aggregate.InnerAggregate;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Percentile;
import org.elasticsearch.xpack.sql.expression.function.aggregate.PercentileRanks;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Percentiles;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.AggExtractorInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.BinaryPipesTests;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.ConstantProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.predicate.In;
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.FullTextPredicate;
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.InPipe;
import org.elasticsearch.xpack.sql.expression.predicate.regex.LikePattern;
import org.elasticsearch.xpack.sql.tree.NodeTests.ChildrenAreAProperty;
import org.elasticsearch.xpack.sql.tree.NodeTests.Dummy;
Expand All @@ -41,6 +46,7 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
Expand Down Expand Up @@ -78,6 +84,10 @@
* </ul>
*/
public class NodeSubclassTests<T extends B, B extends Node<B>> extends ESTestCase {

private static final List<Class<? extends Node<?>>> CLASSES_WITH_MIN_TWO_CHILDREN = Arrays.asList(
In.class, InPipe.class, Percentile.class, Percentiles.class, PercentileRanks.class);

private final Class<T> subclass;

public NodeSubclassTests(Class<T> subclass) {
Expand Down Expand Up @@ -147,7 +157,6 @@ public void testTransform() throws Exception {
/**
* Test {@link Node#replaceChildren} implementation on {@link #subclass}.
*/
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/34775")
public void testReplaceChildren() throws Exception {
Constructor<T> ctor = longestCtor(subclass);
Object[] nodeCtorArgs = ctorArgs(ctor);
Expand Down Expand Up @@ -343,20 +352,14 @@ private Object makeArg(Type argType) {
*/
@SuppressWarnings("unchecked")
private static Object makeArg(Class<? extends Node<?>> toBuildClass, Type argType) throws Exception {

if (argType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) argType;
if (pt.getRawType() == Map.class) {
Map<Object, Object> map = new HashMap<>();
int size = between(0, 10);
while (map.size() < size) {
Object key = makeArg(toBuildClass, pt.getActualTypeArguments()[0]);
Object value = makeArg(toBuildClass, pt.getActualTypeArguments()[1]);
map.put(key, value);
}
return map;
return makeMap(toBuildClass, pt);
}
if (pt.getRawType() == List.class) {
return makeList(toBuildClass, pt, between(1, 10));
return makeList(toBuildClass, pt);
}
if (pt.getRawType() == EnumSet.class) {
@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -512,6 +515,10 @@ public boolean equals(Object obj) {
}
}

private static List<?> makeList(Class<? extends Node<?>> toBuildClass, ParameterizedType listType) throws Exception {
return makeList(toBuildClass, listType, randomSizeForCollection(toBuildClass));
}

private static List<?> makeList(Class<? extends Node<?>> toBuildClass, ParameterizedType listType, int size) throws Exception {
List<Object> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
Expand All @@ -520,6 +527,27 @@ private static List<?> makeList(Class<? extends Node<?>> toBuildClass, Parameter
return list;
}

private static Object makeMap(Class<? extends Node<?>> toBuildClass, ParameterizedType pt) throws Exception {
Map<Object, Object> map = new HashMap<>();
int size = randomSizeForCollection(toBuildClass);
while (map.size() < size) {
Object key = makeArg(toBuildClass, pt.getActualTypeArguments()[0]);
Object value = makeArg(toBuildClass, pt.getActualTypeArguments()[1]);
map.put(key, value);
}
return map;
}

private static int randomSizeForCollection(Class<? extends Node<?>> toBuildClass) {
int minCollectionLength = 0;
int maxCollectionLength = 10;

if (CLASSES_WITH_MIN_TWO_CHILDREN.stream().anyMatch(c -> c == toBuildClass)) {
minCollectionLength = 2;
}
return between(minCollectionLength, maxCollectionLength);
}

private List<?> makeListOfSameSizeOtherThan(Type listType, List<?> original) throws Exception {
if (original.isEmpty()) {
throw new IllegalArgumentException("Can't make a different empty list");
Expand Down

0 comments on commit 731e48b

Please sign in to comment.