Search in sources :

Example 6 with WidgetRow

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

the class DatabaseIntegrationTest method selectRow.

@Test
void selectRow() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturerId = newId();
    ManufacturerRow aManufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Acclaimed Widgets")).build();
    WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Acclaimed 1").build();
    WidgetRow aWidget2 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Acclaimed 2").build();
    database.insert(aManufacturer);
    database.insert(aWidget1, aWidget2);
    List<WidgetRow> acclaimedWidgets1 = database.from(ManufacturerRow.class, "m").join(WidgetRow.class, "w").on(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId).select(WidgetRow.class).where(ManufacturerRow::name).isEqualTo("Acclaimed Widgets").orderBy(WidgetRow::name, DESC).list();
    List<Tuple2<String, WidgetRow>> acclaimedWidgets2 = database.from(ManufacturerRow.class, "m").join(WidgetRow.class, "w").on(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId).select(WidgetRow::name, "name").comma(WidgetRow.class).where(ManufacturerRow::name).isEqualTo("Acclaimed Widgets").orderBy(WidgetRow::name, Order.ASC).list();
    assertThat(acclaimedWidgets1, hasSize(2));
    assertThat(acclaimedWidgets1.get(0).name(), is("Acclaimed 2"));
    assertThat(acclaimedWidgets1.get(1).name(), is("Acclaimed 1"));
    assertThat(acclaimedWidgets2, hasSize(2));
    assertThat(acclaimedWidgets2.get(0).item1(), is("Acclaimed 1"));
    assertThat(acclaimedWidgets2.get(1).item1(), is("Acclaimed 2"));
    assertThat(acclaimedWidgets2.get(0).item2().name(), is("Acclaimed 1"));
    assertThat(acclaimedWidgets2.get(1).item2().name(), is("Acclaimed 2"));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) Tuple2(com.cadenzauk.core.tuple.Tuple2) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with WidgetRow

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

the class DatabaseIntegrationTest method selectFromDatabaseTwoTables.

@Test
void selectFromDatabaseTwoTables() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturerId = newId();
    ManufacturerRow aManufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Makers")).build();
    WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Dodacky").description(Optional.of("Thingamibob")).build();
    database.insert(aManufacturer);
    database.insert(aWidget);
    Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
    Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
    Optional<WidgetRow> theSame = database.from(w).join(m).on(m, ManufacturerRow::manufacturerId).isEqualTo(w, WidgetRow::manufacturerId).where(w, WidgetRow::name).isEqualTo("Dodacky").and(w, WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional().map(Tuple2::item1);
    assertThat(theSame, is(Optional.of(aWidget)));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) Tuple2(com.cadenzauk.core.tuple.Tuple2) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with WidgetRow

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

the class UpdateTest method update.

@Test
void update() {
    Database database = Database.newBuilder().defaultSchema("SIESTA").table(WidgetRow.class, t -> t.builder(WidgetRow.Builder::build)).build();
    database.update(WidgetRow.class, "w").set(WidgetRow::name).to("Fred").set(WidgetRow::description).to(Optional.of("Bob")).where(WidgetRow::widgetId).isEqualTo(1L).and(column(WidgetRow::description).isBetween("A").and("w", WidgetRow::name).or(column(WidgetRow::description).isNull())).execute(transaction);
    verify(transaction).update(sql.capture(), args.capture());
    assertThat(sql.getValue(), is("update SIESTA.WIDGET w " + "set NAME = ?, " + "DESCRIPTION = ? " + "where w.WIDGET_ID = ? " + "and (" + "w.DESCRIPTION between ? and w.NAME " + "or w.DESCRIPTION is null" + ")"));
    assertThat(args.getValue(), is(toArray("Fred", "Bob", 1L, "A")));
}
Also used : Mock(org.mockito.Mock) Captor(org.mockito.Captor) TypedExpression.column(com.cadenzauk.siesta.grammar.expression.TypedExpression.column) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ArgumentCaptor(org.mockito.ArgumentCaptor) ArrayUtils.toArray(org.apache.commons.lang3.ArrayUtils.toArray) MockitoTest(com.cadenzauk.core.MockitoTest) Transaction(com.cadenzauk.siesta.Transaction) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Database(com.cadenzauk.siesta.Database) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 9 with WidgetRow

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

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

the class CaseExpressionTest method initialWhen.

@ParameterizedTest
@MethodSource("parametersForInitialWhen")
void initialWhen(String expected, Function<Alias<WidgetRow>, CaseExpression<String>> f, Object[] expectedArgs) {
    Database database = testDatabase(new AnsiDialect());
    Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
    database.from(w).select(f.apply(w).orElse(literal("DEFAULT")), "name").list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), any());
    assertThat(sql.getValue(), is("select case " + expected + " else 'DEFAULT' end as name " + "from SIESTA.WIDGET w"));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

WidgetRow (com.cadenzauk.siesta.model.WidgetRow)37 Test (org.junit.jupiter.api.Test)32 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)26 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)23 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 MockitoTest (com.cadenzauk.core.MockitoTest)9 ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)8 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)8 Database (com.cadenzauk.siesta.Database)7 Tuple2 (com.cadenzauk.core.tuple.Tuple2)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 Tuple3 (com.cadenzauk.core.tuple.Tuple3)2 TypedExpression.column (com.cadenzauk.siesta.grammar.expression.TypedExpression.column)2 WidgetViewRow (com.cadenzauk.siesta.model.WidgetViewRow)2 Optional (java.util.Optional)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.is (org.hamcrest.Matchers.is)2 RandomValues (com.cadenzauk.core.RandomValues)1 Tuple5 (com.cadenzauk.core.tuple.Tuple5)1 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)1