Search in sources :

Example 1 with ManufacturerRow

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

the class FromTest method validateInvalidThrowsWhenWrongAlias.

@Test
void validateInvalidThrowsWhenWrongAlias() {
    Database database = testDatabase(new AnsiDialect());
    calling(() -> database.from(ManufacturerRow.class, "m1").join(ManufacturerRow.class, "m2").on("m1", ManufacturerRow::name).isEqualTo("Fred").sql()).shouldThrow(InvalidJoinException.class).withMessage(is("Joined table 'SIESTA.MANUFACTURER m2' is not referenced in the ON clause."));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) InvalidJoinException(com.cadenzauk.siesta.grammar.InvalidJoinException) Test(org.junit.jupiter.api.Test)

Example 2 with ManufacturerRow

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

the class FromTest method invalidJoinCanBeSuppressed.

@Test
void invalidJoinCanBeSuppressed() {
    Database database = testDatabase(new AnsiDialect());
    String sql = database.from(ManufacturerRow.class, "m1").join(ManufacturerRow.class, "m2").validate(false).on("m1", ManufacturerRow::name).isEqualTo("Fred").sql();
    assertThat(sql, startsWith("select m1.MANUFACTURER_ID as m1_MANUFACTURER_ID"));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test)

Example 3 with ManufacturerRow

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

the class DatabaseIntegrationTest method delete.

@Test
void delete() {
    Database database = testDatabase(dataSource, dialect);
    long manufacturer1 = newId();
    long manufacturer2 = newId();
    ManufacturerRow manufacturerRow1 = ManufacturerRow.newBuilder().manufacturerId(manufacturer1).name(Optional.of("tbc 1")).build();
    ManufacturerRow manufacturerRow2 = ManufacturerRow.newBuilder().manufacturerId(manufacturer2).name(Optional.of("tbc 2")).build();
    database.insert(manufacturerRow1, manufacturerRow2);
    assertThat(database.from(ManufacturerRow.class).select(count()).where(ManufacturerRow::name).isLike("tbc %").single(), is(2));
    int rowsDeleted = database.delete(ManufacturerRow.class).where(ManufacturerRow::name).isEqualTo("tbc 1").execute();
    assertThat(database.from(ManufacturerRow.class).select(count()).where(ManufacturerRow::name).isLike("tbc %").single(), is(1));
    assertThat(rowsDeleted, is(1));
}
Also used : ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ManufacturerRow

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

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

Aggregations

ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)15 Test (org.junit.jupiter.api.Test)15 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)13 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)8 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Tuple2 (com.cadenzauk.core.tuple.Tuple2)5 MockitoTest (com.cadenzauk.core.MockitoTest)2 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)2 WidgetViewRow (com.cadenzauk.siesta.model.WidgetViewRow)2 RandomValues (com.cadenzauk.core.RandomValues)1 Tuple5 (com.cadenzauk.core.tuple.Tuple5)1 Database (com.cadenzauk.siesta.Database)1 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)1 InvalidJoinException (com.cadenzauk.siesta.grammar.InvalidJoinException)1 Aggregates.count (com.cadenzauk.siesta.grammar.expression.Aggregates.count)1 TypedExpression.column (com.cadenzauk.siesta.grammar.expression.TypedExpression.column)1 MoneyAmount (com.cadenzauk.siesta.model.MoneyAmount)1 PartRow (com.cadenzauk.siesta.model.PartRow)1 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)1