Skip to content

Commit de3dd41

Browse files
committed
Moved matchers to server.test
1 parent f6d6d2c commit de3dd41

File tree

6 files changed

+104
-115
lines changed

6 files changed

+104
-115
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.test;
11+
12+
import org.elasticsearch.common.time.DateFormatter;
13+
import org.elasticsearch.common.time.DateUtils;
14+
import org.hamcrest.TypeSafeMatcher;
15+
16+
import java.time.Instant;
17+
18+
public class ReadableMatchers {
19+
private static final DateFormatter dateFormatter = DateFormatter.forPattern("strict_date_optional_time");
20+
21+
/**
22+
* Test matcher for millis dates that expects longs, but describes the errors as dates, for better readability.
23+
* <p>
24+
* See {@link #matchesDateNanos} for the nanos counterpart.
25+
* </p>
26+
*/
27+
public static DateMillisMatcher matchesDateMillis(String date) {
28+
return new DateMillisMatcher(date);
29+
}
30+
31+
/**
32+
* Test matcher for nanos dates that expects longs, but describes the errors as dates, for better readability.
33+
* <p>
34+
* See {@link DateMillisMatcher} for the millis counterpart.
35+
* </p>
36+
*/
37+
public static DateNanosMatcher matchesDateNanos(String date) {
38+
return new DateNanosMatcher(date);
39+
}
40+
41+
public static class DateMillisMatcher extends TypeSafeMatcher<Long> {
42+
private final long timeMillis;
43+
44+
public DateMillisMatcher(String date) {
45+
this.timeMillis = Instant.parse(date).toEpochMilli();
46+
}
47+
48+
@Override
49+
public boolean matchesSafely(Long item) {
50+
return timeMillis == item;
51+
}
52+
53+
@Override
54+
public void describeMismatchSafely(Long item, org.hamcrest.Description description) {
55+
description.appendText("was ").appendValue(dateFormatter.formatMillis(item));
56+
}
57+
58+
@Override
59+
public void describeTo(org.hamcrest.Description description) {
60+
description.appendText(dateFormatter.formatMillis(timeMillis));
61+
}
62+
}
63+
64+
public static class DateNanosMatcher extends TypeSafeMatcher<Long> {
65+
private final long timeNanos;
66+
67+
public DateNanosMatcher(String date) {
68+
this.timeNanos = DateUtils.toLong(Instant.parse(date));
69+
}
70+
71+
@Override
72+
public boolean matchesSafely(Long item) {
73+
return timeNanos == item;
74+
}
75+
76+
@Override
77+
public void describeMismatchSafely(Long item, org.hamcrest.Description description) {
78+
description.appendText("was ").appendValue(dateFormatter.formatNanos(item));
79+
}
80+
81+
@Override
82+
public void describeTo(org.hamcrest.Description description) {
83+
description.appendText(dateFormatter.formatNanos(timeNanos));
84+
}
85+
}
86+
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/common/matchers/DateMillisMatcher.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/common/matchers/DateNanosMatcher.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import org.elasticsearch.common.time.DateUtils;
1616
import org.elasticsearch.index.mapper.DateFieldMapper;
1717
import org.elasticsearch.logging.LogManager;
18-
import org.elasticsearch.xpack.esql.common.matchers.DateMillisMatcher;
19-
import org.elasticsearch.xpack.esql.common.matchers.DateNanosMatcher;
2018
import org.elasticsearch.xpack.esql.core.expression.Expression;
2119
import org.elasticsearch.xpack.esql.core.tree.Source;
2220
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -35,6 +33,8 @@
3533
import java.util.function.LongSupplier;
3634
import java.util.function.Supplier;
3735

36+
import static org.elasticsearch.test.ReadableMatchers.matchesDateMillis;
37+
import static org.elasticsearch.test.ReadableMatchers.matchesDateNanos;
3838
import static org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier.TEST_SOURCE;
3939
import static org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTruncTests.makeTruncDurationTestCases;
4040
import static org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTruncTests.makeTruncPeriodTestCases;
@@ -154,7 +154,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
154154
),
155155
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
156156
DataType.DATETIME,
157-
new DateMillisMatcher(data.expectedDate())
157+
matchesDateMillis(data.expectedDate())
158158
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
159159
),
160160
new TestCaseSupplier(
@@ -171,7 +171,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
171171
),
172172
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
173173
DataType.DATE_NANOS,
174-
new DateNanosMatcher(data.expectedDate())
174+
matchesDateNanos(data.expectedDate())
175175
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
176176
)
177177
)
@@ -191,7 +191,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
191191
),
192192
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
193193
DataType.DATETIME,
194-
new DateMillisMatcher(data.expectedDate())
194+
matchesDateMillis(data.expectedDate())
195195
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
196196
),
197197
new TestCaseSupplier(
@@ -208,7 +208,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
208208
),
209209
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
210210
DataType.DATE_NANOS,
211-
new DateNanosMatcher(data.expectedDate())
211+
matchesDateNanos(data.expectedDate())
212212
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
213213
)
214214
)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import org.elasticsearch.common.time.DateUtils;
1515
import org.elasticsearch.index.mapper.DateFieldMapper;
1616
import org.elasticsearch.logging.LogManager;
17-
import org.elasticsearch.xpack.esql.common.matchers.DateMillisMatcher;
18-
import org.elasticsearch.xpack.esql.common.matchers.DateNanosMatcher;
1917
import org.elasticsearch.xpack.esql.core.expression.Expression;
2018
import org.elasticsearch.xpack.esql.core.tree.Source;
2119
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -34,6 +32,8 @@
3432
import java.util.function.LongSupplier;
3533
import java.util.function.Supplier;
3634

