Search in sources :

Example 1 with UncheckedAutoCloseable

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

the class DataTypeTest method toDatabaseZonedDateTime.

@ParameterizedTest
@MethodSource("timeZonePairs")
void toDatabaseZonedDateTime(String dbTimeZone, String jvmTimeZone) {
    ZoneId dbZone = ZoneId.of(dbTimeZone);
    ZoneId jvmZone = ZoneId.of(jvmTimeZone);
    ZonedDateTime input = RandomValues.randomZonedDateTime(jvmZone);
    try (UncheckedAutoCloseable ignored = withTimeZone(jvmTimeZone)) {
        ZonedDateTime dbTime = input.withZoneSameInstant(dbZone);
        Timestamp expected = Timestamp.valueOf(dbTime.toLocalDateTime());
        when(db.databaseTimeZone()).thenReturn(dbZone);
        when(db.dialect()).thenReturn(new AnsiDialect());
        Object result = DataType.ZONED_DATE_TIME.toDatabase(db, input);
        assertThat(result, is(expected));
    }
}
Also used : ZoneId(java.time.ZoneId) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ZonedDateTime(java.time.ZonedDateTime) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) Timestamp(java.sql.Timestamp) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with UncheckedAutoCloseable

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

the class LocalDateConverterTest method convertToDatabaseColumn.

@ParameterizedTest
@MethodSource("parameters")
void convertToDatabaseColumn(String timeZone, LocalDate input, Date expected) {
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        LocalDateConverter sut = new LocalDateConverter();
        Date actual = sut.convertToDatabaseColumn(input);
        assertThat(actual, is(expected));
    }
}
Also used : UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) Date(java.sql.Date) LocalDate(java.time.LocalDate) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with UncheckedAutoCloseable

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

the class ZonedDateTimeConverterTest method convertToDatabaseColumn.

@ParameterizedTest
@MethodSource("parameters")
void convertToDatabaseColumn(String timeZone, LocalDate local) {
    ZonedDateTime input = zonedDateTime(timeZone, local);
    Timestamp expected = timestamp(timeZone, local);
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        ZonedDateTimeConverter sut = new ZonedDateTimeConverter(ZoneId.of(timeZone));
        Timestamp actual = sut.convertToDatabaseColumn(input);
        assertThat(actual, is(expected));
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) Timestamp(java.sql.Timestamp) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with UncheckedAutoCloseable

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

the class ZonedDateTimeConverterTest method convertToDatabaseColumnPreGregorianShouldThrow.

@ParameterizedTest
@MethodSource("timezones")
void convertToDatabaseColumnPreGregorianShouldThrow(String timeZone) {
    ZonedDateTime input = ZonedDateTime.of(LocalDateUtil.START_OF_GREGORIAN_CALENDAR, LocalTime.MIN, ZoneId.of(timeZone)).minusNanos(1);
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        ZonedDateTimeConverter sut = new ZonedDateTimeConverter(ZoneId.of(timeZone));
        calling(() -> sut.convertToDatabaseColumn(input)).shouldThrow(IllegalArgumentException.class);
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) UncheckedAutoCloseable(com.cadenzauk.core.lang.UncheckedAutoCloseable) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with UncheckedAutoCloseable

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

the class DatabaseIntegrationTest method canRoundTripLocalTimes.

@ParameterizedTest
@ArgumentsSource(TestCaseArgumentsProvider.class)
@TestCase({ "America/Anchorage" })
@TestCase({ "America/Sao_Paulo" })
@TestCase({ "UTC" })
@TestCase({ "Europe/London" })
@TestCase({ "Africa/Johannesburg" })
@TestCase({ "Pacific/Chatham" })
void canRoundTripLocalTimes(String timeZone) {
    Database database = testDatabase(dataSource, dialect);
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        LocalTime expected = randomLocalTime();
        TestRow input = TestRow.of(expected);
        database.insert(input);
        LocalTime result = database.from(TestRow.class).select(TestRow::localTimeOpt).where(TestRow::guid).isEqualTo(input.guid()).single();
        assertThat(result, is(expected));
    }
}
Also used : LocalTime(java.time.LocalTime) RandomValues.randomLocalTime(com.cadenzauk.core.RandomValues.randomLocalTime) 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)

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