Search in sources :

Example 11 with SalespersonRow

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

the class ExceptionIntegrationTest method deadlockOnUpdate.

@SuppressWarnings("ThrowableNotThrown")
@Test
void deadlockOnUpdate() {
    Database database = TestDatabase.testDatabase(dataSource);
    SalespersonRow salespersonRow1 = aRandomSalesperson();
    SalespersonRow salespersonRow2 = aRandomSalesperson();
    database.insert(salespersonRow1, salespersonRow2);
    CompletableFuture<Void> wait11 = new CompletableFuture<>();
    CompletableFuture<Void> wait12 = new CompletableFuture<>();
    CompletableFuture<Void> wait2 = new CompletableFuture<>();
    CompletableFuture<Void> wait3 = new CompletableFuture<>();
    CompletableFuture<Void> wait4 = new CompletableFuture<>();
    CompletableFuture<Throwable> future1 = performUpdate(database, salespersonRow1, salespersonRow2, wait11, wait2, wait3, wait4);
    CompletableFuture<Throwable> future2 = performUpdate(database, salespersonRow2, salespersonRow1, wait12, wait2, wait3, wait4);
    CompletableFuture.allOf(wait11, wait12).join();
    wait2.complete(null);
    wait3.join();
    wait4.complete(null);
    CompletableFuture.allOf(future1, future2).join();
    Throwable exception = Stream.of(future1, future2).map(CompletableFuture::join).filter(Objects::nonNull).findFirst().orElseThrow(() -> new AssertionError("No exception was thrown for deadlock."));
    assertThat(exception, instanceOf(LockingException.class));
}
Also used : LockingException(com.cadenzauk.core.sql.exception.LockingException) CompletableFuture(java.util.concurrent.CompletableFuture) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) Objects(java.util.Objects) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with SalespersonRow

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

the class ExceptionIntegrationTest method uniqueConstraintViolationOnUpdate.

@Test
void uniqueConstraintViolationOnUpdate() {
    Database database = TestDatabase.testDatabase(dataSource);
    SalespersonRow salespersonRow1 = aRandomSalesperson();
    SalespersonRow salespersonRow2 = aRandomSalesperson();
    database.insert(salespersonRow1, salespersonRow2);
    calling(() -> database.update(SalespersonRow.class).set(SalespersonRow::salespersonId).to(salespersonRow1.salespersonId()).where(SalespersonRow::salespersonId).isEqualTo(salespersonRow2.salespersonId()).execute()).shouldThrow(RuntimeSqlException.class).with(subclass(DuplicateKeyException.class)).withMessage(startsWith("Unique constraint violated while executing 'update "));
}
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 13 with SalespersonRow

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

the class ExceptionIntegrationTest method foreignKeyViolationOnDelete.

@Test
void foreignKeyViolationOnDelete() {
    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.delete(SalespersonRow.class).where(SalespersonRow::salespersonId).isEqualTo(salespersonRow.salespersonId()).execute()).shouldThrow(RuntimeSqlException.class).with(subclass(ReferentialIntegrityException.class)).withMessage(startsWith("Foreign key constraint violated while executing 'delete "));
}
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 14 with SalespersonRow

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

the class OlapFunctionTest method addPartitionByAndParitionBy.

@ParameterizedTest(name = "{index}: {1}")
@MethodSource("parametersForRowNumber")
void addPartitionByAndParitionBy(BiFunction<InOlapExpectingPartitionBy<Integer>, Alias<SalespersonRow>, TypedExpression<Integer>> f, String expected, Object[] expectedArgs) {
    Database database = testDatabase(new AnsiDialect());
    Alias<SalespersonRow> s = database.table(SalespersonRow.class).as("s");
    database.from(s).select(f.apply(Olap.rowNumber(), s)).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), any());
    assertThat(sql.getValue(), is("select row_number() over (" + expected + ") as row_number_1 " + "from SIESTA.SALESPERSON s"));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : 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 15 with SalespersonRow

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

the class OlapTest method olapFunction.

@ParameterizedTest(name = "{index}: {1}")
@MethodSource("parametersForOlapTest")
void olapFunction(Function<Alias<SalespersonRow>, InOlapExpectingPartitionBy<?>> f, String expectedSql) {
    Database database = testDatabase(new AnsiDialect());
    Alias<SalespersonRow> s = database.table(SalespersonRow.class).as("s");
    Scope scope = new Scope(database, s);
    InOlapExpectingPartitionBy<?> result = f.apply(s);
    assertThat(result.sql(scope), is(expectedSql));
    assertThat(result.precedence(), is(Precedence.UNARY));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) Scope(com.cadenzauk.siesta.Scope) 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)

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