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)));
}
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)))));
}
Aggregations