Skip to content

Fix test to use date-time strings at corresponding precision for from and to. #127899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -86,10 +89,21 @@ public void testRangeQuery() throws Exception {
);
}

private static String dateTimeString(long epochMillis, int increment, ChronoUnit precision) {
var dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneOffset.UTC)
.plus(increment, precision)
.truncatedTo(precision);
return switch (precision) {
case MILLIS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT).format(dateTime);
case SECONDS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT).format(dateTime);
case MINUTES -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'", Locale.ROOT).format(dateTime);
default -> dateTime.toString();
};
}

/**
* test the queries are correct if from/to are adjacent and the range is exclusive of those values
*/
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86284")
public void testRangeQueryIntersectsAdjacentValues() throws Exception {
SearchExecutionContext context = createContext();
ShapeRelation relation = randomFrom(ShapeRelation.values());
Expand All @@ -105,8 +119,9 @@ public void testRangeQueryIntersectsAdjacentValues() throws Exception {
}
case DATE -> {
long fromValue = randomInt();
from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue), ZoneOffset.UTC);
to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue + 1), ZoneOffset.UTC);
var precision = randomFrom(ChronoUnit.MILLIS, ChronoUnit.SECONDS, ChronoUnit.MINUTES);
from = dateTimeString(fromValue, 0, precision);
to = dateTimeString(fromValue, 1, precision);
}
case INTEGER -> {
int fromValue = randomInt();
Expand Down Expand Up @@ -143,7 +158,6 @@ public void testRangeQueryIntersectsAdjacentValues() throws Exception {
/**
* check that we catch cases where the user specifies larger "from" than "to" value, not counting the include upper/lower settings
*/
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86284")
public void testFromLargerToErrors() throws Exception {
SearchExecutionContext context = createContext();
RangeFieldType ft = createDefaultFieldType();
Expand All @@ -159,8 +173,9 @@ public void testFromLargerToErrors() throws Exception {
}
case DATE: {
long fromValue = randomInt();
from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue), ZoneOffset.UTC);
to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue - 1), ZoneOffset.UTC);
var precision = randomFrom(ChronoUnit.MILLIS, ChronoUnit.SECONDS, ChronoUnit.MINUTES);
from = dateTimeString(fromValue, 0, precision);
to = dateTimeString(fromValue, -1, precision);
break;
}
case INTEGER: {
Expand Down