Skip to content

Commit 8f6ac25

Browse files
authored
ESQL: Separate Aggregate and TimeSeriesAggregate tests to cover timeBucket (elastic#130327)
Continuation of elastic#130218 The previous PR fixed a missing equals/hashCode. This PR adds the test that would have caught that. Technically the test existed, but wasn't covering the `timeBucket` field
1 parent 5a4961b commit 8f6ac25

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/grouping/BucketSerializationTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
public class BucketSerializationTests extends AbstractExpressionSerializationTests<Bucket> {
1717
@Override
1818
protected Bucket createTestInstance() {
19+
return createRandomBucket();
20+
}
21+
22+
public static Bucket createRandomBucket() {
1923
Source source = randomSource();
2024
Expression field = randomChild();
2125
Expression buckets = randomChild();

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plan/logical/AggregateSerializationTests.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ protected Aggregate createTestInstance() {
3232
LogicalPlan child = randomChild(0);
3333
List<Expression> groupings = randomFieldAttributes(0, 5, false).stream().map(a -> (Expression) a).toList();
3434
List<? extends NamedExpression> aggregates = randomAggregates();
35-
if (randomBoolean()) {
36-
return new Aggregate(source, child, groupings, aggregates);
37-
} else {
38-
return new TimeSeriesAggregate(source, child, groupings, aggregates, null);
39-
}
35+
return new Aggregate(source, child, groupings, aggregates);
4036
}
4137

4238
public static List<? extends NamedExpression> randomAggregates() {
@@ -75,11 +71,7 @@ protected Aggregate mutateInstance(Aggregate instance) throws IOException {
7571
);
7672
case 2 -> aggregates = randomValueOtherThan(aggregates, AggregateSerializationTests::randomAggregates);
7773
}
78-
if (instance instanceof TimeSeriesAggregate) {
79-
return new TimeSeriesAggregate(instance.source(), child, groupings, aggregates, null);
80-
} else {
81-
return new Aggregate(instance.source(), child, groupings, aggregates);
82-
}
74+
return new Aggregate(instance.source(), child, groupings, aggregates);
8375
}
8476

8577
@Override
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.plan.logical;
9+
10+
import org.elasticsearch.xpack.esql.core.expression.Expression;
11+
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
12+
import org.elasticsearch.xpack.esql.core.tree.Source;
13+
import org.elasticsearch.xpack.esql.expression.function.grouping.Bucket;
14+
import org.elasticsearch.xpack.esql.expression.function.grouping.BucketSerializationTests;
15+
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
public class TimeSeriesAggregateSerializationTests extends AbstractLogicalPlanSerializationTests<TimeSeriesAggregate> {
20+
@Override
21+
protected TimeSeriesAggregate createTestInstance() {
22+
Source source = randomSource();
23+
LogicalPlan child = randomChild(0);
24+
List<Expression> groupings = randomFieldAttributes(0, 5, false).stream().map(a -> (Expression) a).toList();
25+
List<? extends NamedExpression> aggregates = AggregateSerializationTests.randomAggregates();
26+
Bucket timeBucket = BucketSerializationTests.createRandomBucket();
27+
return new TimeSeriesAggregate(source, child, groupings, aggregates, timeBucket);
28+
}
29+
30+
@Override
31+
protected TimeSeriesAggregate mutateInstance(TimeSeriesAggregate instance) throws IOException {
32+
LogicalPlan child = instance.child();
33+
List<Expression> groupings = instance.groupings();
34+
List<? extends NamedExpression> aggregates = instance.aggregates();
35+
Bucket timeBucket = instance.timeBucket();
36+
switch (between(0, 3)) {
37+
case 0 -> child = randomValueOtherThan(child, () -> randomChild(0));
38+
case 1 -> groupings = randomValueOtherThan(
39+
groupings,
40+
() -> randomFieldAttributes(0, 5, false).stream().map(a -> (Expression) a).toList()
41+
);
42+
case 2 -> aggregates = randomValueOtherThan(aggregates, AggregateSerializationTests::randomAggregates);
43+
case 3 -> timeBucket = randomValueOtherThan(timeBucket, BucketSerializationTests::createRandomBucket);
44+
}
45+
return new TimeSeriesAggregate(instance.source(), child, groupings, aggregates, timeBucket);
46+
}
47+
48+
@Override
49+
protected boolean alwaysEmptySource() {
50+
return true;
51+
}
52+
}

0 commit comments

Comments
 (0)