Skip to content

Commit 449abe6

Browse files
committed
Randomize configuration in function tests, and make it static for the ones that use it
1 parent cc15c9f commit 449abe6

File tree

12 files changed

+90
-43
lines changed

12 files changed

+90
-43
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/EsqlTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public static Configuration configuration(QueryPragmas pragmas) {
545545
}
546546

547547
public static Configuration configuration(String query) {
548-
return configuration(new QueryPragmas(Settings.EMPTY), query);
548+
return configuration(QueryPragmas.EMPTY, query);
549549
}
550550

551551
public static AnalyzerSettings queryClusterSettings() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ protected static List<TestCaseSupplier> withNoRowsExpectingNull(List<TestCaseSup
117117
var newData = testCase.getData().stream().map(td -> td.isMultiRow() ? td.withData(List.of()) : td).toList();
118118

119119
return new TestCaseSupplier.TestCase(
120+
testCase.getSource(),
121+
testCase.getConfiguration(),
120122
newData,
121123
testCase.evaluatorToString(),
122124
testCase.expectedType(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ protected static List<TestCaseSupplier> anyNullIsNull(
179179
}).toList();
180180
TestCaseSupplier.TypedData nulledData = oc.getData().get(finalNullPosition);
181181
return new TestCaseSupplier.TestCase(
182+
oc.getSource(),
183+
oc.getConfiguration(),
182184
data,
183185
evaluatorToString.evaluatorToString(finalNullPosition, nulledData, oc.evaluatorToString()),
184186
expectedType.expectedType(finalNullPosition, nulledData.type(), oc),
@@ -211,6 +213,8 @@ protected static List<TestCaseSupplier> anyNullIsNull(
211213
)
212214
.toList();
213215
return new TestCaseSupplier.TestCase(
216+
oc.getSource(),
217+
oc.getConfiguration(),
214218
data,
215219
equalTo("LiteralsEvaluator[lit=null]"),
216220
expectedType.expectedType(finalNullPosition, DataType.NULL, oc),

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.logging.Logger;
2424
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils;
2525
import org.elasticsearch.test.ESTestCase;
26+
import org.elasticsearch.xpack.esql.ConfigurationTestUtils;
2627
import org.elasticsearch.xpack.esql.EsqlTestUtils;
2728
import org.elasticsearch.xpack.esql.core.expression.Expression;
2829
import org.elasticsearch.xpack.esql.core.expression.Literal;
@@ -42,8 +43,10 @@
4243
import java.time.Period;
4344
import java.util.ArrayList;
4445
import java.util.Arrays;
46+
import java.util.Collection;
4547
import java.util.Collections;
4648
import java.util.List;
49+
import java.util.Map;
4750
import java.util.function.BiFunction;
4851
import java.util.function.BinaryOperator;
4952
import java.util.function.DoubleFunction;
@@ -1570,6 +1573,17 @@ public static String castToDoubleEvaluator(String original, DataType current) {
15701573
throw new UnsupportedOperationException();
15711574
}
15721575

1576+
public static List<TestCaseSupplier> mapTestCases(
1577+
Collection<TestCaseSupplier> suppliers,
1578+
Function<TestCaseSupplier.TestCase, TestCaseSupplier.TestCase> mapper
1579+
) {
1580+
return suppliers.stream().map(supplier ->
1581+
new TestCaseSupplier(supplier.name(), supplier.types(), () ->
1582+
mapper.apply(supplier.get())
1583+
)
1584+
).toList();
1585+
}
1586+
15731587
public static final class TestCase {
15741588
/**
15751589
* The {@link Source} this test case should be run with
@@ -1655,6 +1669,8 @@ public static TestCase typeError(List<TypedData> data, String expectedTypeError)
16551669
Object extra
16561670
) {
16571671
this(
1672+
TEST_SOURCE,
1673+
ConfigurationTestUtils.randomConfiguration(TEST_SOURCE.text(), Map.of()),
16581674
data,
16591675
evaluatorToString,
16601676
expectedType,
@@ -1670,6 +1686,8 @@ public static TestCase typeError(List<TypedData> data, String expectedTypeError)
16701686
}
16711687

16721688
TestCase(
1689+
Source source,
1690+
Configuration configuration,
16731691
List<TypedData> data,
16741692
Matcher<String> evaluatorToString,
16751693
DataType expectedType,
@@ -1682,8 +1700,8 @@ public static TestCase typeError(List<TypedData> data, String expectedTypeError)
16821700
Object extra,
16831701
boolean canBuildEvaluator
16841702
) {
1685-
this.source = TEST_SOURCE;
1686-
this.configuration = TEST_CONFIGURATION;
1703+
this.source = source;
1704+
this.configuration = configuration;
16871705
this.data = data;
16881706
this.evaluatorToString = evaluatorToString;
16891707
this.expectedType = expectedType == null ? null : expectedType.noText();
@@ -1779,11 +1797,34 @@ public Object extra() {
17791797
return extra;
17801798
}
17811799

1800+
/**
1801+
* Build a new {@link TestCase} with new {@link #configuration}.
1802+
*/
1803+
public TestCase withConfiguration(Configuration configuration) {
1804+
return new TestCase(
1805+
source,
1806+
configuration,
1807+
data,
1808+
evaluatorToString,
1809+
expectedType,
1810+
matcher,
1811+
expectedWarnings,
1812+
expectedBuildEvaluatorWarnings,
1813+
expectedTypeError,
1814+
foldingExceptionClass,
1815+
foldingExceptionMessage,
1816+
extra,
1817+
canBuildEvaluator
1818+
);
1819+
}
1820+
17821821
/**
17831822
* Build a new {@link TestCase} with new {@link #data}.
17841823
*/
17851824
public TestCase withData(List<TestCaseSupplier.TypedData> data) {
17861825
return new TestCase(
1826+
source,
1827+
configuration,
17871828
data,
17881829
evaluatorToString,
17891830
expectedType,
@@ -1803,6 +1844,8 @@ public TestCase withData(List<TestCaseSupplier.TypedData> data) {
18031844
*/
18041845
public TestCase withExtra(Object extra) {
18051846
return new TestCase(
1847+
source,
1848+
configuration,
18061849
data,
18071850
evaluatorToString,
18081851
expectedType,
@@ -1819,6 +1862,8 @@ public TestCase withExtra(Object extra) {
18191862

18201863
public TestCase withWarning(String warning) {
18211864
return new TestCase(
1865+
source,
1866+
configuration,
18221867
data,
18231868
evaluatorToString,
18241869
expectedType,
@@ -1839,6 +1884,8 @@ public TestCase withWarning(String warning) {
18391884
*/
18401885
public TestCase withBuildEvaluatorWarning(String warning) {
18411886
return new TestCase(
1887+
source,
1888+
configuration,
18421889
data,
18431890
evaluatorToString,
18441891
expectedType,
@@ -1864,6 +1911,8 @@ private String[] addWarning(String[] warnings, String warning) {
18641911

18651912
public TestCase withFoldingException(Class<? extends Throwable> clazz, String message) {
18661913
return new TestCase(
1914+
source,
1915+
configuration,
18671916
data,
18681917
evaluatorToString,
18691918
expectedType,
@@ -1886,6 +1935,8 @@ public TestCase withFoldingException(Class<? extends Throwable> clazz, String me
18861935
*/
18871936
public TestCase withoutEvaluator() {
18881937
return new TestCase(
1938+
source,
1939+
configuration,
18891940
data,
18901941
evaluatorToString,
18911942
expectedType,

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/AbstractConfigurationFunctionTestCase.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77

88
package org.elasticsearch.xpack.esql.expression.function.scalar;
99

10-
import org.elasticsearch.common.settings.Settings;
11-
import org.elasticsearch.xpack.esql.analysis.AnalyzerSettings;
10+
import org.elasticsearch.xpack.esql.ConfigurationTestUtils;
1211
import org.elasticsearch.xpack.esql.core.expression.Expression;
1312
import org.elasticsearch.xpack.esql.core.tree.Source;
14-
import org.elasticsearch.xpack.esql.core.util.StringUtils;
1513
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
16-
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
1714
import org.elasticsearch.xpack.esql.session.Configuration;
1815

1916
import java.util.List;
2017
import java.util.Map;
2118

19+
import static org.elasticsearch.xpack.esql.ConfigurationTestUtils.randomConfiguration;
20+
import static org.elasticsearch.xpack.esql.ConfigurationTestUtils.randomTables;
2221
import static org.elasticsearch.xpack.esql.SerializationTestUtils.assertSerialization;
2322

2423
public abstract class AbstractConfigurationFunctionTestCase extends AbstractScalarFunctionTestCase {
@@ -35,29 +34,9 @@ public void testSerializationWithConfiguration() {
3534

3635
assertSerialization(expr, config);
3736

38-
Configuration differentConfig = randomValueOtherThan(config, AbstractConfigurationFunctionTestCase::randomConfiguration);
37+
Configuration differentConfig = randomValueOtherThan(config, () -> randomConfiguration(testCase.getSource().text(), randomTables()));
3938

4039
Expression differentExpr = buildWithConfiguration(testCase.getSource(), testCase.getDataAsFields(), differentConfig);
4140
assertNotEquals(expr, differentExpr);
4241
}
43-
44-
private static Configuration randomConfiguration() {
45-
// TODO: Randomize the query and maybe the pragmas.
46-
return new Configuration(
47-
randomZone(),
48-
randomLocale(random()),
49-
randomBoolean() ? null : randomAlphaOfLength(randomInt(64)),
50-
randomBoolean() ? null : randomAlphaOfLength(randomInt(64)),
51-
QueryPragmas.EMPTY,
52-
AnalyzerSettings.QUERY_RESULT_TRUNCATION_MAX_SIZE.getDefault(Settings.EMPTY),
53-
AnalyzerSettings.QUERY_RESULT_TRUNCATION_DEFAULT_SIZE.getDefault(Settings.EMPTY),
54-
StringUtils.EMPTY,
55-
randomBoolean(),
56-
Map.of(),
57-
System.nanoTime(),
58-
randomBoolean(),
59-
AnalyzerSettings.QUERY_TIMESERIES_RESULT_TRUNCATION_MAX_SIZE.getDefault(Settings.EMPTY),
60-
AnalyzerSettings.QUERY_TIMESERIES_RESULT_TRUNCATION_DEFAULT_SIZE.getDefault(Settings.EMPTY)
61-
);
62-
}
6342
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/date/DateExtractTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static Iterable<Object[]> parameters() {
5656
"DateExtractMillisEvaluator[value=Attribute[channel=1], chronoField=Attribute[channel=0], zone=Z]",
5757
DataType.LONG,
5858
equalTo(2023L)
59-
)
59+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
6060
),
6161
new TestCaseSupplier(
6262
List.of(stringType, DataType.DATE_NANOS),
@@ -68,7 +68,7 @@ public static Iterable<Object[]> parameters() {
6868
"DateExtractNanosEvaluator[value=Attribute[channel=1], chronoField=Attribute[channel=0], zone=Z]",
6969
DataType.LONG,
7070
equalTo(2023L)
71-
)
71+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
7272
),
7373
new TestCaseSupplier(
7474
List.of(stringType, DataType.DATE_NANOS),
@@ -80,7 +80,7 @@ public static Iterable<Object[]> parameters() {
8080
"DateExtractNanosEvaluator[value=Attribute[channel=1], chronoField=Attribute[channel=0], zone=Z]",
8181
DataType.LONG,
8282
equalTo(123456L)
83-
)
83+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
8484
),
8585
new TestCaseSupplier(
8686
List.of(stringType, DataType.DATETIME),
@@ -93,7 +93,8 @@ public static Iterable<Object[]> parameters() {
9393
"DateExtractMillisEvaluator[value=Attribute[channel=1], chronoField=Attribute[channel=0], zone=Z]",
9494
DataType.LONG,
9595
is(nullValue())
96-
).withWarning("Line 1:1: evaluation of [source] failed, treating result as null. Only first 20 failures recorded.")
96+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
97+
.withWarning("Line 1:1: evaluation of [source] failed, treating result as null. Only first 20 failures recorded.")
9798
.withWarning(
9899
"Line 1:1: java.lang.IllegalArgumentException: "
99100
+ "No enum constant java.time.temporal.ChronoField.NOT A UNIT"

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/date/DateFormatTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.lucene.util.BytesRef;
1414
import org.elasticsearch.common.time.DateFormatter;
1515
import org.elasticsearch.common.time.DateUtils;
16+
import org.elasticsearch.xpack.esql.EsqlTestUtils;
1617
import org.elasticsearch.xpack.esql.core.expression.Expression;
1718
import org.elasticsearch.xpack.esql.core.tree.Source;
1819
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -23,9 +24,13 @@
2324

2425
import java.time.Instant;
2526
import java.util.ArrayList;
27+
import java.util.Collection;
2628
import java.util.List;
29+
import java.util.function.Function;
2730
import java.util.function.Supplier;
2831

32+
import static java.util.stream.Collectors.toList;
33+
import static org.hamcrest.Matchers.equalTo;
2934
import static org.hamcrest.Matchers.matchesPattern;
3035

3136
public class DateFormatTests extends AbstractConfigurationFunctionTestCase {
@@ -84,6 +89,10 @@ public static Iterable<Object[]> parameters() {
8489
(value) -> new BytesRef(EsqlDataTypeConverter.DEFAULT_DATE_TIME_FORMATTER.formatNanos(DateUtils.toLong((Instant) value))),
8590
List.of()
8691
);
92+
suppliers = TestCaseSupplier.mapTestCases(
93+
suppliers,
94+
testCase -> testCase.withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
95+
);
8796
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, suppliers);
8897
}
8998

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/date/DayNameTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.lucene.BytesRefs;
1515
import org.elasticsearch.common.settings.Settings;
1616
import org.elasticsearch.common.time.DateUtils;
17+
import org.elasticsearch.xpack.esql.ConfigurationTestUtils;
1718
import org.elasticsearch.xpack.esql.analysis.AnalyzerSettings;
1819
import org.elasticsearch.xpack.esql.core.expression.Expression;
1920
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
@@ -62,7 +63,7 @@ public static Iterable<Object[]> parameters() {
6263
Matchers.startsWith("DayNameMillisEvaluator[val=Attribute[channel=0], zoneId=Z, locale=en_US]"),
6364
DataType.KEYWORD,
6465
equalTo(null)
65-
)
66+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
6667
)
6768
);
6869

@@ -78,7 +79,7 @@ private static List<TestCaseSupplier> generateTest(String dateTime, String expec
7879
Matchers.startsWith("DayNameMillisEvaluator[val=Attribute[channel=0], zoneId=Z, locale=en_US]"),
7980
DataType.KEYWORD,
8081
equalTo(new BytesRef(expectedWeekDay))
81-
)
82+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
8283
),
8384
new TestCaseSupplier(
8485
List.of(DataType.DATE_NANOS),
@@ -87,7 +88,7 @@ private static List<TestCaseSupplier> generateTest(String dateTime, String expec
8788
Matchers.is("DayNameNanosEvaluator[val=Attribute[channel=0], zoneId=Z, locale=en_US]"),
8889
DataType.KEYWORD,
8990
equalTo(new BytesRef(expectedWeekDay))
90-
)
91+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
9192
)
9293
);
9394
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/date/MonthNameTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static Iterable<Object[]> parameters() {
7272
Matchers.startsWith("MonthNameMillisEvaluator[val=Attribute[channel=0], zoneId=Z, locale=en_US]"),
7373
DataType.KEYWORD,
7474
equalTo(null)
75-
)
75+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
7676
)
7777
);
7878

@@ -88,7 +88,7 @@ private static List<TestCaseSupplier> generateTest(String dateTime, String expec
8888
Matchers.startsWith("MonthNameMillisEvaluator[val=Attribute[channel=0], zoneId=Z, locale=en_US]"),
8989
DataType.KEYWORD,
9090
equalTo(new BytesRef(expectedMonthName))
91-
)
91+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
9292
),
9393
new TestCaseSupplier(
9494
List.of(DataType.DATE_NANOS),
@@ -97,7 +97,7 @@ private static List<TestCaseSupplier> generateTest(String dateTime, String expec
9797
Matchers.is("MonthNameNanosEvaluator[val=Attribute[channel=0], zoneId=Z, locale=en_US]"),
9898
DataType.KEYWORD,
9999
equalTo(new BytesRef(expectedMonthName))
100-
)
100+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
101101
)
102102
);
103103
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/date/NowTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static Iterable<Object[]> parameters() {
4242
matchesPattern("LiteralsEvaluator\\[lit=.*]"),
4343
DataType.DATETIME,
4444
equalTo(TestCaseSupplier.TEST_CONFIGURATION.now().toInstant().toEpochMilli())
45-
)
45+
).withConfiguration(TestCaseSupplier.TEST_CONFIGURATION)
4646
)
4747
)
4848
);

0 commit comments

Comments
 (0)