Search in sources :

Example 11 with UncheckedAutoCloseable

use of com.cadenzauk.core.lang.UncheckedAutoCloseable in project siesta by cadenzauk.

the class DatabaseIntegrationTest method localDateTimeLiteral.

@SuppressWarnings("UnnecessaryLocalVariable")
@ParameterizedTest
@MethodSource("parametersForLocalDateTimeLiteral")
void localDateTimeLiteral(String timeZone, LocalDateTime value) {
    Database database = testDatabase(dataSource, dialect);
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        LocalDateTime expected = value;
        LocalDateTime result = database.select(literal(value)).single();
        assertThat(result, is(expected));
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) RandomValues.randomLocalDateTime(com.cadenzauk.core.RandomValues.randomLocalDateTime) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with UncheckedAutoCloseable

use of com.cadenzauk.core.lang.UncheckedAutoCloseable in project siesta by cadenzauk.

the class DatabaseIntegrationTest method canRoundTripLocalDateTimes.

@ParameterizedTest
@ArgumentsSource(TestCaseArgumentsProvider.class)
@TestCase({ "America/Anchorage" })
@TestCase({ "America/Sao_Paulo" })
@TestCase({ "UTC" })
@TestCase({ "Europe/London" })
@TestCase({ "Africa/Johannesburg" })
@TestCase({ "Pacific/Chatham" })
void canRoundTripLocalDateTimes(String timeZone) {
    Database database = testDatabase(dataSource, dialect);
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        LocalDateTime expected = randomLocalDateTime();
        TestRow input = TestRow.of(expected);
        database.insert(input);
        LocalDateTime result = database.from(TestRow.class).select(TestRow::localDateTimeOpt).where(TestRow::guid).isEqualTo(input.guid()).single();
        assertThat(result, is(expected));
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) RandomValues.randomLocalDateTime(com.cadenzauk.core.RandomValues.randomLocalDateTime) TestRow(com.cadenzauk.siesta.model.TestRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) TestCase(com.cadenzauk.core.junit.TestCase) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 13 with UncheckedAutoCloseable

use of com.cadenzauk.core.lang.UncheckedAutoCloseable in project siesta by cadenzauk.

the class DatabaseIntegrationTest method currentTimestampTest.

@ParameterizedTest
@ArgumentsSource(TestCaseArgumentsProvider.class)
@TestCase({ "America/Anchorage" })
@TestCase({ "America/Sao_Paulo" })
@TestCase({ "UTC" })
@TestCase({ "Europe/London" })
@TestCase({ "Africa/Johannesburg" })
@TestCase({ "Pacific/Chatham" })
void currentTimestampTest(String timeZone) {
    Database database = testDatabase(dataSource, dialect);
    try (UncheckedAutoCloseable ignored = TemporalTestUtil.withTimeZone(timeZone)) {
        ZonedDateTime before = ZonedDateTime.now(ZoneId.of("UTC")).minusSeconds(10);
        ZonedDateTime now = database.select(currentTimestamp()).single();
        ZonedDateTime after = ZonedDateTime.now(ZoneId.of("UTC")).plusSeconds(10);
        System.out.printf("%s <= %s <= %s%n", before, now, after);
        assertThat(before.isAfter(now), is(false));
        assertThat(now.isAfter(after), is(false));
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) RandomValues.randomZonedDateTime(com.cadenzauk.core.RandomValues.randomZonedDateTime) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) TestCase(com.cadenzauk.core.junit.TestCase) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 14 with UncheckedAutoCloseable

use of com.cadenzauk.core.lang.UncheckedAutoCloseable in project siesta by cadenzauk.

the class DataTypeTest method getLocalDate.

@ParameterizedTest
@MethodSource("timeZones")
void getLocalDate(String timeZone) throws SQLException {
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        LocalDate expected = randomLocalDate();
        when(db.dialect()).thenReturn(new AnsiDialect());
        when(rs.getDate(eq("someColumn"), any())).thenReturn(Date.valueOf(expected));
        Optional<LocalDate> result = DataType.LOCAL_DATE.get(rs, "someColumn", db);
        assertThat(result, is(Optional.of(expected)));
    }
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) RandomValues.randomLocalDate(com.cadenzauk.core.RandomValues.randomLocalDate) LocalDate(java.time.LocalDate) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 15 with UncheckedAutoCloseable

use of com.cadenzauk.core.lang.UncheckedAutoCloseable in project siesta by cadenzauk.

the class DataTypeTest method getLocalDateTime.

@ParameterizedTest
@MethodSource("timeZones")
void getLocalDateTime(String timeZone) throws SQLException {
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        LocalDateTime expected = randomLocalDateTime();
        when(db.dialect()).thenReturn(new AnsiDialect());
        when(rs.getTimestamp(eq("someColumn"), any())).thenReturn(Timestamp.valueOf(expected));
        Optional<LocalDateTime> result = DataType.LOCAL_DATE_TIME.get(rs, "someColumn", db);
        assertThat(result, is(Optional.of(expected)));
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) RandomValues.randomLocalDateTime(com.cadenzauk.core.RandomValues.randomLocalDateTime) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

UncheckedAutoCloseable (com.cadenzauk.core.lang.UncheckedAutoCloseable)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)22 MethodSource (org.junit.jupiter.params.provider.MethodSource)16 ZonedDateTime (java.time.ZonedDateTime)10 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)9 Timestamp (java.sql.Timestamp)7 TestCase (com.cadenzauk.core.junit.TestCase)6 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)6 RandomValues.randomLocalDateTime (com.cadenzauk.core.RandomValues.randomLocalDateTime)5 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)5 LocalDate (java.time.LocalDate)5 LocalDateTime (java.time.LocalDateTime)5 RandomValues.randomZonedDateTime (com.cadenzauk.core.RandomValues.randomZonedDateTime)3 TestRow (com.cadenzauk.siesta.model.TestRow)3 RandomValues.randomLocalDate (com.cadenzauk.core.RandomValues.randomLocalDate)2 RandomValues.randomLocalTime (com.cadenzauk.core.RandomValues.randomLocalTime)2 Date (java.sql.Date)2 LocalTime (java.time.LocalTime)2 ZoneId (java.time.ZoneId)1