Search in sources :

Example 16 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class UpdateTest method update.

@Test
void update() {
    Database database = Database.newBuilder().defaultSchema("SIESTA").table(WidgetRow.class, t -> t.builder(WidgetRow.Builder::build)).build();
    database.update(WidgetRow.class, "w").set(WidgetRow::name).to("Fred").set(WidgetRow::description).to(Optional.of("Bob")).where(WidgetRow::widgetId).isEqualTo(1L).and(column(WidgetRow::description).isBetween("A").and("w", WidgetRow::name).or(column(WidgetRow::description).isNull())).execute(transaction);
    verify(transaction).update(sql.capture(), args.capture());
    assertThat(sql.getValue(), is("update SIESTA.WIDGET w " + "set NAME = ?, " + "DESCRIPTION = ? " + "where w.WIDGET_ID = ? " + "and (" + "w.DESCRIPTION between ? and w.NAME " + "or w.DESCRIPTION is null" + ")"));
    assertThat(args.getValue(), is(toArray("Fred", "Bob", 1L, "A")));
}
Also used : Mock(org.mockito.Mock) Captor(org.mockito.Captor) TypedExpression.column(com.cadenzauk.siesta.grammar.expression.TypedExpression.column) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ArgumentCaptor(org.mockito.ArgumentCaptor) ArrayUtils.toArray(org.apache.commons.lang3.ArrayUtils.toArray) MockitoTest(com.cadenzauk.core.MockitoTest) Transaction(com.cadenzauk.siesta.Transaction) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Database(com.cadenzauk.siesta.Database) WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 17 with Database

use of com.cadenzauk.siesta.Database 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 18 with Database

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

Example 19 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class ExpectingJoinTest method testJoin.

@ParameterizedTest
@MethodSource("argsForJoin")
void testJoin(BiFunction<ExpectingJoin1<SalespersonRow>, Alias<SalespersonRow>[], ExpectingSelect<?>> method, String expectedSql) {
    MockitoAnnotations.initMocks(this);
    Database database = testDatabase(new AnsiDialect());
    @SuppressWarnings("unchecked") Alias<SalespersonRow>[] alias = toArray(database.table(SalespersonRow.class).as("s1"), database.table(SalespersonRow.class).as("s2"), database.table(SalespersonRow.class).as("s3"), database.table(SalespersonRow.class).as("s4"), database.table(SalespersonRow.class).as("s5"), database.table(SalespersonRow.class).as("s6"), database.table(SalespersonRow.class).as("s7"), database.table(SalespersonRow.class).as("s8"), database.table(SalespersonRow.class).as("s9"));
    method.apply(database.from(alias[0]), alias).select(alias[0], SalespersonRow::firstName, "name").list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select s1.FIRST_NAME as name from SIESTA.SALESPERSON s1 " + expectedSql));
    assertThat(args.getValue(), arrayWithSize(0));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) Alias(com.cadenzauk.siesta.Alias) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class ExpectingSelectTest method testSelect.

@ParameterizedTest
@MethodSource("argsForSelect")
void testSelect(BiFunction<ExpectingSelect<SalespersonRow>, Alias<SalespersonRow>, Select<?>> method, String expectedSql) {
    MockitoAnnotations.initMocks(this);
    Database database = testDatabase(new AnsiDialect());
    Alias<SalespersonRow> alias = database.table(SalespersonRow.class).as("s");
    method.apply(database.from(alias), alias).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select " + expectedSql + " from SIESTA.SALESPERSON s"));
    assertThat(args.getValue(), arrayWithSize(0));
}
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)

Aggregations

Database (com.cadenzauk.siesta.Database)57 Test (org.junit.jupiter.api.Test)41 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)21 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 ZonedDateTime (java.time.ZonedDateTime)17 Optional (java.util.Optional)17 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)16 Alias (com.cadenzauk.siesta.Alias)15 BigDecimal (java.math.BigDecimal)14 Collectors.joining (java.util.stream.Collectors.joining)14 AttributeOverride (javax.persistence.AttributeOverride)14 Embedded (javax.persistence.Embedded)14 EmbeddedId (javax.persistence.EmbeddedId)14 Id (javax.persistence.Id)14 CoreMatchers.is (org.hamcrest.CoreMatchers.is)14 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)13 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)7 MockitoTest (com.cadenzauk.core.MockitoTest)5