Skip to content

Commit a54daad

Browse files
authored
Tests InternalSingleBucketAggregation subclasses (#23388)
Adds a common base class for testing subclasses of `InternalSingleBucketAggregation`. They are so similar they call into question the utility of having all of these classes. We maybe could just use `InternalSingleBucketAggregation` in all those cases.... But for now, let's test the classes! Relates to #22278
1 parent 64e1938 commit a54daad

13 files changed

Lines changed: 340 additions & 39 deletions

File tree

buildSrc/src/main/resources/checkstyle_suppressions.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,14 @@
437437
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]InternalMultiBucketAggregation.java" checks="LineLength" />
438438
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]ValuesSourceAggregationBuilder.java" checks="LineLength" />
439439
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]BucketsAggregator.java" checks="LineLength" />
440-
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]InternalSingleBucketAggregation.java" checks="LineLength" />
441440
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]children[/\\]ChildrenParser.java" checks="LineLength" />
442441
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]children[/\\]ParentToChildrenAggregator.java" checks="LineLength" />
443-
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]filter[/\\]InternalFilter.java" checks="LineLength" />
444442
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]filters[/\\]FiltersAggregator.java" checks="LineLength" />
445443
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]filters[/\\]FiltersParser.java" checks="LineLength" />
446444
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]filters[/\\]InternalFilters.java" checks="LineLength" />
447445
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]geogrid[/\\]GeoHashGridAggregator.java" checks="LineLength" />
448446
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]histogram[/\\]HistogramAggregator.java" checks="LineLength" />
449-
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]missing[/\\]InternalMissing.java" checks="LineLength" />
450447
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]missing[/\\]MissingAggregator.java" checks="LineLength" />
451-
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]nested[/\\]InternalReverseNested.java" checks="LineLength" />
452448
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]nested[/\\]ReverseNestedAggregator.java" checks="LineLength" />
453449
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]range[/\\]RangeAggregator.java" checks="LineLength" />
454450
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]search[/\\]aggregations[/\\]bucket[/\\]significant[/\\]GlobalOrdinalsSignificantTermsAggregator.java" checks="LineLength" />

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public abstract class InternalSingleBucketAggregation extends InternalAggregatio
4747
* @param docCount The document count in the single bucket.
4848
* @param aggregations The already built sub-aggregations that are associated with the bucket.
4949
*/
50-
protected InternalSingleBucketAggregation(String name, long docCount, InternalAggregations aggregations, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
50+
protected InternalSingleBucketAggregation(String name, long docCount, InternalAggregations aggregations,
51+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
5152
super(name, pipelineAggregators, metaData);
5253
this.docCount = docCount;
5354
this.aggregations = aggregations;

core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
1920
package org.elasticsearch.search.aggregations.bucket.filter;
2021

2122
import org.elasticsearch.common.io.stream.StreamInput;
@@ -28,7 +29,8 @@
2829
import java.util.Map;
2930

3031
public class InternalFilter extends InternalSingleBucketAggregation implements Filter {
31-
InternalFilter(String name, long docCount, InternalAggregations subAggregations, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
32+
InternalFilter(String name, long docCount, InternalAggregations subAggregations, List<PipelineAggregator> pipelineAggregators,
33+
Map<String, Object> metaData) {
3234
super(name, docCount, subAggregations, pipelineAggregators, metaData);
3335
}
3436

core/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissing.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
import java.util.Map;
2929

3030
public class InternalMissing extends InternalSingleBucketAggregation implements Missing {
31-
InternalMissing(String name, long docCount, InternalAggregations aggregations, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
31+
InternalMissing(String name, long docCount, InternalAggregations aggregations, List<PipelineAggregator> pipelineAggregators,
32+
Map<String, Object> metaData) {
3233
super(name, docCount, aggregations, pipelineAggregators, metaData);
3334
}
3435

core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNested.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
* Result of the {@link ReverseNestedAggregator}.
3232
*/
3333
public class InternalReverseNested extends InternalSingleBucketAggregation implements ReverseNested {
34-
public InternalReverseNested(String name, long docCount, InternalAggregations aggregations, List<PipelineAggregator> pipelineAggregators,
35-
Map<String, Object> metaData) {
34+
public InternalReverseNested(String name, long docCount, InternalAggregations aggregations,
35+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
3636
super(name, docCount, aggregations, pipelineAggregators, metaData);
3737
}
3838

core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ protected T createUnmappedInstance(String name,
5050
}
5151

5252
public final void testReduceRandom() {
53+
String name = randomAsciiOfLength(5);
5354
List<T> inputs = new ArrayList<>();
5455
List<InternalAggregation> toReduce = new ArrayList<>();
5556
int toReduceSize = between(1, 200);
5657
for (int i = 0; i < toReduceSize; i++) {
57-
T t = randomBoolean() ? createUnmappedInstance() : createTestInstance();
58+
T t = randomBoolean() ? createUnmappedInstance(name) : createTestInstance(name);
5859
inputs.add(t);
5960
toReduce.add(t);
6061
}
@@ -87,7 +88,10 @@ protected ScriptService mockScriptService() {
8788

8889
@Override
8990
protected final T createTestInstance() {
90-
String name = randomAsciiOfLength(5);
91+
return createTestInstance(randomAsciiOfLength(5));
92+
}
93+
94+
private T createTestInstance(String name) {
9195
List<PipelineAggregator> pipelineAggregators = new ArrayList<>();
9296
// TODO populate pipelineAggregators
9397
Map<String, Object> metaData = new HashMap<>();
@@ -99,8 +103,7 @@ protected final T createTestInstance() {
99103
}
100104

101105
/** Return an instance on an unmapped field. */
102-
protected final T createUnmappedInstance() {
103-
String name = randomAsciiOfLength(5);
106+
protected final T createUnmappedInstance(String name) {
104107
List<PipelineAggregator> pipelineAggregators = new ArrayList<>();
105108
// TODO populate pipelineAggregators
106109
Map<String, Object> metaData = new HashMap<>();
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;
21+
22+
import org.elasticsearch.search.DocValueFormat;
23+
import org.elasticsearch.search.aggregations.InternalAggregation;
24+
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.InternalAggregations;
26+
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
27+
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
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+
import static java.util.Collections.emptyList;
35+
import static java.util.Collections.emptyMap;
36+
37+
public abstract class InternalSingleBucketAggregationTestCase<T extends InternalSingleBucketAggregation>
38+
extends InternalAggregationTestCase<T> {
39+
private final boolean hasInternalMax = randomBoolean();
40+
private final boolean hasInternalMin = randomBoolean();
41+
42+
protected abstract T createTestInstance(String name, long docCount, InternalAggregations aggregations,
43+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData);
44+
protected abstract void extraAssertReduced(T reduced, List<T> inputs);
45+
46+
@Override
47+
protected final T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
48+
List<InternalAggregation> internal = new ArrayList<>();
49+
if (hasInternalMax) {
50+
internal.add(new InternalMax("max", randomDouble(),
51+
randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(),
52+
emptyMap()));
53+
}
54+
if (hasInternalMin) {
55+
internal.add(new InternalMin("min", randomDouble(),
56+
randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(),
57+
emptyMap()));
58+
}
59+
// 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
60+
long docCount = between(0, Integer.MAX_VALUE);
61+
return createTestInstance(name, docCount, new InternalAggregations(internal), pipelineAggregators, metaData);
62+
}
63+
64+
@Override
65+
protected final void assertReduced(T reduced, List<T> inputs) {
66+
assertEquals(inputs.stream().mapToLong(InternalSingleBucketAggregation::getDocCount).sum(), reduced.getDocCount());
67+
if (hasInternalMax) {
68+
double expected = inputs.stream().mapToDouble(i -> {
69+
InternalMax max = i.getAggregations().get("max");
70+
return max.getValue();
71+
}).max().getAsDouble();
72+
InternalMax reducedMax = reduced.getAggregations().get("max");
73+
assertEquals(expected, reducedMax.getValue(), 0);
74+
}
75+
if (hasInternalMin) {
76+
double expected = inputs.stream().mapToDouble(i -> {
77+
InternalMin min = i.getAggregations().get("min");
78+
return min.getValue();
79+
}).min().getAsDouble();
80+
InternalMin reducedMin = reduced.getAggregations().get("min");
81+
assertEquals(expected, reducedMin.getValue(), 0);
82+
}
83+
extraAssertReduced(reduced, inputs);
84+
}
85+
}

core/src/test/java/org/elasticsearch/search/aggregations/bucket/children/InternalChildrenTests.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,23 @@
2020
package org.elasticsearch.search.aggregations.bucket.children;
2121

2222
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;
2623
import org.elasticsearch.search.aggregations.InternalAggregations;
27-
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
24+
import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase;
2825
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2926

30-
import java.util.ArrayList;
3127
import java.util.List;
3228
import java.util.Map;
3329

34-
public class InternalChildrenTests extends InternalAggregationTestCase<InternalChildren> {
35-
30+
public class InternalChildrenTests extends InternalSingleBucketAggregationTestCase<InternalChildren> {
3631
@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);
32+
protected InternalChildren createTestInstance(String name, long docCount, InternalAggregations aggregations,
33+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
34+
return new InternalChildren(name, docCount, aggregations, pipelineAggregators, metaData);
5035
}
5136

5237
@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());
38+
protected void extraAssertReduced(InternalChildren reduced, List<InternalChildren> inputs) {
39+
// Nothing extra to assert
5940
}
6041

6142
@Override
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.filter;
21+
22+
import org.elasticsearch.common.io.stream.Writeable.Reader;
23+
import org.elasticsearch.search.aggregations.InternalAggregations;
24+
import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
public class InternalFilterTests extends InternalSingleBucketAggregationTestCase<InternalFilter> {
31+
@Override
32+
protected InternalFilter createTestInstance(String name, long docCount, InternalAggregations aggregations,
33+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
34+
return new InternalFilter(name, docCount, aggregations, pipelineAggregators, metaData);
35+
}
36+
37+
@Override
38+
protected void extraAssertReduced(InternalFilter reduced, List<InternalFilter> inputs) {
39+
// Nothing extra to assert
40+
}
41+
42+
@Override
43+
protected Reader<InternalFilter> instanceReader() {
44+
return InternalFilter::new;
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.global;
21+
22+
import org.elasticsearch.common.io.stream.Writeable.Reader;
23+
import org.elasticsearch.search.aggregations.InternalAggregations;
24+
import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
public class InternalGlogbalTests extends InternalSingleBucketAggregationTestCase<InternalGlobal> {
31+
@Override
32+
protected InternalGlobal createTestInstance(String name, long docCount, InternalAggregations aggregations,
33+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
34+
return new InternalGlobal(name, docCount, aggregations, pipelineAggregators, metaData);
35+
}
36+
37+
@Override
38+
protected void extraAssertReduced(InternalGlobal reduced, List<InternalGlobal> inputs) {
39+
// Nothing extra to assert
40+
}
41+
42+
@Override
43+
protected Reader<InternalGlobal> instanceReader() {
44+
return InternalGlobal::new;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)