35+
import static org.elasticsearch.test.ReadableMatchers.matchesDateMillis;
36+
import static org.elasticsearch.test.ReadableMatchers.matchesDateNanos;
3737
import static org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier.TEST_SOURCE;
3838
import static org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTruncTests.makeTruncDurationTestCases;
3939
import static org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTruncTests.makeTruncPeriodTestCases;
@@ -139,7 +139,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
139139
),
140140
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
141141
DataType.DATETIME,
142-
new DateMillisMatcher(data.expectedDate())
142+
matchesDateMillis(data.expectedDate())
143143
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
144144
),
145145
new TestCaseSupplier(
@@ -156,7 +156,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
156156
),
157157
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
158158
DataType.DATE_NANOS,
159-
new DateNanosMatcher(data.expectedDate())
159+
matchesDateNanos(data.expectedDate())
160160
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
161161
)
162162
)
@@ -176,7 +176,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
176176
),
177177
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
178178
DataType.DATETIME,
179-
new DateMillisMatcher(data.expectedDate())
179+
matchesDateMillis(data.expectedDate())
180180
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
181181
),
182182
new TestCaseSupplier(
@@ -193,7 +193,7 @@ private static void dateTruncCases(List<TestCaseSupplier> suppliers) {
193193
),
194194
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
195195
DataType.DATE_NANOS,
196-
new DateNanosMatcher(data.expectedDate())
196+
matchesDateNanos(data.expectedDate())
197197
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
198198
)
199199
)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
import org.elasticsearch.common.time.DateUtils;
1414
import org.elasticsearch.core.Nullable;
15-
import org.elasticsearch.xpack.esql.common.matchers.DateMillisMatcher;
16-
import org.elasticsearch.xpack.esql.common.matchers.DateNanosMatcher;
1715
import org.elasticsearch.xpack.esql.core.expression.Expression;
1816
import org.elasticsearch.xpack.esql.core.tree.Source;
1917
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -30,6 +28,8 @@
3028
import java.util.List;
3129
import java.util.function.Supplier;
3230

31+
import static org.elasticsearch.test.ReadableMatchers.matchesDateMillis;
32+
import static org.elasticsearch.test.ReadableMatchers.matchesDateNanos;
3333
import static org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier.TEST_SOURCE;
3434
import static org.hamcrest.Matchers.equalTo;
3535

@@ -201,7 +201,7 @@ private static List<TestCaseSupplier> ofDatePeriod(PeriodTestCaseData data) {
201201
),
202202
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
203203
DataType.DATETIME,
204-
new DateMillisMatcher(data.expectedDate())
204+
matchesDateMillis(data.expectedDate())
205205
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
206206
),
207207
new TestCaseSupplier(
@@ -214,7 +214,7 @@ private static List<TestCaseSupplier> ofDatePeriod(PeriodTestCaseData data) {
214214
),
215215
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
216216
DataType.DATE_NANOS,
217-
new DateNanosMatcher(data.expectedDate())
217+
matchesDateNanos(data.expectedDate())
218218
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
219219
)
220220
);
@@ -232,7 +232,7 @@ private static List<TestCaseSupplier> ofDuration(DurationTestCaseData data) {
232232
),
233233
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
234234
DataType.DATETIME,
235-
new DateMillisMatcher(data.expectedDate())
235+
matchesDateMillis(data.expectedDate())
236236
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
237237
),
238238
new TestCaseSupplier(
@@ -245,7 +245,7 @@ private static List<TestCaseSupplier> ofDuration(DurationTestCaseData data) {
245245
),
246246
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
247247
DataType.DATE_NANOS,
248-
new DateNanosMatcher(data.expectedDate())
248+
matchesDateNanos(data.expectedDate())
249249
).withConfiguration(TEST_SOURCE, configurationForTimezone(data.zoneId()))
250250
)
251251
);

0 commit comments

Comments
 (0)