Search in sources :

Example 51 with Database

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

Example 52 with Database

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

the class ExpressionBuilderTest method isInWithNoValuesShouldThrow.

@Test
void isInWithNoValuesShouldThrow() {
    Database database = testDatabase(new AnsiDialect());
    ExpressionBuilder<String, InWhereExpectingAnd<SalespersonRow>> select = database.from(SalespersonRow.class).where(SalespersonRow::firstName);
    // noinspection Convert2MethodRef
    calling(() -> select.isIn(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)

Example 53 with Database

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

the class TypedExpressionTest method columnAncCast.

@ParameterizedTest(name = "{1}")
@MethodSource({ "parametersForColumn", "parametersForCast" })
void columnAncCast(Function<Alias<TestTable>, ExpressionBuilder<Integer, BooleanExpression>> method, String expectedSql, Object[] expectedArgs) {
    Database database = Database.newBuilder().build();
    Alias<TestTable> alias = database.table(TestTable.class).as("t");
    Scope scope = new Scope(database, alias);
    ExpressionBuilder<Integer, BooleanExpression> result = method.apply(alias);
    assertThat(result.sql(scope), is(expectedSql));
    assertThat(result.args(scope).toArray(), is(expectedArgs));
}
Also used : Scope(com.cadenzauk.siesta.Scope) Database(com.cadenzauk.siesta.Database) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 54 with Database

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

the class TypedExpressionTest method arithmeticOperators.

@ParameterizedTest(name = "{1}")
@MethodSource("parametersForArithmeticOperators")
void arithmeticOperators(BiFunction<TypedExpression<Integer>, Alias<TestTable>, TypedExpression<Integer>> method, String expectedSql, Object[] expectedArgs) {
    Database database = Database.newBuilder().build();
    Alias<TestTable> alias = database.table(TestTable.class).as("t");
    Scope scope = new Scope(database, alias);
    when(sut.precedence()).thenReturn(Precedence.COLUMN);
    when(sut.sql(scope)).thenReturn("lhs");
    when(sut.args(scope)).thenReturn(Stream.of(1, 2));
    TypedExpression<Integer> result = method.apply(sut, alias);
    assertThat(result.sql(scope), is("lhs" + expectedSql));
    assertThat(result.args(scope).toArray(), is(ArrayUtils.addAll(toArray(1, 2), expectedArgs)));
}
Also used : Scope(com.cadenzauk.siesta.Scope) Database(com.cadenzauk.siesta.Database) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 55 with Database

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

the class ExpectingUnionTest method union.

@Test
void union() {
    Database database = testDatabase(new AnsiDialect());
    database.select(TypedExpression.value(1), "one").union(database.select(TypedExpression.literal(2), "two")).unionAll(database.select(TypedExpression.value(3), "three")).orderBy(1).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select ? as one from DUAL union select 2 as two from DUAL union all select ? as three from DUAL order by 1 asc"));
    assertThat(args.getValue(), is(toArray(1, 3)));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

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