Skip to content

Commit dd523d5

Browse files
committed
Extracted matchers
1 parent 24e99c1 commit dd523d5

File tree

5 files changed

+115
-80
lines changed

5 files changed

+115
-80
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry;
2222
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
2323
import org.elasticsearch.xpack.esql.expression.function.scalar.AbstractConfigurationFunctionTestCase;
24+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.matchers.DateMillisMatcher;
25+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.matchers.DateNanosMatcher;
2426
import org.elasticsearch.xpack.esql.session.Configuration;
2527
import org.hamcrest.Matcher;
2628
import org.hamcrest.Matchers;
@@ -152,7 +154,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
152154
),
153155
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
154156
DataType.DATETIME,
155-
equalTo(data.expectedDateAsMillis())
157+
new DateMillisMatcher(data.expectedDate())
156158
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
157159
),
158160
new TestCaseSupplier(
@@ -169,7 +171,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
169171
),
170172
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
171173
DataType.DATE_NANOS,
172-
equalTo(DateUtils.toLong(Instant.parse(data.expectedDate())))
174+
new DateNanosMatcher(data.expectedDate())
173175
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
174176
)
175177
)
@@ -189,7 +191,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
189191
),
190192
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
191193
DataType.DATETIME,
192-
equalTo(data.expectedDateAsMillis())
194+
new DateMillisMatcher(data.expectedDate())
193195
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
194196
),
195197
new TestCaseSupplier(
@@ -206,7 +208,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
206208
),
207209
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
208210
DataType.DATE_NANOS,
209-
equalTo(DateUtils.toLong(Instant.parse(data.expectedDate())))
211+
new DateNanosMatcher(data.expectedDate())
210212
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
211213
)
212214
)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.elasticsearch.xpack.esql.expression.function.DocsV3Support;
2121
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
2222
import org.elasticsearch.xpack.esql.expression.function.scalar.AbstractConfigurationFunctionTestCase;
23+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.matchers.DateMillisMatcher;
24+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.matchers.DateNanosMatcher;
2325
import org.elasticsearch.xpack.esql.session.Configuration;
2426
import org.hamcrest.Matcher;
2527
import org.hamcrest.Matchers;
@@ -137,7 +139,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
137139
),
138140
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
139141
DataType.DATETIME,
140-
equalTo(data.expectedDateAsMillis())
142+
new DateMillisMatcher(data.expectedDate())
141143
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
142144
),
143145
new TestCaseSupplier(
@@ -154,7 +156,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
154156
),
155157
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
156158
DataType.DATE_NANOS,
157-
equalTo(DateUtils.toLong(Instant.parse(data.expectedDate())))
159+
new DateNanosMatcher(data.expectedDate())
158160
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
159161
)
160162
)
@@ -174,7 +176,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
174176
),
175177
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
176178
DataType.DATETIME,
177-
equalTo(data.expectedDateAsMillis())
179+
new DateMillisMatcher(data.expectedDate())
178180
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
179181
),
180182
new TestCaseSupplier(
@@ -191,7 +193,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
191193
),
192194
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
193195
DataType.DATE_NANOS,
194-
equalTo(DateUtils.toLong(Instant.parse(data.expectedDate())))
196+
new DateNanosMatcher(data.expectedDate())
195197
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
196198
)
197199
)

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

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.elasticsearch.xpack.esql.core.type.DataType;
1818
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
1919
import org.elasticsearch.xpack.esql.expression.function.scalar.AbstractConfigurationFunctionTestCase;
20+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.matchers.DateMillisMatcher;
21+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.matchers.DateNanosMatcher;
2022
import org.elasticsearch.xpack.esql.session.Configuration;
2123
import org.hamcrest.BaseMatcher;
2224
import org.hamcrest.Matchers;
@@ -62,10 +64,6 @@ public ZoneId zoneId() {
6264
public long inputDateAsMillis() {
6365
return Instant.parse(inputDate).toEpochMilli();
6466
}
65-
66-
public long expectedDateAsMillis() {
67-
return Instant.parse(expectedDate).toEpochMilli();
68-
}
6967
}
7068

