Adds mutate function for all pipeline aggregation tests (#26058)

* Adds mutate function for all metric aggregation tests

 Relates to #25929

* Fixes review comments
This commit is contained in:
Colin Goodheart-Smithe 2017-08-07 10:09:41 +01:00 committed by GitHub
parent 14ba36977e
commit 8fda74aee1
8 changed files with 178 additions and 1 deletions

View File

@ -71,6 +71,10 @@ public class InternalSimpleValue extends InternalNumericMetricsAggregation.Singl
return value;
}
DocValueFormat formatter() {
return format;
}
@Override
public InternalSimpleValue doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
throw new UnsupportedOperationException("Not supported");

View File

@ -81,6 +81,10 @@ public class InternalBucketMetricValue extends InternalNumericMetricsAggregation
return keys;
}
DocValueFormat formatter() {
return format;
}
@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
throw new UnsupportedOperationException("Not supported");

View File

@ -100,6 +100,10 @@ public class InternalPercentilesBucket extends InternalNumericMetricsAggregation
return format.format(percentile(percent));
}
DocValueFormat formatter() {
return format;
}
@Override
public Iterator<Percentile> iterator() {
return new Iter(percents, percentiles);

View File

@ -64,6 +64,14 @@ public class InternalDerivative extends InternalSimpleValue implements Derivativ
return normalizationFactor > 0 ? (value() / normalizationFactor) : value();
}
DocValueFormat formatter() {
return format;
}
double getNormalizationFactor() {
return normalizationFactor;
}
@Override
public Object getProperty(List<String> path) {
if (path.isEmpty()) {

View File

@ -21,10 +21,11 @@ package org.elasticsearch.search.aggregations.pipeline;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.test.InternalAggregationTestCase;
import org.elasticsearch.search.aggregations.ParsedAggregation;
import org.elasticsearch.test.InternalAggregationTestCase;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -67,4 +68,33 @@ public class InternalSimpleValueTests extends InternalAggregationTestCase<Intern
assertEquals(parsed.value(), Double.NaN, 0);
}
}
@Override
protected InternalSimpleValue mutateInstance(InternalSimpleValue instance) {
String name = instance.getName();
double value = instance.getValue();
DocValueFormat formatter = instance.formatter();
List<PipelineAggregator> pipelineAggregators = instance.pipelineAggregators();
Map<String, Object> metaData = instance.getMetaData();
switch (between(0, 2)) {
case 0:
name += randomAlphaOfLength(5);
break;
case 1:
value += between(1, 100);
break;
case 2:
if (metaData == null) {
metaData = new HashMap<>(1);
} else {
metaData = new HashMap<>(instance.getMetaData());
}
metaData.put(randomAlphaOfLength(15), randomInt());
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new InternalSimpleValue(name, value, formatter, pipelineAggregators, metaData);
}
}

View File

@ -20,11 +20,14 @@
package org.elasticsearch.search.aggregations.pipeline.bucketmetrics;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.ParsedAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.test.InternalAggregationTestCase;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -72,4 +75,37 @@ public class InternalBucketMetricValueTests extends InternalAggregationTestCase<
assertEquals(parsed.value(), Double.NEGATIVE_INFINITY, 0);
}
}
@Override
protected InternalBucketMetricValue mutateInstance(InternalBucketMetricValue instance) {
String name = instance.getName();
String[] keys = instance.keys();
double value = instance.value();
DocValueFormat formatter = instance.formatter();
List<PipelineAggregator> pipelineAggregators = instance.pipelineAggregators();
Map<String, Object> metaData = instance.getMetaData();
switch (between(0, 3)) {
case 0:
name += randomAlphaOfLength(5);
break;
case 1:
value += between(1, 100);
break;
case 2:
keys = Arrays.copyOf(keys, keys.length + 1);
keys[keys.length - 1] = randomAlphaOfLengthBetween(1, 20);
break;
case 3:
if (metaData == null) {
metaData = new HashMap<>(1);
} else {
metaData = new HashMap<>(instance.getMetaData());
}
metaData.put(randomAlphaOfLength(15), randomInt());
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new InternalBucketMetricValue(name, keys, value, formatter, pipelineAggregators, metaData);
}
}

View File

@ -28,7 +28,10 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.test.InternalAggregationTestCase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -124,4 +127,59 @@ public class InternalPercentilesBucketTests extends InternalAggregationTestCase<
protected Predicate<String> excludePathsFromXContentInsertion() {
return path -> path.endsWith(CommonFields.VALUES.getPreferredName());
}
@Override
protected InternalPercentilesBucket mutateInstance(InternalPercentilesBucket instance) {
String name = instance.getName();
double[] percents = extractPercents(instance);
double[] percentiles = extractPercentiles(instance);
;
DocValueFormat formatter = instance.formatter();
List<PipelineAggregator> pipelineAggregators = instance.pipelineAggregators();
Map<String, Object> metaData = instance.getMetaData();
switch (between(0, 3)) {
case 0:
name += randomAlphaOfLength(5);
break;
case 1:
percents = Arrays.copyOf(percents, percents.length);
percents[percents.length - 1] = randomDouble();
break;
case 2:
percentiles = Arrays.copyOf(percentiles, percentiles.length);
percentiles[percentiles.length - 1] = randomDouble();
break;
case 3:
if (metaData == null) {
metaData = new HashMap<>(1);
} else {
metaData = new HashMap<>(instance.getMetaData());
}
metaData.put(randomAlphaOfLength(15), randomInt());
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new InternalPercentilesBucket(name, percents, percentiles, formatter, pipelineAggregators, metaData);
}
private double[] extractPercentiles(InternalPercentilesBucket instance) {
List<Double> values = new ArrayList<>();
instance.iterator().forEachRemaining(percentile -> values.add(percentile.getValue()));
double[] valuesArray = new double[values.size()];
for (int i = 0; i < values.size(); i++) {
valuesArray[i] = values.get(i);
}
return valuesArray;
}
private double[] extractPercents(InternalPercentilesBucket instance) {
List<Double> percents = new ArrayList<>();
instance.iterator().forEachRemaining(percentile -> percents.add(percentile.getPercent()));
double[] percentArray = new double[percents.size()];
for (int i = 0; i < percents.size(); i++) {
percentArray[i] = percents.get(i);
}
return percentArray;
}
}

View File

@ -26,6 +26,7 @@ import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.test.InternalAggregationTestCase;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -69,4 +70,36 @@ public class InternalDerivativeTests extends InternalAggregationTestCase<Interna
assertEquals(parsed.value(), Double.NaN, Double.MIN_VALUE);
}
}
@Override
protected InternalDerivative mutateInstance(InternalDerivative instance) {
String name = instance.getName();
double value = instance.getValue();
double normalizationFactor = instance.getNormalizationFactor();
DocValueFormat formatter = instance.formatter();
List<PipelineAggregator> pipelineAggregators = instance.pipelineAggregators();
Map<String, Object> metaData = instance.getMetaData();
switch (between(0, 2)) {
case 0:
name += randomAlphaOfLength(5);
break;
case 1:
value += between(1, 100);
break;
case 2:
normalizationFactor += between(1, 100);
break;
case 3:
if (metaData == null) {
metaData = new HashMap<>(1);
} else {
metaData = new HashMap<>(instance.getMetaData());
}
metaData.put(randomAlphaOfLength(15), randomInt());
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new InternalDerivative(name, value, normalizationFactor, formatter, pipelineAggregators, metaData);
}
}