Search in sources :

Example 21 with SalespersonRow

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

the class ExpressionBuilderTest method testExpression.

@ParameterizedTest
@MethodSource("argsForTestExpression")
void testExpression(BiFunction<ExpressionBuilder<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");
    ExpressionBuilder<String, InWhereExpectingAnd<String>> select = database.from(alias).select(SalespersonRow::firstName, "name").where(SalespersonRow::firstName);
    method.apply(select, alias).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), any());
    assertThat(sql.getValue(), is("select s.FIRST_NAME as name from SIESTA.SALESPERSON s where s.FIRST_NAME " + 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 22 with SalespersonRow

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

the class TransactionIntegrationTest method whenCommittedThenInserted.

@Test
void whenCommittedThenInserted() {
    Database database = testDatabase(dataSource, dialect);
    SalespersonRow salesperson = aRandomSalesperson();
    try (Transaction transaction = database.beginTransaction()) {
        database.insert(transaction, salesperson);
        transaction.commit();
    }
    Integer count = database.from(SalespersonRow.class).select(count()).where(SalespersonRow::salespersonId).isEqualTo(salesperson.salespersonId()).single();
    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 23 with SalespersonRow

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

the class TransactionIntegrationTest method whenUncommittedThenVisibleWithUr.

@Test
void whenUncommittedThenVisibleWithUr() {
    assumeTrue(dialect.supportsIsolationLevelInQuery(), "Database doesn't support isolation levels in query.");
    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).single();
        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 24 with SalespersonRow

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

the class TransactionIntegrationTest method whenUncommittedThenNotVisible.

@Test
void whenUncommittedThenNotVisible() {
    Database database = testDatabase(dataSource, dialect);
    SalespersonRow salesperson = aRandomSalesperson();
    try (Transaction transaction = database.beginTransaction()) {
        database.insert(transaction, salesperson);
        Optional<Long> result = database.from(SalespersonRow.class).select(SalespersonRow::salespersonId).where(SalespersonRow::salespersonId).isEqualTo(salesperson.salespersonId()).withIsolation(IsolationLevel.REPEATABLE_READ).keepLocks(LockLevel.UPDATE).optional();
        assertThat(result.isPresent(), is(false));
    }
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test)

Example 25 with SalespersonRow

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

the class TransactionIntegrationTest method whenRollingBackNothingInserted.

@Test
void whenRollingBackNothingInserted() {
    Database database = testDatabase(dataSource, dialect);
    SalespersonRow salesperson = aRandomSalesperson();
    try (Transaction transaction = database.beginTransaction()) {
        database.insert(transaction, salesperson);
        transaction.rollback();
    }
    Integer count = database.from(SalespersonRow.class).select(count()).where(SalespersonRow::salespersonId).isEqualTo(salesperson.salespersonId()).single();
    assertThat(count, is(0));
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test)

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