Search in sources :

Example 56 with Database

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

the class DatabaseIntegrationTestH2 method ormUpdate.

@Test
void ormUpdate() {
    Database database = testDatabase(dataSource);
    SalespersonRow salespersonRow = aRandomSalesperson();
    SalespersonRow updated = SalespersonRow.newBuilder(salespersonRow).numberOfSales(salespersonRow.numberOfSales() + 10).build();
    database.insert(salespersonRow);
    database.update(updated);
    Integer valueInDatabase = database.from(SalespersonRow.class).select(SalespersonRow::numberOfSales).where(SalespersonRow::salespersonId).isEqualTo(salespersonRow.salespersonId()).single();
    assertThat(valueInDatabase, is(updated.numberOfSales()));
}
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 57 with Database

use of com.cadenzauk.siesta.Database 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)

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