Search in sources :

Example 6 with TestCase

use of com.cadenzauk.core.junit.TestCase in project siesta by cadenzauk.

the class DatabaseIntegrationTest method zonedDateTimePartFuncs.

@ParameterizedTest
@ArgumentsSource(TestCaseArgumentsProvider.class)
@TestCase({ "America/Anchorage" })
@TestCase({ "America/Sao_Paulo" })
@TestCase({ "UTC" })
@TestCase({ "Europe/London" })
@TestCase({ "Africa/Johannesburg" })
@TestCase({ "Pacific/Chatham" })
void zonedDateTimePartFuncs(String timeZone) {
    Database database = testDatabaseBuilder(dialect).defaultSqlExecutor(JdbcSqlExecutor.of(dataSource)).databaseTimeZone(ZoneId.of(timeZone)).build();
    ZonedDateTime dateTime = RandomValues.randomZonedDateTime(database.databaseTimeZone());
    Tuple7<Integer, Integer, Integer, Integer, Integer, Integer, Integer> result = database.select(year(LiteralExpression.of(dateTime))).comma(month(ValueExpression.of(dateTime))).comma(day(LiteralExpression.of(dateTime))).comma(hour(ValueExpression.of(dateTime))).comma(hour(LiteralExpression.of(dateTime))).comma(minute(LiteralExpression.of(dateTime))).comma(second(ValueExpression.of(dateTime))).single();
    assertThat(result.item1(), is(dateTime.getYear()));
    assertThat(result.item2(), is(dateTime.getMonthValue()));
    assertThat(result.item3(), is(dateTime.getDayOfMonth()));
    assertThat(result.item4(), is(dateTime.getHour()));
    assertThat(result.item5(), is(dateTime.getHour()));
    assertThat(result.item6(), is(dateTime.getMinute()));
    assertThat(result.item7(), is(dateTime.getSecond()));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) RandomValues.randomZonedDateTime(com.cadenzauk.core.RandomValues.randomZonedDateTime) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) TestCase(com.cadenzauk.core.junit.TestCase) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 7 with TestCase

use of com.cadenzauk.core.junit.TestCase 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 8 with TestCase

use of com.cadenzauk.core.junit.TestCase 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)

Aggregations

TestCase (com.cadenzauk.core.junit.TestCase)8 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)7 UncheckedAutoCloseable (com.cadenzauk.core.lang.UncheckedAutoCloseable)6 RandomValues.randomZonedDateTime (com.cadenzauk.core.RandomValues.randomZonedDateTime)3 TestRow (com.cadenzauk.siesta.model.TestRow)3 ZonedDateTime (java.time.ZonedDateTime)3 RandomValues.randomLocalDateTime (com.cadenzauk.core.RandomValues.randomLocalDateTime)2 LocalDateTime (java.time.LocalDateTime)2 RandomValues.randomLocalTime (com.cadenzauk.core.RandomValues.randomLocalTime)1 Method (java.lang.reflect.Method)1 LocalDate (java.time.LocalDate)1 LocalTime (java.time.LocalTime)1 Test (org.junit.jupiter.api.Test)1