From eb26e1a2923a37adccc5d417d6819691b950549c Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 6 Feb 2017 18:18:21 +0100 Subject: [PATCH] Add unit tests to histogram aggregations. (#22961) --- .../aggregations/InternalAggregations.java | 13 + .../bucket/histogram/InternalHistogram.java | 51 ++++ .../histogram/HistogramAggregatorTests.java | 248 ++++++++++++++++++ .../histogram/InternalHistogramTests.java | 75 ++++++ 4 files changed, 387 insertions(+) create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregatorTests.java create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogramTests.java diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java index 66e45156caf..b9fca78ec44 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java @@ -33,6 +33,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import static java.util.Collections.emptyMap; @@ -208,4 +209,16 @@ public class InternalAggregations implements Aggregations, ToXContent, Streamabl out.writeNamedWriteableList(aggregations); } + @Override + public boolean equals(Object obj) { + if (obj == null || getClass() != obj.getClass()) { + return false; + } + return aggregations.equals(((InternalAggregations) obj).aggregations); + } + + @Override + public int hashCode() { + return Objects.hash(getClass(), aggregations); + } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java index e7f5b739216..eb90dfae732 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java @@ -38,6 +38,7 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.Objects; /** * Implementation of {@link Histogram}. @@ -72,6 +73,24 @@ public final class InternalHistogram extends InternalMultiBucketAggregation buckets; @@ -429,4 +465,19 @@ public final class InternalHistogram extends InternalMultiBucketAggregation { + + @Override + protected InternalHistogram createTestInstance(String name, List pipelineAggregators, + Map metaData) { + final boolean keyed = randomBoolean(); + final DocValueFormat format = DocValueFormat.RAW; + final int base = randomInt(50) - 30; + final int numBuckets = randomInt(10); + final int interval = randomIntBetween(1, 3); + List buckets = new ArrayList<>(); + for (int i = 0; i < numBuckets; ++i) { + final int docCount = TestUtil.nextInt(random(), 1, 50); + buckets.add(new InternalHistogram.Bucket(base + i * interval, docCount, keyed, format, InternalAggregations.EMPTY)); + } + return new InternalHistogram(name, buckets, (InternalOrder) InternalHistogram.Order.KEY_ASC, + 1, null, format, keyed, pipelineAggregators, metaData); + } + + @Override + protected void assertReduced(InternalHistogram reduced, List inputs) { + Map expectedCounts = new TreeMap<>(); + for (Histogram histogram : inputs) { + for (Histogram.Bucket bucket : histogram.getBuckets()) { + expectedCounts.compute((Double) bucket.getKey(), + (key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount()); + } + } + Map actualCounts = new TreeMap<>(); + for (Histogram.Bucket bucket : reduced.getBuckets()) { + actualCounts.compute((Double) bucket.getKey(), + (key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount()); + } + assertEquals(expectedCounts, actualCounts); + } + + @Override + protected Reader instanceReader() { + return InternalHistogram::new; + } + +}