Search in sources :

Example 6 with TestRow

use of com.cadenzauk.siesta.model.TestRow in project siesta by cadenzauk.

the class DatabaseIntegrationTest method dateAdd.

@Test
void dateAdd() {
    Database database = testDatabase(dataSource, dialect);
    UUID guid = UUID.randomUUID();
    LocalDate localDate = RandomValues.randomLocalDate();
    database.insert(TestRow.newBuilder().guid(guid).localDateReq(localDate).build());
    LocalDate result = database.from(TestRow.class).select(DateFunctions.addDays(TestRow::localDateReq, 7)).where(TestRow::guid).isEqualTo(guid).single();
    assertThat(result, is(localDate.plusDays(7)));
}
Also used : TestRow(com.cadenzauk.siesta.model.TestRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) UUID(java.util.UUID) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with TestRow

use of com.cadenzauk.siesta.model.TestRow in project siesta by cadenzauk.

the class DateFunctionsTest method parametersForFunctionTest.

@SuppressWarnings("unused")
private static Stream<Arguments> parametersForFunctionTest() {
    LocalDate randomLocalDate = RandomValues.randomLocalDate();
    LocalDateTime randomLocalDateTime = RandomValues.randomLocalDateTime();
    ZonedDateTime randomZonedDateTime = RandomValues.randomZonedDateTime(ZoneId.systemDefault());
    return Stream.of(testCase(s -> DateFunctions.currentDate(), "current_date", toArray()), testCase(s -> DateFunctions.currentTimestampLocal(), "current_timestamp", toArray()), testCase(s -> DateFunctions.currentTimestamp(), "current_timestamp", toArray()), testCase(s -> year(randomLocalDate), "year(cast(? as date))", toArray(Date.valueOf(randomLocalDate))), testCase(s -> year(randomLocalDateTime), "year(cast(? as timestamp))", toArray(Timestamp.valueOf(randomLocalDateTime))), testCase(s -> year(randomZonedDateTime), "year(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> year(TypedExpression.value(randomZonedDateTime)), "year(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> year(TestRow::localDateReq), "year(s.LOCAL_DATE_REQ)", toArray()), testCase(s -> year(TestRow::localDateOpt), "year(s.LOCAL_DATE_OPT)", toArray()), testCase(s -> year("s", TestRow::localDateReq), "year(s.LOCAL_DATE_REQ)", toArray()), testCase(s -> year("s", TestRow::localDateOpt), "year(s.LOCAL_DATE_OPT)", toArray()), testCase(s -> year(s, TestRow::localDateReq), "year(s.LOCAL_DATE_REQ)", toArray()), testCase(s -> year(s, TestRow::localDateOpt), "year(s.LOCAL_DATE_OPT)", toArray()), testCase(s -> month(randomLocalDate), "month(cast(? as date))", toArray(Date.valueOf(randomLocalDate))), testCase(s -> month(randomLocalDateTime), "month(cast(? as timestamp))", toArray(Timestamp.valueOf(randomLocalDateTime))), testCase(s -> month(randomZonedDateTime), "month(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> month(TypedExpression.value(randomZonedDateTime)), "month(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> month(TestRow::localDateTimeReq), "month(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> month(TestRow::localDateTimeOpt), "month(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> month("s", TestRow::localDateTimeReq), "month(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> month("s", TestRow::localDateTimeOpt), "month(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> month(s, TestRow::localDateTimeReq), "month(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> month(s, TestRow::localDateTimeOpt), "month(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> day(randomLocalDate), "day(cast(? as date))", toArray(Date.valueOf(randomLocalDate))), testCase(s -> day(randomLocalDateTime), "day(cast(? as timestamp))", toArray(Timestamp.valueOf(randomLocalDateTime))), testCase(s -> day(randomZonedDateTime), "day(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> day(TypedExpression.value(randomZonedDateTime)), "day(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> day(TestRow::utcDateTimeReq), "day(s.UTC_DATE_TIME_REQ)", toArray()), testCase(s -> day(TestRow::utcDateTimeOpt), "day(s.UTC_DATE_TIME_OPT)", toArray()), testCase(s -> day("s", TestRow::utcDateTimeReq), "day(s.UTC_DATE_TIME_REQ)", toArray()), testCase(s -> day("s", TestRow::utcDateTimeOpt), "day(s.UTC_DATE_TIME_OPT)", toArray()), testCase(s -> day(s, TestRow::utcDateTimeReq), "day(s.UTC_DATE_TIME_REQ)", toArray()), testCase(s -> day(s, TestRow::utcDateTimeOpt), "day(s.UTC_DATE_TIME_OPT)", toArray()), testCase(s -> hour(randomLocalDateTime), "hour(cast(? as timestamp))", toArray(Timestamp.valueOf(randomLocalDateTime))), testCase(s -> hour(randomZonedDateTime), "hour(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> hour(TypedExpression.value(randomZonedDateTime)), "hour(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> hour(TestRow::localDateTimeReq), "hour(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> hour(TestRow::localDateTimeOpt), "hour(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> hour("s", TestRow::localDateTimeReq), "hour(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> hour("s", TestRow::localDateTimeOpt), "hour(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> hour(s, TestRow::localDateTimeReq), "hour(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> hour(s, TestRow::localDateTimeOpt), "hour(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> minute(randomLocalDateTime), "minute(cast(? as timestamp))", toArray(Timestamp.valueOf(randomLocalDateTime))), testCase(s -> minute(randomZonedDateTime), "minute(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> minute(TypedExpression.value(randomZonedDateTime)), "minute(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> minute(TestRow::utcDateTimeReq), "minute(s.UTC_DATE_TIME_REQ)", toArray()), testCase(s -> minute(TestRow::utcDateTimeOpt), "minute(s.UTC_DATE_TIME_OPT)", toArray()), testCase(s -> minute("s", TestRow::utcDateTimeReq), "minute(s.UTC_DATE_TIME_REQ)", toArray()), testCase(s -> minute("s", TestRow::utcDateTimeOpt), "minute(s.UTC_DATE_TIME_OPT)", toArray()), testCase(s -> minute(s, TestRow::utcDateTimeReq), "minute(s.UTC_DATE_TIME_REQ)", toArray()), testCase(s -> minute(s, TestRow::utcDateTimeOpt), "minute(s.UTC_DATE_TIME_OPT)", toArray()), testCase(s -> second(randomLocalDateTime), "second(cast(? as timestamp))", toArray(Timestamp.valueOf(randomLocalDateTime))), testCase(s -> second(randomZonedDateTime), "second(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> second(TypedExpression.value(randomZonedDateTime)), "second(cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> second(TestRow::localDateTimeReq), "second(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> second(TestRow::localDateTimeOpt), "second(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> second("s", TestRow::localDateTimeReq), "second(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> second("s", TestRow::localDateTimeOpt), "second(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> second(s, TestRow::localDateTimeReq), "second(s.LOCAL_DATE_TIME_REQ)", toArray()), testCase(s -> second(s, TestRow::localDateTimeOpt), "second(s.LOCAL_DATE_TIME_OPT)", toArray()), testCase(s -> addDays(TypedExpression.value(randomZonedDateTime), 1), "dateadd(day, ?, cast(? as timestamp))", toArray(1, Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> addDays(TypedExpression.value(randomZonedDateTime), literal(4)), "dateadd(day, 4, cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> addDays(TestRow::localDateTimeReq, 1), "dateadd(day, ?, s.LOCAL_DATE_TIME_REQ)", toArray(1)), testCase(s -> addDays(TestRow::localDateTimeOpt, 1), "dateadd(day, ?, s.LOCAL_DATE_TIME_OPT)", toArray(1)), testCase(s -> addDays("s", TestRow::localDateTimeReq, 2), "dateadd(day, ?, s.LOCAL_DATE_TIME_REQ)", toArray(2)), testCase(s -> addDays("s", TestRow::localDateTimeOpt, 2), "dateadd(day, ?, s.LOCAL_DATE_TIME_OPT)", toArray(2)), testCase(s -> addDays(s, TestRow::localDateTimeReq, 3), "dateadd(day, ?, s.LOCAL_DATE_TIME_REQ)", toArray(3)), testCase(s -> addDays(s, TestRow::localDateTimeOpt, 3), "dateadd(day, ?, s.LOCAL_DATE_TIME_OPT)", toArray(3)), testCase(s -> secondDiff(TypedExpression.value(randomZonedDateTime), literal(LocalDate.of(2013, 12, 25))), "datediff(second, DATE '2013-12-25', cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> secondDiff(TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(second, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> secondDiff(TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(second, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> secondDiff("s", TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(second, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> secondDiff("s", TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(second, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> secondDiff(s, TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(second, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> secondDiff(s, TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(second, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> minuteDiff(TypedExpression.value(randomZonedDateTime), literal(LocalDate.of(2013, 12, 25))), "datediff(minute, DATE '2013-12-25', cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> minuteDiff(TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(minute, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> minuteDiff(TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(minute, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> minuteDiff("s", TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(minute, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> minuteDiff("s", TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(minute, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> minuteDiff(s, TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(minute, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> minuteDiff(s, TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(minute, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> hourDiff(TypedExpression.value(randomZonedDateTime), literal(LocalDate.of(2013, 12, 25))), "datediff(hour, DATE '2013-12-25', cast(? as timestamp))", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> hourDiff(TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(hour, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> hourDiff(TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(hour, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> hourDiff("s", TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(hour, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> hourDiff("s", TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(hour, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> hourDiff(s, TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "datediff(hour, cast(? as date), s.LOCAL_DATE_TIME_REQ)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> hourDiff(s, TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "datediff(hour, cast(? as date), s.LOCAL_DATE_TIME_OPT)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> dayDiff(TypedExpression.value(randomZonedDateTime), literal(LocalDate.of(2013, 12, 25))), "cast(? as timestamp) - DATE '2013-12-25'", toArray(Timestamp.valueOf(randomZonedDateTime.toLocalDateTime()))), testCase(s -> dayDiff(TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "s.LOCAL_DATE_TIME_REQ - cast(? as date)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> dayDiff(TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "s.LOCAL_DATE_TIME_OPT - cast(? as date)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> dayDiff("s", TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "s.LOCAL_DATE_TIME_REQ - cast(? as date)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> dayDiff("s", TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "s.LOCAL_DATE_TIME_OPT - cast(? as date)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> dayDiff(s, TestRow::localDateTimeReq, LocalDate.of(2013, 12, 25)), "s.LOCAL_DATE_TIME_REQ - cast(? as date)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))), testCase(s -> dayDiff(s, TestRow::localDateTimeOpt, LocalDate.of(2013, 12, 25)), "s.LOCAL_DATE_TIME_OPT - cast(? as date)", toArray(Date.valueOf(LocalDate.of(2013, 12, 25)))));
}
Also used : LocalDateTime(java.time.LocalDateTime) TestRow(com.cadenzauk.siesta.model.TestRow) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) TypedExpression.literal(com.cadenzauk.siesta.grammar.expression.TypedExpression.literal) DateFunctions.month(com.cadenzauk.siesta.grammar.expression.DateFunctions.month) DateFunctions.hourDiff(com.cadenzauk.siesta.grammar.expression.DateFunctions.hourDiff) DateFunctions.second(com.cadenzauk.siesta.grammar.expression.DateFunctions.second) DateFunctions.year(com.cadenzauk.siesta.grammar.expression.DateFunctions.year) DateFunctions.minuteDiff(com.cadenzauk.siesta.grammar.expression.DateFunctions.minuteDiff) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DateFunctions.addDays(com.cadenzauk.siesta.grammar.expression.DateFunctions.addDays) RandomValues(com.cadenzauk.core.RandomValues) Timestamp(java.sql.Timestamp) DateFunctions.day(com.cadenzauk.siesta.grammar.expression.DateFunctions.day) Arguments(org.junit.jupiter.params.provider.Arguments) ZoneId(java.time.ZoneId) Date(java.sql.Date) Test(org.junit.jupiter.api.Test) DateFunctions.secondDiff(com.cadenzauk.siesta.grammar.expression.DateFunctions.secondDiff) Stream(java.util.stream.Stream) ArrayUtils.toArray(org.apache.commons.lang3.ArrayUtils.toArray) LocalDate(java.time.LocalDate) DateFunctions.hour(com.cadenzauk.siesta.grammar.expression.DateFunctions.hour) DateFunctions.minute(com.cadenzauk.siesta.grammar.expression.DateFunctions.minute) IsUtilityClass.isUtilityClass(com.cadenzauk.core.testutil.IsUtilityClass.isUtilityClass) DateFunctions.dayDiff(com.cadenzauk.siesta.grammar.expression.DateFunctions.dayDiff) ZonedDateTime(java.time.ZonedDateTime) TestRow(com.cadenzauk.siesta.model.TestRow) LocalDate(java.time.LocalDate)

Aggregations

TestRow (com.cadenzauk.siesta.model.TestRow)7 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 TestCase (com.cadenzauk.core.junit.TestCase)3 UncheckedAutoCloseable (com.cadenzauk.core.lang.UncheckedAutoCloseable)3 Test (org.junit.jupiter.api.Test)3 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)3 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 ZonedDateTime (java.time.ZonedDateTime)2 UUID (java.util.UUID)2 RandomValues (com.cadenzauk.core.RandomValues)1 RandomValues.randomLocalDateTime (com.cadenzauk.core.RandomValues.randomLocalDateTime)1 RandomValues.randomLocalTime (com.cadenzauk.core.RandomValues.randomLocalTime)1 RandomValues.randomZonedDateTime (com.cadenzauk.core.RandomValues.randomZonedDateTime)1 IsUtilityClass.isUtilityClass (com.cadenzauk.core.testutil.IsUtilityClass.isUtilityClass)1 Database (com.cadenzauk.siesta.Database)1 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)1 DateFunctions.addDays (com.cadenzauk.siesta.grammar.expression.DateFunctions.addDays)1 DateFunctions.day (com.cadenzauk.siesta.grammar.expression.DateFunctions.day)1