Search in sources :

Example 1 with PartRow

use of com.cadenzauk.siesta.model.PartRow in project siesta by cadenzauk.

the class EmbeddedTest method embeddedInWhereClause.

@Test
void embeddedInWhereClause() {
    Database database = testDatabaseBuilder().defaultSchema("SIESTA").table(PartRow.class, t -> t.embedded(MoneyAmount.class, PartRow::purchasePrice, pp -> pp.columnName("PURCH_PRICE").column(MoneyAmount::amount, "PURCHASE_PRICE_AMT")).embedded(MoneyAmount.class, PartRow::retailPrice, rp -> rp.column(MoneyAmount::amount, "RETAIL_PRICE_AMT"))).build();
    database.from(PartRow.class).select(Aggregates.countDistinct(column(PartRow::purchasePrice).dot(MoneyAmount::amount))).where(PartRow::purchasePrice).dot(MoneyAmount::currency).isEqualTo("USD").or(PartRow::retailPrice).dot(MoneyAmount::currency).isEqualTo("NZD").optional(sqlExecutor);
    Mockito.verify(sqlExecutor).query(sql.capture(), args.capture(), any());
    assertThat(sql.getValue(), is("select count(distinct PART.PURCHASE_PRICE_AMT) as count_PART_PURCHASE_PRICE_AMT " + "from SIESTA.PART PART where PART.PURCH_PRICE_CCY = ? or PART.RETAIL_PRICE_CCY = ?"));
    assertThat(args.getValue(), is(toArray("USD", "NZD")));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Aggregates(com.cadenzauk.siesta.grammar.expression.Aggregates) Mock(org.mockito.Mock) PartRow(com.cadenzauk.siesta.model.PartRow) TestDatabase.testDatabaseBuilder(com.cadenzauk.siesta.model.TestDatabase.testDatabaseBuilder) Captor(org.mockito.Captor) TypedExpression.column(com.cadenzauk.siesta.grammar.expression.TypedExpression.column) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) AttributeOverrides(javax.persistence.AttributeOverrides) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) AttributeOverride(javax.persistence.AttributeOverride) Column(javax.persistence.Column) ArgumentCaptor(org.mockito.ArgumentCaptor) Table(javax.persistence.Table) ArrayUtils.toArray(org.apache.commons.lang3.ArrayUtils.toArray) MockitoTest(com.cadenzauk.core.MockitoTest) Optional(java.util.Optional) MoneyAmount(com.cadenzauk.siesta.model.MoneyAmount) Matchers.is(org.hamcrest.Matchers.is) Embeddable(javax.persistence.Embeddable) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) PartRow(com.cadenzauk.siesta.model.PartRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) MoneyAmount(com.cadenzauk.siesta.model.MoneyAmount) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 2 with PartRow

use of com.cadenzauk.siesta.model.PartRow 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 3 with PartRow

use of com.cadenzauk.siesta.model.PartRow in project siesta by cadenzauk.

the class DatabaseIntegrationTestH2 method embeddedInsertedAndReadBack.

@Test
void embeddedInsertedAndReadBack() {
    Database database = testDatabase(dataSource);
    long partId = newId();
    MoneyAmount purchasePrice = new MoneyAmount(RandomValues.randomBigDecimal(10, 2), "USD");
    MoneyAmount retailPrice = new MoneyAmount(RandomValues.randomBigDecimal(10, 2), "NZD");
    database.insert(PartRow.newBuilder().partId(partId).description("ABC").widgetId(3L).purchasePrice(purchasePrice).retailPrice(Optional.of(retailPrice)).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(), is(purchasePrice));
    assertThat(result.item1().retailPrice(), is(Optional.of(retailPrice)));
    assertThat(result.item2(), is(purchasePrice));
    assertThat(result.item3(), is(retailPrice));
    assertThat(result.item4(), is("USD"));
    assertThat(result.item5(), is("NZD"));
}
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 4 with PartRow

use of com.cadenzauk.siesta.model.PartRow in project siesta by cadenzauk.

the class EmbeddedTest method embeddedInProjection.

@Test
void embeddedInProjection() {
    Database database = testDatabase(new AnsiDialect());
    database.from(PartRow.class).select(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(4L).optional(sqlExecutor);
    Mockito.verify(sqlExecutor).query(sql.capture(), args.capture(), any());
    assertThat(sql.getValue(), is("select PART.PURCHASE_PRICE_AMOUNT as pp_PURCHASE_PRICE_AMOUNT, " + "PART.PURCHASE_PRICE_CCY as pp_PURCHASE_PRICE_CCY, " + "PART.RETAIL_PRICE_AMOUNT as rp_RETAIL_PRICE_AMOUNT, " + "PART.RETAIL_PRICE_CCY as rp_RETAIL_PRICE_CCY, " + "PART.PURCHASE_PRICE_CCY as pc, " + "PART.RETAIL_PRICE_CCY as rc " + "from SIESTA.PART PART " + "where PART.PART_ID = ?"));
    assertThat(args.getValue(), is(toArray(4L)));
}
Also used : PartRow(com.cadenzauk.siesta.model.PartRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) MoneyAmount(com.cadenzauk.siesta.model.MoneyAmount) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Aggregations

MoneyAmount (com.cadenzauk.siesta.model.MoneyAmount)4 PartRow (com.cadenzauk.siesta.model.PartRow)4 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)4 Test (org.junit.jupiter.api.Test)4 MockitoTest (com.cadenzauk.core.MockitoTest)2 Database (com.cadenzauk.siesta.Database)2 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)2 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)2 Aggregates (com.cadenzauk.siesta.grammar.expression.Aggregates)1 TypedExpression.column (com.cadenzauk.siesta.grammar.expression.TypedExpression.column)1 TestDatabase.testDatabaseBuilder (com.cadenzauk.siesta.model.TestDatabase.testDatabaseBuilder)1 Optional (java.util.Optional)1 AttributeOverride (javax.persistence.AttributeOverride)1 AttributeOverrides (javax.persistence.AttributeOverrides)1 Column (javax.persistence.Column)1 Embeddable (javax.persistence.Embeddable)1 Table (javax.persistence.Table)1 ArrayUtils.toArray (org.apache.commons.lang3.ArrayUtils.toArray)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1