Search in sources :

Example 21 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class InProjectionExpectingCommaTest method testComma.

@ParameterizedTest
@MethodSource("argsForComma")
void testComma(BiFunction<InProjectionExpectingComma1<String>, Alias<SalespersonRow>, Select<?>> method, String expectedSql, Object[] expectedArgs) {
    MockitoAnnotations.initMocks(this);
    Database database = testDatabase(new AnsiDialect());
    Alias<SalespersonRow> alias = database.table(SalespersonRow.class).as("s");
    InProjectionExpectingComma1<String> sut = database.from(alias).select(SalespersonRow::firstName, "name");
    method.apply(sut, alias).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    Pattern pattern = Pattern.compile("select s.FIRST_NAME as name, " + expectedSql + " from SIESTA.SALESPERSON s");
    assertThat(pattern.matcher(sql.getValue()).matches(), is(true));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : Pattern(java.util.regex.Pattern) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class DatabaseIntegrationTestH2 method selectSubquery.

@Test
void selectSubquery() {
    Database database = testDatabase(dataSource);
    long manufacturerId = newId();
    ManufacturerRow manufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Doc Brown")).build();
    WidgetRow[] widgets = IntStream.range(1, 4).mapToObj(i -> WidgetRow.newBuilder().widgetId(newId()).name("Flux capacitor " + i).manufacturerId(manufacturerId).build()).toArray(WidgetRow[]::new);
    database.insert(manufacturer);
    database.insert(widgets);
    Tuple2<Long, Integer> result1 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count()).where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId)).where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
    Tuple2<Long, Integer> result2 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count()).where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId), "widget_count").where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
    Tuple2<Long, Integer> result3 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count(), "wcount").where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId), "wcount").where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
    assertThat(result1.item1(), is(manufacturerId));
    assertThat(result1.item2(), is(3));
    assertThat(result2.item1(), is(manufacturerId));
    assertThat(result2.item2(), is(3));
    assertThat(result3.item1(), is(manufacturerId));
    assertThat(result3.item2(), is(3));
}
Also used : Tuple2(com.cadenzauk.core.tuple.Tuple2) IntStream(java.util.stream.IntStream) RandomValues(com.cadenzauk.core.RandomValues) Tuple5(com.cadenzauk.core.tuple.Tuple5) Aggregates.count(com.cadenzauk.siesta.grammar.expression.Aggregates.count) DatabaseIntegrationTest(com.cadenzauk.siesta.DatabaseIntegrationTest) PartRow(com.cadenzauk.siesta.model.PartRow) TypedExpression.column(com.cadenzauk.siesta.grammar.expression.TypedExpression.column) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Optional(java.util.Optional) MoneyAmount(com.cadenzauk.siesta.model.MoneyAmount) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Database(com.cadenzauk.siesta.Database) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) DatabaseIntegrationTest(com.cadenzauk.siesta.DatabaseIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 23 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class DatabaseIntegrationTestH2 method embeddedThatIsAllNullsComesBackEmpty.

@Test
void embeddedThatIsAllNullsComesBackEmpty() {
    Database database = testDatabase(dataSource);
    long partId = newId();
    database.insert(PartRow.newBuilder().partId(partId).description("ABC").widgetId(3L).purchasePrice(null).retailPrice(Optional.empty()).build());
    Tuple5<PartRow, MoneyAmount, MoneyAmount, String, String> result = database.from(PartRow.class).select(PartRow.class).comma(PartRow::purchasePrice, "pp").comma(PartRow::retailPrice, "rp").comma(column(PartRow::purchasePrice).dot(MoneyAmount::currency), "pc").comma(column(PartRow::retailPrice).dot(MoneyAmount::currency), "rc").where(PartRow::partId).isEqualTo(partId).single();
    assertThat(result.item1().purchasePrice(), nullValue());
    assertThat(result.item1().retailPrice(), is(Optional.empty()));
    assertThat(result.item2(), nullValue());
    assertThat(result.item3(), nullValue());
    assertThat(result.item4(), nullValue());
    assertThat(result.item5(), nullValue());
}
Also used : PartRow(com.cadenzauk.siesta.model.PartRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) MoneyAmount(com.cadenzauk.siesta.model.MoneyAmount) DatabaseIntegrationTest(com.cadenzauk.siesta.DatabaseIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 24 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class DatabaseIntegrationTestH2 method ormDelete.

@Test
void ormDelete() {
    Database database = testDatabase(dataSource);
    SalespersonRow salespersonRow = aRandomSalesperson();
    database.insert(salespersonRow);
    int count = countOf(database, salespersonRow);
    assertThat(count, is(1));
    database.delete(salespersonRow);
    count = countOf(database, salespersonRow);
    assertThat(count, is(0));
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) DatabaseIntegrationTest(com.cadenzauk.siesta.DatabaseIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 25 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class BooleanExpressionTest method testAnd.

@ParameterizedTest
@MethodSource("argsForAnd")
void testAnd(BiFunction<Alias<SalespersonRow>, BooleanExpression, BooleanExpression> method, String expectedSql) {
    MockitoAnnotations.initMocks(this);
    Database database = testDatabase(new AnsiDialect());
    Alias<SalespersonRow> alias = database.table(SalespersonRow.class).as("s");
    Scope scope = new Scope(database, alias);
    when(sut.precedence()).thenReturn(Precedence.UNARY);
    method.apply(alias, sut);
    verify(sut).appendAnd(appendArg.capture());
    assertThat(appendArg.getValue().sql(scope), is(expectedSql));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) Scope(com.cadenzauk.siesta.Scope) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Database (com.cadenzauk.siesta.Database)57 Test (org.junit.jupiter.api.Test)41 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)21 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 ZonedDateTime (java.time.ZonedDateTime)17 Optional (java.util.Optional)17 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)16 Alias (com.cadenzauk.siesta.Alias)15 BigDecimal (java.math.BigDecimal)14 Collectors.joining (java.util.stream.Collectors.joining)14 AttributeOverride (javax.persistence.AttributeOverride)14 Embedded (javax.persistence.Embedded)14 EmbeddedId (javax.persistence.EmbeddedId)14 Id (javax.persistence.Id)14 CoreMatchers.is (org.hamcrest.CoreMatchers.is)14 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)13 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)7 MockitoTest (com.cadenzauk.core.MockitoTest)5