Search in sources :

Example 6 with Statement

use of io.r2dbc.spi.Statement in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method shouldApplySimpleStatementFilterFunctions.

@Test
void shouldApplySimpleStatementFilterFunctions() {
    MockResult result = mockSingleColumnEmptyResult();
    Statement statement = mockStatement(result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT").filter(s -> s.returnGeneratedValues("foo")).filter(s -> s.returnGeneratedValues("bar")).fetch().all().as(StepVerifier::create).verifyComplete();
    InOrder inOrder = inOrder(statement);
    inOrder.verify(statement).returnGeneratedValues("foo");
    inOrder.verify(statement).returnGeneratedValues("bar");
    inOrder.verify(statement).execute();
    inOrder.verifyNoMoreInteractions();
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BDDMockito.inOrder(org.mockito.BDDMockito.inOrder) BDDMockito.times(org.mockito.BDDMockito.times) BDDMockito.when(org.mockito.BDDMockito.when) CoreSubscriber(reactor.core.CoreSubscriber) BDDMockito.verify(org.mockito.BDDMockito.verify) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BindMarkersFactory(org.springframework.r2dbc.core.binding.BindMarkersFactory) Result(io.r2dbc.spi.Result) Nullable(org.springframework.lang.Nullable) BDDMockito.doReturn(org.mockito.BDDMockito.doReturn) BDDMockito.verifyNoInteractions(org.mockito.BDDMockito.verifyNoInteractions) MockRow(io.r2dbc.spi.test.MockRow) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) MockResult(io.r2dbc.spi.test.MockResult) MockRowMetadata(io.r2dbc.spi.test.MockRowMetadata) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) InOrder(org.mockito.InOrder) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) BDDMockito.verifyNoMoreInteractions(org.mockito.BDDMockito.verifyNoMoreInteractions) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) Subscription(org.reactivestreams.Subscription) MockColumnMetadata(io.r2dbc.spi.test.MockColumnMetadata) BDDMockito.mock(org.mockito.BDDMockito.mock) Statement(io.r2dbc.spi.Statement) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MockResult(io.r2dbc.spi.test.MockResult) InOrder(org.mockito.InOrder) Statement(io.r2dbc.spi.Statement) Test(org.junit.jupiter.api.Test)

Example 7 with Statement

use of io.r2dbc.spi.Statement in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method executeShouldBindNamedValuesFromIndexes.

@Test
void executeShouldBindNamedValuesFromIndexes() {
    Statement statement = mockStatementFor("SELECT id, name, manual FROM legoset WHERE name IN ($1, $2, $3)");
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT id, name, manual FROM legoset WHERE name IN (:name)").bind(0, Arrays.asList("unknown", "dunno", "other")).then().as(StepVerifier::create).verifyComplete();
    verify(statement).bind(0, "unknown");
    verify(statement).bind(1, "dunno");
    verify(statement).bind(2, "other");
    verify(statement).execute();
    verifyNoMoreInteractions(statement);
}
Also used : Statement(io.r2dbc.spi.Statement) Test(org.junit.jupiter.api.Test)

Example 8 with Statement

use of io.r2dbc.spi.Statement in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method shouldApplyPreparedOperation.

@Test
void shouldApplyPreparedOperation() {
    MockResult result = mockSingleColumnResult(MockRow.builder().identified(0, Object.class, "Walter"));
    Statement statement = mockStatementFor("SELECT * FROM person", result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql(new PreparedOperation<String>() {

        @Override
        public String toQuery() {
            return "SELECT * FROM person";
        }

        @Override
        public String getSource() {
            return "SELECT";
        }

        @Override
        public void bindTo(BindTarget target) {
            target.bind("index", "value");
        }
    }).fetch().all().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verify(statement).bind("index", "value");
}
Also used : MockResult(io.r2dbc.spi.test.MockResult) Statement(io.r2dbc.spi.Statement) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 9 with Statement

use of io.r2dbc.spi.Statement in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method mockStatementFor.

private Statement mockStatementFor(@Nullable String sql, @Nullable Result result) {
    Statement statement = mock(Statement.class);
    when(connection.createStatement(sql == null ? anyString() : eq(sql))).thenReturn(statement);
    when(statement.returnGeneratedValues(anyString())).thenReturn(statement);
    when(statement.returnGeneratedValues()).thenReturn(statement);
    doReturn(result == null ? Mono.empty() : Flux.just(result)).when(statement).execute();
    return statement;
}
Also used : Statement(io.r2dbc.spi.Statement)

Example 10 with Statement

use of io.r2dbc.spi.Statement in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method executeShouldBindNamedValuesByIndex.

@Test
void executeShouldBindNamedValuesByIndex() {
    Statement statement = mockStatementFor("SELECT * FROM table WHERE key = $1");
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT * FROM table WHERE key = :key").bind("key", "foo").then().as(StepVerifier::create).verifyComplete();
    verify(statement).bind(0, "foo");
}
Also used : Statement(io.r2dbc.spi.Statement) Test(org.junit.jupiter.api.Test)

Aggregations

Statement (io.r2dbc.spi.Statement)12 Test (org.junit.jupiter.api.Test)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 StepVerifier (reactor.test.StepVerifier)5 MockResult (io.r2dbc.spi.test.MockResult)4 BindTarget (org.springframework.r2dbc.core.binding.BindTarget)4 Connection (io.r2dbc.spi.Connection)3 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)3 Result (io.r2dbc.spi.Result)3 MockColumnMetadata (io.r2dbc.spi.test.MockColumnMetadata)3 MockRow (io.r2dbc.spi.test.MockRow)3 MockRowMetadata (io.r2dbc.spi.test.MockRowMetadata)3 Arrays (java.util.Arrays)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)3 BDDMockito.doReturn (org.mockito.BDDMockito.doReturn)3 BDDMockito.inOrder (org.mockito.BDDMockito.inOrder)3 BDDMockito.mock (org.mockito.BDDMockito.mock)3