7169
public record DurationTestCaseData(Duration duration, String inputDate, @Nullable String zoneIdString, String expectedDate) {
@@ -76,10 +74,6 @@ public ZoneId zoneId() {
7674
public long inputDateAsMillis() {
7775
return Instant.parse(inputDate).toEpochMilli();
7876
}
79-
80-
public long expectedDateAsMillis() {
81-
return Instant.parse(expectedDate).toEpochMilli();
82-
}
8377
}
8478

8579
public static List<DurationTestCaseData> makeTruncDurationTestCases() {
@@ -192,7 +186,7 @@ private static List<TestCaseSupplier> ofDatePeriod(PeriodTestCaseData data) {
192186
),
193187
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
194188
DataType.DATETIME,
195-
new DateMillisMatcher(data.expectedDate()) // equalTo(data.expectedDateAsMillis())
189+
new DateMillisMatcher(data.expectedDate())
196190
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
197191
),
198192
new TestCaseSupplier(
@@ -205,7 +199,7 @@ private static List<TestCaseSupplier> ofDatePeriod(PeriodTestCaseData data) {
205199
),
206200
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
207201
DataType.DATE_NANOS,
208-
new DateNanosMatcher(data.expectedDate()) // equalTo(toNanos(data.expectedDate()))
202+
new DateNanosMatcher(data.expectedDate())
209203
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
210204
)
211205
);
@@ -223,7 +217,7 @@ private static List<TestCaseSupplier> ofDuration(DurationTestCaseData data) {
223217
),
224218
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
225219
DataType.DATETIME,
226-
new DateMillisMatcher(data.expectedDate()) // equalTo(data.expectedDateAsMillis())
220+
new DateMillisMatcher(data.expectedDate())
227221
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
228222
),
229223
new TestCaseSupplier(
@@ -236,7 +230,7 @@ private static List<TestCaseSupplier> ofDuration(DurationTestCaseData data) {
236230
),
237231
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
238232
DataType.DATE_NANOS,
239-
new DateNanosMatcher(data.expectedDate()) // equalTo(toNanos(data.expectedDate()))
233+
new DateNanosMatcher(data.expectedDate())
240234
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
241235
)
242236
);
@@ -275,68 +269,8 @@ private static long toMillis(String timestamp) {
275269
return Instant.parse(timestamp).toEpochMilli();
276270
}
277271

