Search in sources :

Example 1 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect in project siesta by cadenzauk.

the class CountTest method countDistinct1.

@Test
void countDistinct1() {
    Database database = TestDatabase.testDatabase(new AnsiDialect());
    database.from(WidgetRow.class, "w").select(countDistinct(WidgetRow::name), "n").where(WidgetRow::manufacturerId).isEqualTo(4002L).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select count(distinct w.NAME) as n " + "from SIESTA.WIDGET w " + "where w.MANUFACTURER_ID = ?"));
    assertThat(args.getValue(), arrayWithSize(1));
    assertThat(args.getValue()[0], is(4002L));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 2 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect in project siesta by cadenzauk.

the class DataTypeTest method get.

@ParameterizedTest
@MethodSource("parametersForGet")
<T> void get(DataType<T> sut, SqlBiConsumer<ResultSet, String> resultSetExtractor, Optional<ZoneId> dbTimeZone, T expected) throws SQLException {
    resultSetExtractor.accept(rs, "someColumn");
    when(db.dialect()).thenReturn(new AnsiDialect());
    dbTimeZone.ifPresent(zone -> when(db.databaseTimeZone()).thenReturn(zone));
    Optional<T> result = sut.get(rs, "someColumn", db);
    assertThat(result, is(Optional.of(expected)));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect in project siesta by cadenzauk.

the class DataTypeTest method getZonedDateTime.

@ParameterizedTest
@MethodSource("timeZones")
void getZonedDateTime(String timeZone) throws SQLException {
    ZonedDateTime expected = ZonedDateTime.of(randomLocalDateTime(), ZoneId.of("UTC"));
    Timestamp returnVal = Timestamp.from(expected.toInstant());
    when(rs.getTimestamp(eq("someColumn"), any())).thenReturn(returnVal);
    when(db.databaseTimeZone()).thenReturn(ZoneId.of(timeZone));
    when(db.dialect()).thenReturn(new AnsiDialect());
    Optional<ZonedDateTime> result = DataType.ZONED_DATE_TIME.get(rs, "someColumn", db);
    assertThat(result, is(Optional.of(expected)));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ZonedDateTime(java.time.ZonedDateTime) Timestamp(java.sql.Timestamp) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect 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 5 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect in project siesta by cadenzauk.

the class EmbeddedTest method embeddedOverride.

@Test
void embeddedOverride() {
    Database database = Database.newBuilder().dialect(new AnsiDialect()).build();
    database.from(CustomerWithOverride.class).select(CustomerWithOverride::address).comma(column(CustomerWithOverride::address).dot(Address::line1), "l1").comma(column(CustomerWithOverride::address).dot(Address::line2), "l2").list(sqlExecutor);
    Mockito.verify(sqlExecutor).query(sql.capture(), args.capture(), any());
    assertThat(sql.getValue(), is("select " + "CUSTOMER.ADDRESS_LINE1 as CUSTOMER_ADDRESS_LINE1, " + "CUSTOMER.ADDR_LINE_2 as CUSTOMER_ADDR_LINE_2, " + "CUSTOMER.ADDRESS_LINE1 as l1, " + "CUSTOMER.ADDR_LINE_2 as l2 " + "from CUSTOMER CUSTOMER"));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Aggregations

AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)56 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)36 Test (org.junit.jupiter.api.Test)32 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)23 Database (com.cadenzauk.siesta.Database)16 MockitoTest (com.cadenzauk.core.MockitoTest)14 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)10 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)10 ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)8 UncheckedAutoCloseable (com.cadenzauk.core.lang.UncheckedAutoCloseable)5 InWhereExpectingAnd (com.cadenzauk.siesta.grammar.select.InWhereExpectingAnd)4 Scope (com.cadenzauk.siesta.Scope)3 Timestamp (java.sql.Timestamp)3 RandomValues.randomLocalDate (com.cadenzauk.core.RandomValues.randomLocalDate)2 RandomValues.randomLocalDateTime (com.cadenzauk.core.RandomValues.randomLocalDateTime)2 InvalidJoinException (com.cadenzauk.siesta.grammar.InvalidJoinException)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2