Skip to content

Commit

Permalink
Add tests for fractional epoch parsing (#38162)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spinscale authored Feb 1, 2019
1 parent 2229e72 commit 979e557
Showing 1 changed file with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void testEpochMillisParser() {
assertThat(instant.getEpochSecond(), is(0L));
assertThat(instant.getNano(), is(0));
}
{
Instant instant = Instant.from(formatter.parse("123.123456"));
assertThat(instant.getEpochSecond(), is(0L));
assertThat(instant.getNano(), is(123123456));
}
}

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

IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("1234.1234567890"));
TemporalAccessor accessor = formatter.parse("1234.1");
Instant instant = DateFormatters.from(accessor).toInstant();
assertThat(instant.getEpochSecond(), is(1234L));
assertThat(DateFormatters.from(accessor).toInstant().getNano(), is(100_000_000));

accessor = formatter.parse("1234");
instant = DateFormatters.from(accessor).toInstant();
assertThat(instant.getEpochSecond(), is(1234L));
assertThat(instant.getNano(), is(0));

IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("abc"));
assertThat(e.getMessage(), is("failed to parse date field [abc] with format [epoch_second]"));

e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("1234.abc"));
assertThat(e.getMessage(), is("failed to parse date field [1234.abc] with format [epoch_second]"));

e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("1234.1234567890"));
assertThat(e.getMessage(), is("failed to parse date field [1234.1234567890] with format [epoch_second]"));
e = expectThrows(IllegalArgumentException .class, () -> formatter.parse("1234.123456789013221"));
assertThat(e.getMessage(), containsString("[1234.123456789013221]"));
e = expectThrows(IllegalArgumentException .class, () -> formatter.parse("abc"));
assertThat(e.getMessage(), containsString("[abc]"));
e = expectThrows(IllegalArgumentException .class, () -> formatter.parse("1234.abc"));
assertThat(e.getMessage(), containsString("[1234.abc]"));
}

public void testEpochMilliParsersWithDifferentFormatters() {
Expand Down

0 comments on commit 979e557

Please sign in to comment.