Browse Source

ESQL: Separate Aggregate and TimeSeriesAggregate tests to cover timeBucket (#130327)

Continuation of https://github.com/elastic/elasticsearch/pull/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
Iván Cea Fontenla 3 months ago
parent
commit
8f6ac258a8

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

@@ -16,6 +16,10 @@ import java.io.IOException;
 public class BucketSerializationTests extends AbstractExpressionSerializationTests<Bucket> {
     @Override
     protected Bucket createTestInstance() {
+        return createRandomBucket();
+    }
+
+    public static Bucket createRandomBucket() {
         Source source = randomSource();
         Expression field = randomChild();
         Expression buckets = randomChild();

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

@@ -32,11 +32,7 @@ public class AggregateSerializationTests extends AbstractLogicalPlanSerializatio
         LogicalPlan child = randomChild(0);
         List<Expression> groupings = randomFieldAttributes(0, 5, false).stream().map(a -> (Expression) a).toList();
         List<? extends NamedExpression> aggregates = randomAggregates();
-        if (randomBoolean()) {
-            return new Aggregate(source, child, groupings, aggregates);
-        } else {
-            return new TimeSeriesAggregate(source, child, groupings, aggregates, null);
-        }
+        return new Aggregate(source, child, groupings, aggregates);
     }
 
     public static List<? extends NamedExpression> randomAggregates() {
@@ -75,11 +71,7 @@ public class AggregateSerializationTests extends AbstractLogicalPlanSerializatio
             );
             case 2 -> aggregates = randomValueOtherThan(aggregates, AggregateSerializationTests::randomAggregates);
         }
-        if (instance instanceof TimeSeriesAggregate) {
-            return new TimeSeriesAggregate(instance.source(), child, groupings, aggregates, null);
-        } else {
-            return new Aggregate(instance.source(), child, groupings, aggregates);
-        }
+        return new Aggregate(instance.source(), child, groupings, aggregates);
     }
 
     @Override

+ 52 - 0
x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plan/logical/TimeSeriesAggregateSerializationTests.java

@@ -0,0 +1,52 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.esql.plan.logical;
+
+import org.elasticsearch.xpack.esql.core.expression.Expression;
+import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
+import org.elasticsearch.xpack.esql.core.tree.Source;
+import org.elasticsearch.xpack.esql.expression.function.grouping.Bucket;
+import org.elasticsearch.xpack.esql.expression.function.grouping.BucketSerializationTests;
+
+import java.io.IOException;
+import java.util.List;
+
+public class TimeSeriesAggregateSerializationTests extends AbstractLogicalPlanSerializationTests<TimeSeriesAggregate> {
+    @Override
+    protected TimeSeriesAggregate createTestInstance() {
+        Source source = randomSource();
+        LogicalPlan child = randomChild(0);
+        List<Expression> groupings = randomFieldAttributes(0, 5, false).stream().map(a -> (Expression) a).toList();
+        List<? extends NamedExpression> aggregates = AggregateSerializationTests.randomAggregates();
+        Bucket timeBucket = BucketSerializationTests.createRandomBucket();
+        return new TimeSeriesAggregate(source, child, groupings, aggregates, timeBucket);
+    }
+
+    @Override
+    protected TimeSeriesAggregate mutateInstance(TimeSeriesAggregate instance) throws IOException {
+        LogicalPlan child = instance.child();
+        List<Expression> groupings = instance.groupings();
+        List<? extends NamedExpression> aggregates = instance.aggregates();
+        Bucket timeBucket = instance.timeBucket();
+        switch (between(0, 3)) {
+            case 0 -> child = randomValueOtherThan(child, () -> randomChild(0));
+            case 1 -> groupings = randomValueOtherThan(
+                groupings,
+                () -> randomFieldAttributes(0, 5, false).stream().map(a -> (Expression) a).toList()
+            );
+            case 2 -> aggregates = randomValueOtherThan(aggregates, AggregateSerializationTests::randomAggregates);
+            case 3 -> timeBucket = randomValueOtherThan(timeBucket, BucketSerializationTests::createRandomBucket);
+        }
+        return new TimeSeriesAggregate(instance.source(), child, groupings, aggregates, timeBucket);
+    }
+
+    @Override
+    protected boolean alwaysEmptySource() {
+        return true;
+    }
+}