Updated tests for issue 1998

This commit is contained in:
Krzysztof 'impune-pl' Prorok 2019-06-04 17:05:44 +02:00
parent ae2f44f9d5
commit ceb367b906

View File

@ -28,6 +28,7 @@ package com.google.refine.tests.expr.functions.strings;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.ZoneOffset;
import java.util.Properties; import java.util.Properties;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -51,6 +52,12 @@ public class DiffTests extends RefineTest {
private OffsetDateTime odt1; private OffsetDateTime odt1;
private OffsetDateTime odt2; private OffsetDateTime odt2;
private OffsetDateTime odt3;
private OffsetDateTime odt4;
private OffsetDateTime odt5;
private OffsetDateTime odt6;
private OffsetDateTime odt7;
private OffsetDateTime odt8;
@Override @Override
@BeforeTest @BeforeTest
@ -58,6 +65,12 @@ public class DiffTests extends RefineTest {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
odt1 = OffsetDateTime.parse("2011-09-01T10:15:30.123456+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); odt1 = OffsetDateTime.parse("2011-09-01T10:15:30.123456+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
odt2 = OffsetDateTime.parse("2011-12-02T10:16:30.123467+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); odt2 = OffsetDateTime.parse("2011-12-02T10:16:30.123467+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
odt3 = OffsetDateTime.of(2010, 1, 10, 12, 0, 0, 0, ZoneOffset.UTC);
odt4 = OffsetDateTime.of(2999, 1, 10, 12, 0, 0, 0, ZoneOffset.UTC);
odt5 = OffsetDateTime.of(2285, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC);
odt6 = OffsetDateTime.of(1700, 1, 10, 12, 0, 0, 0, ZoneOffset.UTC);
odt7 = OffsetDateTime.of(1923, 4, 19, 12, 0, 0, 0, ZoneOffset.UTC);
odt8 = OffsetDateTime.of(1923, 4, 21, 12, 0, 0, 0, ZoneOffset.UTC);
} }
@BeforeMethod @BeforeMethod
@ -100,15 +113,43 @@ public class DiffTests extends RefineTest {
@Test @Test
public void testDiffOffsetDateTime() { public void testDiffOffsetDateTime() {
// OffsetDateTime diff: // OffsetDateTime diff:
Assert.assertEquals(invoke("diff",odt2,odt3,"years"),Long.valueOf(1));
Assert.assertEquals(invoke("diff",odt2,odt1,"days"),Long.valueOf(92)); Assert.assertEquals(invoke("diff",odt2,odt1,"days"),Long.valueOf(92));
Assert.assertEquals(invoke("diff",odt2,odt1,"weeks"),Long.valueOf(13)); Assert.assertEquals(invoke("diff",odt2,odt1,"weeks"),Long.valueOf(13));
Assert.assertEquals(invoke("diff",odt2,odt1,"months"),Long.valueOf(3)); Assert.assertEquals(invoke("diff",odt2,odt1,"months"),Long.valueOf(3));
Assert.assertEquals(invoke("diff",odt2,odt1,"hours"),Long.valueOf(2208)); Assert.assertEquals(invoke("diff",odt2,odt1,"hours"),Long.valueOf(2208));
Assert.assertEquals(invoke("diff",odt2,odt1,"minutes"),Long.valueOf(132481));
Assert.assertEquals(invoke("diff",odt2,odt1,"seconds"),Long.valueOf(7948860)); Assert.assertEquals(invoke("diff",odt2,odt1,"seconds"),Long.valueOf(7948860));
Assert.assertEquals(invoke("diff",odt2,odt1,"milliseconds"),Long.valueOf(7948860000011l)); //Changed due to an error in previous version- it was checking for microseconds (1,000,000th of second) instead of milliseconds (1000th of second)
Assert.assertEquals(invoke("diff",odt2,odt1,"milliseconds"),Long.valueOf(7948860000l));
Assert.assertEquals(invoke("diff",odt2,odt1,"nanos"),Long.valueOf(7948860000011000l)); Assert.assertEquals(invoke("diff",odt2,odt1,"nanos"),Long.valueOf(7948860000011000l));
} }
@Test
public void testDiffOffsetDateTimeEvalErrors() {
//Check whether EvalError is returned if time in nanoseconds between two dates is larger than long max in java when nanoseconds are desired unit.
Assert.assertTrue(invoke("diff", odt3, odt4, "nanos") instanceof EvalError);
Assert.assertTrue(invoke("diff", odt4, odt5, "nanos") instanceof EvalError);
//At the same time if different time unit is requested for the same period, it should not result in EvalError.
Assert.assertEquals(invoke("diff", odt3, odt4, "milliseconds"), Long.valueOf(-31209840000000l));
Assert.assertEquals(invoke("diff", odt4, odt5, "milliseconds"), Long.valueOf(22532428800000l));
Assert.assertEquals(invoke("diff", odt3, odt4, "days"), Long.valueOf(-361225));
Assert.assertEquals(invoke("diff", odt4, odt5, "days"), Long.valueOf(260792));
//Also, ensure that periods longer (in nanoseconds) than long max preserve continuity when expressed in different time unit, like days.
//Example: 1923-04-19 to 1700-01-01 is just below long max when expressed in nanoseconds
Assert.assertEquals(invoke("diff", odt6, odt7, "days"), Long.valueOf(-81547));
// and 1923-04-21 to 1700-01-01 is slightly above long max when expressed in nanoseconds
Assert.assertEquals(invoke("diff", odt6, odt8, "days"), Long.valueOf(-81549));
}
@Test
public void testDiffOffsetDateTimeEvalErrorsForIncorrectTimeUnit() {
//In case if incorrect time unit is passed to function, EvalError should be returned
Assert.assertTrue(invoke("diff", odt4, odt5, "milis") instanceof EvalError);
Assert.assertTrue(invoke("diff", odt4, odt5, "millis") instanceof EvalError);
Assert.assertTrue(invoke("diff", odt4, odt5, "yars") instanceof EvalError);
}
@Test @Test
public void serializeDiff() { public void serializeDiff() {
String json = "{\"description\":\"For strings, returns the portion where they differ. For dates, it returns the difference in given time units\",\"params\":\"o1, o2, time unit (optional)\",\"returns\":\"string for strings, number for dates\"}"; String json = "{\"description\":\"For strings, returns the portion where they differ. For dates, it returns the difference in given time units\",\"params\":\"o1, o2, time unit (optional)\",\"returns\":\"string for strings, number for dates\"}";