Search in sources :

Example 31 with SalespersonRow

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

the class ExceptionIntegrationTest method foreignKeyViolationOnUpdate.

@Test
void foreignKeyViolationOnUpdate() {
    Database database = TestDatabase.testDatabase(dataSource);
    SalespersonRow salespersonRow = aRandomSalesperson();
    database.insert(salespersonRow);
    SalesAreaRow salesArea = SalesAreaRow.newBuilder().salesAreaId(newId()).salesAreaName("Far East").salespersonId(Optional.of(salespersonRow.salespersonId())).build();
    database.insert(salesArea);
    calling(() -> database.update(SalesAreaRow.class).set(SalesAreaRow::salespersonId).to(newId()).where(SalesAreaRow::salesAreaId).isEqualTo(salesArea.salesAreaId()).execute()).shouldThrow(RuntimeSqlException.class).with(subclass(ReferentialIntegrityException.class)).withMessage(startsWith("Foreign key constraint violated while executing 'update "));
}
Also used : TestDatabase(com.cadenzauk.siesta.model.TestDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) IncorrectSalesAreaRow(com.cadenzauk.siesta.model.IncorrectSalesAreaRow) SalesAreaRow(com.cadenzauk.siesta.model.SalesAreaRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 32 with SalespersonRow

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

the class ExceptionIntegrationTest method uniqueConstraintViolationOnInsert.

@Test
void uniqueConstraintViolationOnInsert() {
    Database database = TestDatabase.testDatabase(dataSource);
    SalespersonRow salespersonRow = aRandomSalesperson();
    calling(() -> database.insert(salespersonRow, salespersonRow)).shouldThrow(RuntimeSqlException.class).with(subclass(DuplicateKeyException.class)).withMessage(startsWith("Unique constraint violated while executing 'insert into "));
}
Also used : TestDatabase(com.cadenzauk.siesta.model.TestDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with SalespersonRow

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

the class TransactionIntegrationTest method whenUncommittedThenVisibleWithinTransaction.

@Test
void whenUncommittedThenVisibleWithinTransaction() {
    Database database = testDatabase(dataSource, dialect);
    SalespersonRow salesperson = aRandomSalesperson();
    try (Transaction transaction = database.beginTransaction()) {
        database.insert(transaction, salesperson);
        Integer count = database.from(SalespersonRow.class).select(count()).where(SalespersonRow::salespersonId).isEqualTo(salesperson.salespersonId()).fetchFirst(1).withIsolation(IsolationLevel.UNCOMMITTED_READ).singleAsync(transaction).join();
        assertThat(count, is(1));
    }
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test)

Example 34 with SalespersonRow

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

the class BetweenBuilderTest method and.

@ParameterizedTest
@MethodSource("argsForAnd")
void and(BiFunction<BetweenBuilder<String, InWhereExpectingAnd<String>>, Alias<SalespersonRow>, InWhereExpectingAnd<String>> method, String expectedSql, Object[] expectedArgs) {
    MockitoAnnotations.initMocks(this);
    Database database = testDatabase(new AnsiDialect());
    Alias<SalespersonRow> alias = database.table(SalespersonRow.class).as("s");
    BetweenBuilder<String, InWhereExpectingAnd<String>> between = database.from(alias).select(SalespersonRow::firstName, "name").where(SalespersonRow::firstName).isBetween(SalespersonRow::middleNames);
    method.apply(between, alias).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select s.FIRST_NAME as name from SIESTA.SALESPERSON s where s.FIRST_NAME between s.MIDDLE_NAMES and " + expectedSql));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : InWhereExpectingAnd(com.cadenzauk.siesta.grammar.select.InWhereExpectingAnd) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 35 with SalespersonRow

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

the class ExpressionBuilderTest method isNotInWithNoValuesShouldThrow.

@Test
void isNotInWithNoValuesShouldThrow() {
    Database database = testDatabase(new AnsiDialect());
    ExpressionBuilder<String, InWhereExpectingAnd<SalespersonRow>> select = database.from(SalespersonRow.class).where(SalespersonRow::firstName);
    // noinspection Convert2MethodRef
    calling(() -> select.isNotIn(new String[0])).shouldThrow(IllegalArgumentException.class);
}
Also used : InWhereExpectingAnd(com.cadenzauk.siesta.grammar.select.InWhereExpectingAnd) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MockitoTest(com.cadenzauk.core.MockitoTest)

Aggregations

SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)39 Test (org.junit.jupiter.api.Test)31 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)27 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 Database (com.cadenzauk.siesta.Database)12 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)10 MockitoTest (com.cadenzauk.core.MockitoTest)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)6 InWhereExpectingAnd (com.cadenzauk.siesta.grammar.select.InWhereExpectingAnd)4 Tuple3 (com.cadenzauk.core.tuple.Tuple3)3 Scope (com.cadenzauk.siesta.Scope)3 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)2 IncorrectSalesAreaRow (com.cadenzauk.siesta.model.IncorrectSalesAreaRow)2 SalesAreaRow (com.cadenzauk.siesta.model.SalesAreaRow)2 LockingException (com.cadenzauk.core.sql.exception.LockingException)1 Tuple2 (com.cadenzauk.core.tuple.Tuple2)1 H2Dialect (com.cadenzauk.siesta.dialect.H2Dialect)1 Objects (java.util.Objects)1 CompletableFuture (java.util.concurrent.CompletableFuture)1