Search in sources :

Example 16 with UncheckedAutoCloseable

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

the class DataTypeTest method toDatabaseLocalDate.

@ParameterizedTest
@MethodSource("timeZones")
void toDatabaseLocalDate(String timeZone) {
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        LocalDate input = RandomValues.randomLocalDate();
        Date expected = Date.valueOf(input);
        when(db.dialect()).thenReturn(new AnsiDialect());
        Object result = DataType.LOCAL_DATE.toDatabase(db, input);
        assertThat(result, is(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) Date(java.sql.Date) 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 17 with UncheckedAutoCloseable

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

the class DataTypeTest method toDatabaseLocalDateTime.

@ParameterizedTest
@MethodSource("timeZones")
void toDatabaseLocalDateTime(String jvmTimeZone) {
    try (UncheckedAutoCloseable ignored = withTimeZone(jvmTimeZone)) {
        LocalDateTime input = RandomValues.randomLocalDateTime();
        Timestamp expected = Timestamp.valueOf(input);
        when(db.dialect()).thenReturn(new AnsiDialect());
        Object result = DataType.LOCAL_DATE_TIME.toDatabase(db, input);
        assertThat(result, is(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) Timestamp(java.sql.Timestamp) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 18 with UncheckedAutoCloseable

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

the class LocalDateConverterTest method convertToEntityAttribute.

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

Example 19 with UncheckedAutoCloseable

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

the class ZonedDateTimeConverterTest method convertToEntityAttribute.

@ParameterizedTest
@MethodSource("parameters")
void convertToEntityAttribute(String timeZone, LocalDate local) {
    Timestamp input = timestamp(timeZone, local);
    ZonedDateTime expected = zonedDateTime(timeZone, local);
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        ZonedDateTimeConverter sut = new ZonedDateTimeConverter(ZoneId.of(timeZone));
        ZonedDateTime actual = sut.convertToEntityAttribute(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 20 with UncheckedAutoCloseable

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

the class ZonedDateTimeConverterTest method convertToEntityAttributePreGregorianShouldThrow.

@ParameterizedTest
@MethodSource("timezones")
void convertToEntityAttributePreGregorianShouldThrow(String timeZone) {
    ZonedDateTime dateTime = ZonedDateTime.of(LocalDateUtil.START_OF_GREGORIAN_CALENDAR, LocalTime.MIN, ZoneId.of(timeZone)).minusNanos(1);
    Timestamp input = Timestamp.from(dateTime.toInstant());
    try (UncheckedAutoCloseable ignored = withTimeZone(timeZone)) {
        ZonedDateTimeConverter sut = new ZonedDateTimeConverter(ZoneId.of(timeZone));
        calling(() -> sut.convertToEntityAttribute(input)).shouldThrow(IllegalArgumentException.class);
    }
}
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)

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