Search in sources :

Example 1 with WidgetViewRow

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

the class DatabaseIntegrationTest method selectIntoView.

@Test
void selectIntoView() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturerId = newId();
    ManufacturerRow aManufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Acme")).build();
    WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Gizmo").description(Optional.of("Acme's Patent Gizmo")).build();
    database.insert(aManufacturer);
    database.insert(aWidget);
    Optional<WidgetViewRow> gizmo = database.from(WidgetRow.class, "w").leftJoin(ManufacturerRow.class, "m").on(ManufacturerRow::manufacturerId).isEqualTo(WidgetRow::manufacturerId).selectInto(WidgetViewRow.class, "v").with(WidgetRow::widgetId).as(WidgetViewRow::widgetId).with(WidgetRow::name).as(WidgetViewRow::widgetName).with(WidgetRow::description).as(WidgetViewRow::description).with(WidgetRow::manufacturerId).as(WidgetViewRow::manufacturerId).with(ManufacturerRow::name).as(WidgetViewRow::manufacturerName).where(WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional();
    assertThat(gizmo.map(WidgetViewRow::widgetName), is(Optional.of("Gizmo")));
    assertThat(gizmo.flatMap(WidgetViewRow::manufacturerName), is(Optional.of("Acme")));
    assertThat(gizmo.flatMap(WidgetViewRow::description), is(Optional.of("Acme's Patent Gizmo")));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) WidgetViewRow(com.cadenzauk.siesta.model.WidgetViewRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with WidgetViewRow

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

the class SelectProjectionTest method projectIntoObject.

@Test
void projectIntoObject() {
    Database database = TestDatabase.testDatabase(new AnsiDialect());
    database.from(WidgetRow.class, "w").join(ManufacturerRow.class, "m").on(ManufacturerRow::manufacturerId).isEqualTo(WidgetRow::manufacturerId).selectInto(WidgetViewRow.class, "v").with(WidgetRow::widgetId).as(WidgetViewRow::widgetId).with(WidgetRow::name).as(WidgetViewRow::widgetName).with(WidgetRow::description).as(WidgetViewRow::description).with(WidgetRow::manufacturerId).as(WidgetViewRow::manufacturerId).with(ManufacturerRow::name).as(WidgetViewRow::manufacturerName).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select w.WIDGET_ID as v_WIDGET_ID, " + "w.NAME as v_WIDGET_NAME, " + "w.DESCRIPTION as v_DESCRIPTION, " + "w.MANUFACTURER_ID as v_MANUFACTURER_ID, " + "m.NAME as v_MANUFACTURER_NAME " + "from SIESTA.WIDGET w " + "join SIESTA.MANUFACTURER m on m.MANUFACTURER_ID = w.MANUFACTURER_ID"));
    assertThat(args.getValue(), arrayWithSize(0));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) WidgetViewRow(com.cadenzauk.siesta.model.WidgetViewRow) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Aggregations

ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)2 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)2 WidgetViewRow (com.cadenzauk.siesta.model.WidgetViewRow)2 Test (org.junit.jupiter.api.Test)2 MockitoTest (com.cadenzauk.core.MockitoTest)1 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)1 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)1 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1