Skip to content

Commit df47187

Browse files
moschejfreden
authored andcommitted
Fix test to use date-time strings at corresponding precision for from and to. (elastic#127899)
Fixes elastic#86284
1 parent 66d800b commit df47187

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@
4141
import java.time.Instant;
4242
import java.time.ZoneOffset;
4343
import java.time.ZonedDateTime;
44+
import java.time.format.DateTimeFormatter;
45+
import java.time.temporal.ChronoUnit;
4446
import java.util.Collections;
4547
import java.util.List;
48+
import java.util.Locale;
4649
import java.util.Map;
4750

4851
import static org.hamcrest.Matchers.containsString;
@@ -86,10 +89,21 @@ public void testRangeQuery() throws Exception {
8689
);
8790
}
8891

92+
private static String dateTimeString(long epochMillis, int increment, ChronoUnit precision) {
93+
var dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneOffset.UTC)
94+
.plus(increment, precision)
95+
.truncatedTo(precision);
96+
return switch (precision) {
97+
case MILLIS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT).format(dateTime);
98+
case SECONDS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT).format(dateTime);
99+
case MINUTES -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'", Locale.ROOT).format(dateTime);
100+
default -> dateTime.toString();
101+
};
102+
}
103+
89104
/**
90105
* test the queries are correct if from/to are adjacent and the range is exclusive of those values
91106
*/
92-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86284")
93107
public void testRangeQueryIntersectsAdjacentValues() throws Exception {
94108
SearchExecutionContext context = createContext();
95109
ShapeRelation relation = randomFrom(ShapeRelation.values());
@@ -105,8 +119,9 @@ public void testRangeQueryIntersectsAdjacentValues() throws Exception {
105119
}
106120
case DATE -> {
107121
long fromValue = randomInt();
108-
from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue), ZoneOffset.UTC);
109-
to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue + 1), ZoneOffset.UTC);
122+
var precision = randomFrom(ChronoUnit.MILLIS, ChronoUnit.SECONDS, ChronoUnit.MINUTES);
123+
from = dateTimeString(fromValue, 0, precision);
124+
to = dateTimeString(fromValue, 1, precision);
110125
}
111126
case INTEGER -> {
112127
int fromValue = randomInt();
@@ -143,7 +158,6 @@ public void testRangeQueryIntersectsAdjacentValues() throws Exception {
143158
/**
144159
* check that we catch cases where the user specifies larger "from" than "to" value, not counting the include upper/lower settings
145160
*/
146-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86284")
147161
public void testFromLargerToErrors() throws Exception {
148162
SearchExecutionContext context = createContext();
149163
RangeFieldType ft = createDefaultFieldType();
@@ -159,8 +173,9 @@ public void testFromLargerToErrors() throws Exception {
159173
}
160174
case DATE: {
161175
long fromValue = randomInt();
162-
from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue), ZoneOffset.UTC);
163-
to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue - 1), ZoneOffset.UTC);
176+
var precision = randomFrom(ChronoUnit.MILLIS, ChronoUnit.SECONDS, ChronoUnit.MINUTES);
177+
from = dateTimeString(fromValue, 0, precision);
178+
to = dateTimeString(fromValue, -1, precision);
164179
break;
165180
}
166181
case INTEGER: {

0 commit comments

Comments
 (0)