From b3695750bc385b2b4734f9bdabb9a43b5e67d4b4 Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Mon, 11 Feb 2019 13:59:31 +0200 Subject: [PATCH] Randomize the time zone properly for the current date test. (#38670) (cherry picked from commit 29abbb8a590cdf4f9e0c0b447d6694bb7223648e) --- .../elasticsearch/xpack/sql/TestUtils.java | 35 +++++++++++++++++++ .../function/FunctionRegistryTests.java | 27 +------------- .../scalar/datetime/CurrentDateTests.java | 17 +++++---- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/TestUtils.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/TestUtils.java index be7f42d3f0c..859fd166e02 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/TestUtils.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/TestUtils.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.sql; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.Protocol; import org.elasticsearch.xpack.sql.session.Configuration; @@ -14,6 +15,14 @@ import org.elasticsearch.xpack.sql.util.DateUtils; import java.time.Clock; import java.time.Duration; import java.time.ZonedDateTime; +import java.time.ZoneId; + +import static org.elasticsearch.test.ESTestCase.randomAlphaOfLength; +import static org.elasticsearch.test.ESTestCase.randomFrom; +import static org.elasticsearch.test.ESTestCase.randomIntBetween; +import static org.elasticsearch.test.ESTestCase.randomNonNegativeLong; +import static org.elasticsearch.test.ESTestCase.randomZone; + public class TestUtils { @@ -35,4 +44,30 @@ public class TestUtils { public static final ZonedDateTime now() { return ZonedDateTime.now(Clock.tick(Clock.system(DateUtils.UTC), Duration.ofMillis(1))); } + + public static Configuration randomConfiguration() { + return new Configuration(randomZone(), + randomIntBetween(0, 1000), + new TimeValue(randomNonNegativeLong()), + new TimeValue(randomNonNegativeLong()), + null, + randomFrom(Mode.values()), + randomAlphaOfLength(10), + randomAlphaOfLength(10), + randomAlphaOfLength(10)); + } + + public static Configuration randomConfiguration(ZoneId providedZoneId) { + return new Configuration(providedZoneId, + randomIntBetween(0, 1000), + new TimeValue(randomNonNegativeLong()), + new TimeValue(randomNonNegativeLong()), + null, + randomFrom(Mode.values()), + randomAlphaOfLength(10), + randomAlphaOfLength(10), + randomAlphaOfLength(10)); + } + } + diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistryTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistryTests.java index 101f4dfe78c..a810dac501e 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistryTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistryTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.sql.expression.function; -import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; import org.elasticsearch.xpack.sql.expression.Expression; @@ -13,7 +12,6 @@ import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction; import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe; import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate; import org.elasticsearch.xpack.sql.parser.ParsingException; -import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.session.Configuration; import org.elasticsearch.xpack.sql.tree.NodeInfo; import org.elasticsearch.xpack.sql.tree.Source; @@ -25,6 +23,7 @@ import java.util.Arrays; import java.util.List; import static java.util.Collections.emptyList; +import static org.elasticsearch.xpack.sql.TestUtils.randomConfiguration; import static org.elasticsearch.xpack.sql.expression.function.FunctionRegistry.def; import static org.elasticsearch.xpack.sql.expression.function.UnresolvedFunction.ResolutionType.DISTINCT; import static org.elasticsearch.xpack.sql.expression.function.UnresolvedFunction.ResolutionType.EXTRACT; @@ -231,30 +230,6 @@ public class FunctionRegistryTests extends ESTestCase { return new UnresolvedFunction(SourceTests.randomSource(), "DUMMY_FUNCTION", resolutionType, Arrays.asList(children)); } - private Configuration randomConfiguration() { - return new Configuration(randomZone(), - randomIntBetween(0, 1000), - new TimeValue(randomNonNegativeLong()), - new TimeValue(randomNonNegativeLong()), - null, - randomFrom(Mode.values()), - randomAlphaOfLength(10), - randomAlphaOfLength(10), - randomAlphaOfLength(10)); - } - - private Configuration randomConfiguration(ZoneId providedZoneId) { - return new Configuration(providedZoneId, - randomIntBetween(0, 1000), - new TimeValue(randomNonNegativeLong()), - new TimeValue(randomNonNegativeLong()), - null, - randomFrom(Mode.values()), - randomAlphaOfLength(10), - randomAlphaOfLength(10), - randomAlphaOfLength(10)); - } - public static class DummyFunction extends ScalarFunction { public DummyFunction(Source source) { super(source, emptyList()); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java index 5eaa9ccd6c2..ff65414bf58 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java @@ -6,18 +6,19 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime; +import org.elasticsearch.xpack.sql.TestUtils; import org.elasticsearch.xpack.sql.expression.Expression; -import org.elasticsearch.xpack.sql.proto.Mode; -import org.elasticsearch.xpack.sql.proto.Protocol; -import org.elasticsearch.xpack.sql.session.Configuration; import org.elasticsearch.xpack.sql.tree.AbstractNodeTestCase; import org.elasticsearch.xpack.sql.tree.Source; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Objects; + public class CurrentDateTests extends AbstractNodeTestCase { public static CurrentDate randomCurrentDate() { - return new CurrentDate(Source.EMPTY, new Configuration(randomZone(), Protocol.FETCH_SIZE, - Protocol.REQUEST_TIMEOUT, Protocol.PAGE_TIMEOUT, null, Mode.PLAIN, null, null, null)); + return new CurrentDate(Source.EMPTY, TestUtils.randomConfiguration()); } @Override @@ -32,8 +33,10 @@ public class CurrentDateTests extends AbstractNodeTestCase Objects.equals(now.getOffset(), o.getRules().getOffset(now.toInstant())), + () -> randomZone()); + return new CurrentDate(instance.source(), TestUtils.randomConfiguration(mutatedZoneId)); } @Override