Skip to content

Commit 979e557

Browse files
authored
Add tests for fractional epoch parsing (#38162)
Fractional epoch parsing is supported, the tests we used were edge cases that did not make sense. This adds tests to properly check for this.
1 parent 2229e72 commit 979e557

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public void testEpochMillisParser() {
5454
assertThat(instant.getEpochSecond(), is(0L));
5555
assertThat(instant.getNano(), is(0));
5656
}
57+
{
58+
Instant instant = Instant.from(formatter.parse("123.123456"));
59+
assertThat(instant.getEpochSecond(), is(0L));
60+
assertThat(instant.getNano(), is(123123456));
61+
}
5762
}
5863

5964
public void testInvalidEpochMilliParser() {
@@ -68,17 +73,27 @@ public void testInvalidEpochMilliParser() {
6873
// this is not in the duelling tests, because the epoch second parser in joda time drops the milliseconds after the comma
6974
// but is able to parse the rest
7075
// as this feature is supported it also makes sense to make it exact
71-
public void testEpochSecondParser() {
76+
public void testEpochSecondParserWithFraction() {
7277
DateFormatter formatter = DateFormatters.forPattern("epoch_second");
7378

74-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("1234.1234567890"));
79+
TemporalAccessor accessor = formatter.parse("1234.1");
80+
Instant instant = DateFormatters.from(accessor).toInstant();
81+
assertThat(instant.getEpochSecond(), is(1234L));
82+
assertThat(DateFormatters.from(accessor).toInstant().getNano(), is(100_000_000));
83+
84+
accessor = formatter.parse("1234");
85+
instant = DateFormatters.from(accessor).toInstant();
86+
assertThat(instant.getEpochSecond(), is(1234L));
87+
assertThat(instant.getNano(), is(0));
88+
89+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("abc"));
90+
assertThat(e.getMessage(), is("failed to parse date field [abc] with format [epoch_second]"));
91+
92+
e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("1234.abc"));
93+
assertThat(e.getMessage(), is("failed to parse date field [1234.abc] with format [epoch_second]"));
94+
95+
e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("1234.1234567890"));
7596
assertThat(e.getMessage(), is("failed to parse date field [1234.1234567890] with format [epoch_second]"));
76-
e = expectThrows(IllegalArgumentException .class, () -> formatter.parse("1234.123456789013221"));
77-
assertThat(e.getMessage(), containsString("[1234.123456789013221]"));
78-
e = expectThrows(IllegalArgumentException .class, () -> formatter.parse("abc"));
79-
assertThat(e.getMessage(), containsString("[abc]"));
80-
e = expectThrows(IllegalArgumentException .class, () -> formatter.parse("1234.abc"));
81-
assertThat(e.getMessage(), containsString("[1234.abc]"));
8297
}
8398

8499
public void testEpochMilliParsersWithDifferentFormatters() {

0 commit comments

Comments
 (0)