278-
private static long toNanos(String timestamp) {
279-
return DateUtils.toLong(Instant.parse(timestamp));
280-
}
281-
282272
@Override
283273
protected Expression buildWithConfiguration(Source source, List<Expression> args, Configuration configuration) {
284274
return new DateTrunc(source, args.get(0), args.get(1), configuration);
285275
}
286-
287-
public static class DateMillisMatcher extends BaseMatcher<Long> {
288-
private final long timeMillis;
289-
290-
public DateMillisMatcher(String date) {
291-
this.timeMillis = toMillis(date);
292-
}
293-
294-
@Override
295-
public boolean matches(Object item) {
296-
return item instanceof Long && timeMillis == (Long) item;
297-
}
298-
299-
@Override
300-
public void describeMismatch(Object item, org.hamcrest.Description description) {
301-
description.appendText("was ");
302-
if (item instanceof Long l) {
303-
description.appendValue(DEFAULT_DATE_TIME_FORMATTER.formatMillis(l));
304-
} else {
305-
description.appendValue(item);
306-
}
307-
}
308-
309-
@Override
310-
public void describeTo(org.hamcrest.Description description) {
311-
description.appendText(DEFAULT_DATE_TIME_FORMATTER.formatMillis(timeMillis));
312-
}
313-
}
314-
315-
public static class DateNanosMatcher extends BaseMatcher<Long> {
316-
private final long timeNanos;
317-
318-
public DateNanosMatcher(String date) {
319-
this.timeNanos = toNanos(date);
320-
}
321-
322-
@Override
323-
public boolean matches(Object item) {
324-
return item instanceof Long && timeNanos == (Long) item;
325-
}
326-
327-
@Override
328-
public void describeMismatch(Object item, org.hamcrest.Description description) {
329-
description.appendText("was ");
330-
if (item instanceof Long l) {
331-
description.appendValue(DEFAULT_DATE_TIME_FORMATTER.formatNanos(l));
332-
} else {
333-
description.appendValue(item);
334-
}
335-
}
336-
337-
@Override
338-
public void describeTo(org.hamcrest.Description description) {
339-
description.appendText(DEFAULT_DATE_TIME_FORMATTER.formatNanos(timeNanos));
340-
}
341-
}
342276
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.expression.function.scalar.date.matchers;
9+
10+
import org.hamcrest.BaseMatcher;
11+
12+
import java.time.Instant;
13+
14+
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.DEFAULT_DATE_TIME_FORMATTER;
15+
16+
/**
17+
* Test matcher for ESQL datetimes that expects longs, but describes the errors as dates, for better readability.
18+
* <p>
19+
* See {@link DateNanosMatcher} for the date nanos counterpart.
20+
* </p>
21+
*/
22+
public class DateMillisMatcher extends BaseMatcher<Long> {
23+
private final long timeMillis;
24+
25+
public DateMillisMatcher(String date) {
26+
this.timeMillis = Instant.parse(date).toEpochMilli();
27+
}
28+
29+
@Override
30+
public boolean matches(Object item) {
31+
return item instanceof Long && timeMillis == (Long) item;
32+
}
33+
34+
@Override
35+
public void describeMismatch(Object item, org.hamcrest.Description description) {
36+
description.appendText("was ");
37+
if (item instanceof Long l) {
38+
description.appendValue(DEFAULT_DATE_TIME_FORMATTER.formatMillis(l));
39+
} else {
40+
description.appendValue(item);
41+
}
42+
}
43+
44+
@Override
45+
public void describeTo(org.hamcrest.Description description) {
46+
description.appendText(DEFAULT_DATE_TIME_FORMATTER.formatMillis(timeMillis));
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.expression.function.scalar.date.matchers;
9+
10+
import org.elasticsearch.common.time.DateUtils;
11+
import org.hamcrest.BaseMatcher;
12+
13+
import java.time.Instant;
14+
15+
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.DEFAULT_DATE_TIME_FORMATTER;
16+
17+
/**
18+
* Test matcher for ESQL date nanos that expects longs, but describes the errors as dates, for better readability.
19+
* <p>
20+
* See {@link DateMillisMatcher} for the datetime (millis) counterpart.
21+
* </p>
22+
*/
23+
public class DateNanosMatcher extends BaseMatcher<Long> {
24+
private final long timeNanos;
25+
26+
public DateNanosMatcher(String date) {
27+
this.timeNanos = DateUtils.toLong(Instant.parse(date));
28+
}
29+
30+
@Override
31+
public boolean matches(Object item) {
32+
return item instanceof Long && timeNanos == (Long) item;
33+
}
34+
35+
@Override
36+
public void describeMismatch(Object item, org.hamcrest.Description description) {
37+
description.appendText("was ");
38+
if (item instanceof Long l) {
39+
description.appendValue(DEFAULT_DATE_TIME_FORMATTER.formatNanos(l));
40+
} else {
41+
description.appendValue(item);
42+
}
43+
}
44+
45+
@Override
46+
public void describeTo(org.hamcrest.Description description) {
47+
description.appendText(DEFAULT_DATE_TIME_FORMATTER.formatNanos(timeNanos));
48+
}
49+
}

0 commit comments

Comments
 (0)