Skip to content

Commit e71d9c1

Browse files
authored
Tests: Add unit test for InternalDateHistogram (#23402)
Relates to #22278
1 parent a37c759 commit e71d9c1

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalDateHistogram.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.List;
4242
import java.util.ListIterator;
4343
import java.util.Map;
44+
import java.util.Objects;
4445

4546
/**
4647
* Implementation of {@link Histogram}.
@@ -76,6 +77,24 @@ public Bucket(StreamInput in, boolean keyed, DocValueFormat format) throws IOExc
7677
aggregations = InternalAggregations.readAggregations(in);
7778
}
7879

80+
@Override
81+
public boolean equals(Object obj) {
82+
if (obj == null || obj.getClass() != InternalDateHistogram.Bucket.class) {
83+
return false;
84+
}
85+
InternalDateHistogram.Bucket that = (InternalDateHistogram.Bucket) obj;
86+
// No need to take the keyed and format parameters into account,
87+
// they are already stored and tested on the InternalDateHistogram object
88+
return key == that.key
89+
&& docCount == that.docCount
90+
&& Objects.equals(aggregations, that.aggregations);
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
return Objects.hash(getClass(), key, docCount, aggregations);
96+
}
97+
7998
@Override
8099
public void writeTo(StreamOutput out) throws IOException {
81100
out.writeLong(key);
@@ -169,6 +188,21 @@ void writeTo(StreamOutput out) throws IOException {
169188
out.writeOptionalWriteable(bounds);
170189
}
171190

191+
@Override
192+
public boolean equals(Object obj) {
193+
if (obj == null || getClass() != obj.getClass()) {
194+
return false;
195+
}
196+
EmptyBucketInfo that = (EmptyBucketInfo) obj;
197+
return Objects.equals(rounding, that.rounding)
198+
&& Objects.equals(bounds, that.bounds)
199+
&& Objects.equals(subAggregations, that.subAggregations);
200+
}
201+
202+
@Override
203+
public int hashCode() {
204+
return Objects.hash(getClass(), rounding, bounds, subAggregations);
205+
}
172206
}
173207

174208
private final List<Bucket> buckets;
@@ -446,4 +480,21 @@ public InternalAggregation createAggregation(List<MultiBucketsAggregation.Bucket
446480
public Bucket createBucket(Number key, long docCount, InternalAggregations aggregations) {
447481
return new Bucket(key.longValue(), docCount, keyed, format, aggregations);
448482
}
483+
484+
@Override
485+
protected boolean doEquals(Object obj) {
486+
InternalDateHistogram that = (InternalDateHistogram) obj;
487+
return Objects.equals(buckets, that.buckets)
488+
&& Objects.equals(order, that.order)
489+
&& Objects.equals(format, that.format)
490+
&& Objects.equals(keyed, that.keyed)
491+
&& Objects.equals(minDocCount, that.minDocCount)
492+
&& Objects.equals(offset, that.offset)
493+
&& Objects.equals(emptyBucketInfo, that.emptyBucketInfo);
494+
}
495+
496+
@Override
497+
protected int doHashCode() {
498+
return Objects.hash(buckets, order, format, keyed, minDocCount, offset, emptyBucketInfo);
499+
}
449500
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search.aggregations.bucket.histogram;
21+
22+
import org.apache.lucene.util.TestUtil;
23+
import org.elasticsearch.common.io.stream.Writeable;
24+
import org.elasticsearch.common.unit.TimeValue;
25+
import org.elasticsearch.search.DocValueFormat;
26+
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
27+
import org.elasticsearch.search.aggregations.InternalAggregations;
28+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
29+
import org.joda.time.DateTime;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.TreeMap;
35+
36+
import static org.elasticsearch.common.unit.TimeValue.timeValueHours;
37+
import static org.elasticsearch.common.unit.TimeValue.timeValueMinutes;
38+
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
39+
40+
public class InternalDateHistogramTests extends InternalAggregationTestCase<InternalDateHistogram> {
41+
42+
@Override
43+
protected InternalDateHistogram createTestInstance(String name, List<PipelineAggregator> pipelineAggregators,
44+
Map<String, Object> metaData) {
45+
46+
boolean keyed = randomBoolean();
47+
DocValueFormat format = DocValueFormat.RAW;
48+
int nbBuckets = randomInt(10);
49+
List<InternalDateHistogram.Bucket> buckets = new ArrayList<>(nbBuckets);
50+
long startingDate = System.currentTimeMillis();
51+
52+
long interval = randomIntBetween(1, 3);
53+
long intervalMillis = randomFrom(timeValueSeconds(interval), timeValueMinutes(interval), timeValueHours(interval)).getMillis();
54+
55+
for (int i = 0; i < nbBuckets; i++) {
56+
long key = startingDate + (intervalMillis * i);
57+
buckets.add(i, new InternalDateHistogram.Bucket(key, randomIntBetween(1, 100), keyed, format, InternalAggregations.EMPTY));
58+
}
59+
60+
InternalOrder order = (InternalOrder) randomFrom(InternalHistogram.Order.KEY_ASC, InternalHistogram.Order.KEY_DESC);
61+
return new InternalDateHistogram(name, buckets, order, 1, 0L, null, format, keyed, pipelineAggregators, metaData);
62+
}
63+
64+
@Override
65+
protected void assertReduced(InternalDateHistogram reduced, List<InternalDateHistogram> inputs) {
66+
Map<Long, Long> expectedCounts = new TreeMap<>();
67+
for (Histogram histogram : inputs) {
68+
for (Histogram.Bucket bucket : histogram.getBuckets()) {
69+
expectedCounts.compute(((DateTime) bucket.getKey()).getMillis(),
70+
(key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount());
71+
}
72+
}
73+
Map<Long, Long> actualCounts = new TreeMap<>();
74+
for (Histogram.Bucket bucket : reduced.getBuckets()) {
75+
actualCounts.compute(((DateTime) bucket.getKey()).getMillis(),
76+
(key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount());
77+
}
78+
assertEquals(expectedCounts, actualCounts);
79+
}
80+
81+
@Override
82+
protected Writeable.Reader<InternalDateHistogram> instanceReader() {
83+
return InternalDateHistogram::new;
84+
}
85+
}

0 commit comments

Comments
 (0)