Skip to content

Commit ea9d511

Browse files
authored
Tests: Add unit test for InternalChildren (#23261)
Relates to #22278
1 parent 76d6b87 commit ea9d511

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregation.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.ArrayList;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.Objects;
3334

3435
/**
3536
* A base class for all the single bucket aggregations.
@@ -80,7 +81,7 @@ public InternalAggregations getAggregations() {
8081
/**
8182
* Create a new copy of this {@link Aggregation} with the same settings as
8283
* this {@link Aggregation} and contains the provided sub-aggregations.
83-
*
84+
*
8485
* @param subAggregations
8586
* the buckets to use in the new {@link Aggregation}
8687
* @return the new {@link Aggregation}
@@ -133,4 +134,16 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
133134
aggregations.toXContentInternal(builder, params);
134135
return builder;
135136
}
137+
138+
@Override
139+
protected boolean doEquals(Object obj) {
140+
InternalSingleBucketAggregation other = (InternalSingleBucketAggregation) obj;
141+
return Objects.equals(docCount, other.docCount) &&
142+
Objects.equals(aggregations, other.aggregations);
143+
}
144+
145+
@Override
146+
protected int doHashCode() {
147+
return Objects.hash(docCount, aggregations);
148+
}
136149
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.children;
21+
22+
import org.elasticsearch.common.io.stream.Writeable.Reader;
23+
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.InternalAggregation;
25+
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
26+
import org.elasticsearch.search.aggregations.InternalAggregations;
27+
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
28+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
29+
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
public class InternalChildrenTests extends InternalAggregationTestCase<InternalChildren> {
35+
36+
@Override
37+
protected InternalChildren createTestInstance(String name, List<PipelineAggregator> pipelineAggregators,
38+
Map<String, Object> metaData) {
39+
// we shouldn't use the full long range here since we sum doc count on reduce, and don't want to overflow the long range there
40+
long docCount = randomIntBetween(0, Integer.MAX_VALUE);
41+
int numAggregations = randomIntBetween(0, 20);
42+
List<InternalAggregation> aggs = new ArrayList<>(numAggregations);
43+
for (int i = 0; i < numAggregations; i++) {
44+
aggs.add(new InternalMax(randomAsciiOfLength(5), randomDouble(),
45+
randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), pipelineAggregators,
46+
metaData));
47+
}
48+
// don't randomize the name parameter, since InternalSingleBucketAggregation#doReduce asserts its the same for all reduced aggs
49+
return new InternalChildren("childAgg", docCount, new InternalAggregations(aggs), pipelineAggregators, metaData);
50+
}
51+
52+
@Override
53+
protected void assertReduced(InternalChildren reduced, List<InternalChildren> inputs) {
54+
long expectedDocCount = 0;
55+
for (Children input : inputs) {
56+
expectedDocCount += input.getDocCount();
57+
}
58+
assertEquals(expectedDocCount, reduced.getDocCount());
59+
}
60+
61+
@Override
62+
protected Reader<InternalChildren> instanceReader() {
63+
return InternalChildren::new;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)