Search in sources :

Example 26 with SalespersonRow

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

the class DatabaseIntegrationTest method olapWithOrderBy.

@Test
void olapWithOrderBy() {
    assumeTrue(dialect.supportsOrderByInOlap(), dialect.getClass().getSimpleName() + " does not support row_number() with ORDER BY.");
    Database database = testDatabase(dataSource, dialect);
    Tuple2<Long, Long> inserted = insertSalespeople(database, 5);
    List<Tuple3<Long, Integer, Long>> result = database.from(SalespersonRow.class).select(SalespersonRow::salespersonId).comma(Olap.rowNumber().orderBy(SalespersonRow::salespersonId, DESC)).comma(Olap.sum(SalespersonRow::salespersonId).orderBy(SalespersonRow::salespersonId, ASC)).where(SalespersonRow::salespersonId).isBetween(inserted.item1()).and(inserted.item2()).orderBy(SalespersonRow::salespersonId).list();
    assertThat(result, hasSize(5));
    assertThat(result.get(0), is(Tuple.of(inserted.item1(), 5, inserted.item1())));
    assertThat(result.get(4), is(Tuple.of(inserted.item2(), 1, LongStream.range(inserted.item1(), inserted.item2() + 1).reduce((x, y) -> x + y).orElse(0))));
}
Also used : Tuple3(com.cadenzauk.core.tuple.Tuple3) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 27 with SalespersonRow

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

the class DatabaseIntegrationTest method sequence.

@Test
void sequence() {
    Database database = testDatabase(dataSource, dialect);
    Sequence<Integer> widgetSeq = database.sequence("widget_seq");
    long salespersonId = newId();
    int seq1 = widgetSeq.single();
    SalespersonRow salesPerson = SalespersonRow.newBuilder().salespersonId(salespersonId).firstName("Terry").surname("McCann").numberOfSales(seq1).build();
    database.insert(salesPerson);
    database.update(SalespersonRow.class).set(SalespersonRow::numberOfSales).to(widgetSeq.nextVal()).where(SalespersonRow::salespersonId).isEqualTo(literal(salespersonId)).execute();
    int seq2 = database.from(SalespersonRow.class).select(SalespersonRow::numberOfSales).where(SalespersonRow::salespersonId).isEqualTo(literal(salespersonId)).single();
    assertThat(seq2, greaterThan(seq1));
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with SalespersonRow

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

the class DatabaseIntegrationTest method fetchFirst.

@Test
void fetchFirst() {
    Database database = testDatabase(dataSource);
    Tuple2<Long, Long> inserted = insertSalespeople(database, 10);
    List<SalespersonRow> fullList = database.from(SalespersonRow.class).where(SalespersonRow::salespersonId).isBetween(inserted.item1()).and(inserted.item2()).list();
    List<SalespersonRow> reducedList = database.from(SalespersonRow.class).where(SalespersonRow::salespersonId).isBetween(inserted.item1()).and(inserted.item2()).fetchFirst(5).list();
    assertThat(fullList, hasSize(10));
    assertThat(reducedList, hasSize(5));
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with SalespersonRow

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

the class DatabaseIntegrationTest method concatTest.

@Test
void concatTest() {
    Database database = testDatabase(dataSource, dialect);
    SalespersonRow george = SalespersonRow.newBuilder().salespersonId(newId()).firstName("George").surname("Jetson").build();
    database.insert(george);
    Alias<SalespersonRow> s = database.table(SalespersonRow.class).as("s");
    String name = database.from(s).select(upper(s.column(SalespersonRow::firstName).concat(cast(" ").asVarchar(1)).concat(SalespersonRow::surname).concat(cast(1).asInteger()))).where(SalespersonRow::salespersonId).isEqualTo(george.salespersonId()).single();
    assertThat(name, is("GEORGE JETSON1"));
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with SalespersonRow

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

the class ExceptionIntegrationTest method uniqueConstraintViolationOnInsertInTransaction.

@Test
void uniqueConstraintViolationOnInsertInTransaction() {
    Database database = TestDatabase.testDatabase(dataSource);
    SalespersonRow salespersonRow = aRandomSalesperson();
    try (Transaction transaction = database.beginTransaction()) {
        calling(() -> database.insert(transaction, 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)

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