Search in sources :

Example 1 with WidgetRow

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

the class CountTest method countDistinct1.

@Test
void countDistinct1() {
    Database database = TestDatabase.testDatabase(new AnsiDialect());
    database.from(WidgetRow.class, "w").select(countDistinct(WidgetRow::name), "n").where(WidgetRow::manufacturerId).isEqualTo(4002L).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select count(distinct w.NAME) as n " + "from SIESTA.WIDGET w " + "where w.MANUFACTURER_ID = ?"));
    assertThat(args.getValue(), arrayWithSize(1));
    assertThat(args.getValue()[0], is(4002L));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 2 with WidgetRow

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

the class DatabaseIntegrationTest method leftJoinOfMissingIsNull.

@Test
void leftJoinOfMissingIsNull() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturer1 = newId();
    long manufacturer2 = newId();
    ManufacturerRow twoParts = ManufacturerRow.newBuilder().manufacturerId(manufacturer1).name(Optional.of("Has a Part")).build();
    ManufacturerRow noParts = ManufacturerRow.newBuilder().manufacturerId(manufacturer2).name(Optional.of("Has No Parts")).build();
    WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Name 1").build();
    database.insert(noParts, twoParts);
    database.insert(aWidget1);
    List<Tuple2<ManufacturerRow, WidgetRow>> result = database.from(ManufacturerRow.class, "m").leftJoin(WidgetRow.class, "w").on(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId).where(ManufacturerRow::manufacturerId).isIn(manufacturer1, manufacturer2).orderBy(ManufacturerRow::manufacturerId).list();
    assertThat(result, hasSize(2));
    assertThat(result.get(0).item1().name(), is(Optional.of("Has a Part")));
    assertThat(result.get(0).item2().name(), is("Name 1"));
    assertThat(result.get(1).item1().name(), is(Optional.of("Has No Parts")));
    assertThat(result.get(1).item2(), nullValue());
}
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 3 with WidgetRow

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

the class DatabaseIntegrationTest method countAndCountDistinct.

@Test
void countAndCountDistinct() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturer1 = newId();
    long manufacturer2 = newId();
    WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Gizmo").build();
    WidgetRow aWidget2 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Gizmo").build();
    WidgetRow aWidget3 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer2).name("Gizmo 2").build();
    database.insert(aWidget1, aWidget2, aWidget3);
    List<Tuple3<Long, Integer, Integer>> result = database.from(WidgetRow.class).select(WidgetRow::manufacturerId).comma(countDistinct(WidgetRow::name)).comma(count()).where(WidgetRow::widgetId).isIn(aWidget1.widgetId(), aWidget2.widgetId(), aWidget3.widgetId()).groupBy(WidgetRow::manufacturerId).orderBy(WidgetRow::manufacturerId, Order.ASC).list();
    assertThat(result.get(0).item1(), is(manufacturer1));
    assertThat(result.get(0).item2(), is(1));
    assertThat(result.get(0).item3(), is(2));
    assertThat(result.get(1).item1(), is(manufacturer2));
    assertThat(result.get(1).item2(), is(1));
    assertThat(result.get(1).item3(), is(1));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Tuple3(com.cadenzauk.core.tuple.Tuple3) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with WidgetRow

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

the class DatabaseIntegrationTest method selectFromDatabaseOneTable.

@Test
void selectFromDatabaseOneTable() {
    Database database = testDatabase(dataSource, dialect);
    WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(newId()).name("Dodacky").description(Optional.of("Thingamibob")).build();
    database.insert(aWidget);
    Optional<WidgetRow> theSame = database.from(WidgetRow.class).where(WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional();
    assertThat(theSame, is(Optional.of(aWidget)));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with WidgetRow

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

the class DatabaseIntegrationTest method caseExpression.

@Test
void caseExpression() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturerId = newId();
    WidgetRow doohicky = WidgetRow.newBuilder().widgetId(newId()).name("Doohicky").manufacturerId(manufacturerId).build();
    WidgetRow doohickie = WidgetRow.newBuilder().widgetId(newId()).name("Doohickie").manufacturerId(manufacturerId).build();
    WidgetRow doodad = WidgetRow.newBuilder().widgetId(newId()).name("Doodad").manufacturerId(manufacturerId).build();
    WidgetRow doofer = WidgetRow.newBuilder().widgetId(newId()).name("Doofer").manufacturerId(manufacturerId).build();
    database.insert(doohicky, doohickie, doodad, doofer);
    List<String> result = database.from(WidgetRow.class).select(when(WidgetRow::name).isLike("Doohick%").then(lower(WidgetRow::name)).when(WidgetRow::name).isEqualTo(value("Doodad")).then(upper(WidgetRow::name)).orElse(WidgetRow::name)).where(WidgetRow::manufacturerId).isEqualTo(manufacturerId).orderBy(WidgetRow::widgetId).list();
    assertThat(result.get(0), is("doohicky"));
    assertThat(result.get(1), is("doohickie"));
    assertThat(result.get(2), is("DOODAD"));
    assertThat(result.get(3), is("Doofer"));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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