[CALCITE-2299] Add tests for TimeUnitRange (Sergey Nuyanzin) Close apache/calcite-avatica#60
Signed-off-by: Kevin Risden <kris...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/calcite-avatica/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite-avatica/commit/b3c3ecfe Tree: http://git-wip-us.apache.org/repos/asf/calcite-avatica/tree/b3c3ecfe Diff: http://git-wip-us.apache.org/repos/asf/calcite-avatica/diff/b3c3ecfe Branch: refs/heads/branch-avatica-1.12 Commit: b3c3ecfe11ae80a32c4563f765b15f9da60a6ed8 Parents: 6a19451 Author: snuyanzin <snuyan...@gmail.com> Authored: Thu Jun 14 16:48:47 2018 +0300 Committer: Kevin Risden <kris...@apache.org> Committed: Fri Jun 15 14:55:37 2018 -0500 ---------------------------------------------------------------------- .../calcite/avatica/util/DateTimeUtilsTest.java | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/b3c3ecfe/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java b/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java index 9c9bc34..7dbfaba 100644 --- a/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java +++ b/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java @@ -18,6 +18,7 @@ package org.apache.calcite.avatica.util; import org.junit.Test; +import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; @@ -48,6 +49,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -89,6 +91,66 @@ public class DateTimeUtilsTest { assertThat(floorMod(-1, 3), is(2L)); } + @Test public void testTimeUnitRange() { + assertSame(TimeUnitRange.of(TimeUnit.YEAR, null), TimeUnitRange.YEAR); + assertSame(TimeUnitRange.of(TimeUnit.YEAR, TimeUnit.MONTH), TimeUnitRange.YEAR_TO_MONTH); + assertSame(TimeUnitRange.of(TimeUnit.MONTH, null), TimeUnitRange.MONTH); + assertSame(TimeUnitRange.of(TimeUnit.DAY, null), TimeUnitRange.DAY); + assertSame(TimeUnitRange.of(TimeUnit.DAY, TimeUnit.HOUR), TimeUnitRange.DAY_TO_HOUR); + assertSame(TimeUnitRange.of(TimeUnit.DAY, TimeUnit.MINUTE), TimeUnitRange.DAY_TO_MINUTE); + assertSame(TimeUnitRange.of(TimeUnit.DAY, TimeUnit.SECOND), TimeUnitRange.DAY_TO_SECOND); + assertSame(TimeUnitRange.of(TimeUnit.HOUR, null), TimeUnitRange.HOUR); + assertSame(TimeUnitRange.of(TimeUnit.HOUR, TimeUnit.MINUTE), TimeUnitRange.HOUR_TO_MINUTE); + assertSame(TimeUnitRange.of(TimeUnit.HOUR, TimeUnit.SECOND), TimeUnitRange.HOUR_TO_SECOND); + assertSame(TimeUnitRange.of(TimeUnit.MINUTE, null), TimeUnitRange.MINUTE); + assertSame(TimeUnitRange.of(TimeUnit.MINUTE, TimeUnit.SECOND), TimeUnitRange.MINUTE_TO_SECOND); + assertSame(TimeUnitRange.of(TimeUnit.SECOND, null), TimeUnitRange.SECOND); + assertSame(TimeUnitRange.of(TimeUnit.ISOYEAR, null), TimeUnitRange.ISOYEAR); + assertSame(TimeUnitRange.of(TimeUnit.QUARTER, null), TimeUnitRange.QUARTER); + assertSame(TimeUnitRange.of(TimeUnit.WEEK, null), TimeUnitRange.WEEK); + assertSame(TimeUnitRange.of(TimeUnit.MILLISECOND, null), TimeUnitRange.MILLISECOND); + assertSame(TimeUnitRange.of(TimeUnit.MICROSECOND, null), TimeUnitRange.MICROSECOND); + assertSame(TimeUnitRange.of(TimeUnit.NANOSECOND, null), TimeUnitRange.NANOSECOND); + assertSame(TimeUnitRange.of(TimeUnit.DOW, null), TimeUnitRange.DOW); + assertSame(TimeUnitRange.of(TimeUnit.ISODOW, null), TimeUnitRange.ISODOW); + assertSame(TimeUnitRange.of(TimeUnit.DOY, null), TimeUnitRange.DOY); + assertSame(TimeUnitRange.of(TimeUnit.EPOCH, null), TimeUnitRange.EPOCH); + assertSame(TimeUnitRange.of(TimeUnit.DECADE, null), TimeUnitRange.DECADE); + assertSame(TimeUnitRange.of(TimeUnit.CENTURY, null), TimeUnitRange.CENTURY); + assertSame(TimeUnitRange.of(TimeUnit.MILLENNIUM, null), TimeUnitRange.MILLENNIUM); + } + + @Test public void testTimeUnitMultipliers() { + assertEquals(TimeUnit.NANOSECOND.multiplier, + TimeUnit.MICROSECOND.multiplier.divide(BigDecimal.valueOf(1000))); + assertEquals(TimeUnit.MICROSECOND.multiplier, + TimeUnit.MILLISECOND.multiplier.divide(BigDecimal.valueOf(1000))); + assertEquals(TimeUnit.MILLISECOND.multiplier, + TimeUnit.SECOND.multiplier.divide(BigDecimal.valueOf(1000))); + assertEquals(BigDecimal.valueOf(60), + TimeUnit.HOUR.multiplier.divide(TimeUnit.MINUTE.multiplier)); + assertEquals(BigDecimal.valueOf(60), + TimeUnit.MINUTE.multiplier.divide(TimeUnit.SECOND.multiplier)); + assertEquals(BigDecimal.valueOf(24), + TimeUnit.DAY.multiplier.divide(TimeUnit.HOUR.multiplier)); + assertEquals(BigDecimal.valueOf(7), + TimeUnit.WEEK.multiplier.divide(TimeUnit.DAY.multiplier)); + assertEquals(BigDecimal.valueOf(4), + TimeUnit.YEAR.multiplier.divide(TimeUnit.QUARTER.multiplier)); + assertEquals(BigDecimal.valueOf(12), + TimeUnit.YEAR.multiplier.divide(TimeUnit.MONTH.multiplier)); + assertEquals(BigDecimal.valueOf(12), + TimeUnit.ISOYEAR.multiplier.divide(TimeUnit.MONTH.multiplier)); + assertEquals(BigDecimal.valueOf(3), + TimeUnit.QUARTER.multiplier.divide(TimeUnit.MONTH.multiplier)); + assertEquals(BigDecimal.valueOf(10), + TimeUnit.DECADE.multiplier.divide(TimeUnit.YEAR.multiplier)); + assertEquals(BigDecimal.valueOf(100), + TimeUnit.CENTURY.multiplier.divide(TimeUnit.YEAR.multiplier)); + assertEquals(BigDecimal.valueOf(1000), + TimeUnit.MILLENNIUM.multiplier.divide(TimeUnit.YEAR.multiplier)); + } + @Test public void testUnixDateToString() { // Verify these using the "date" command. E.g. // $ date -u --date="@$(expr 10957 \* 86400)"