Search in sources :

Example 1 with Statement

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

the class R2dbcTransactionManagerUnitTests method appliesReadOnly.

@Test
void appliesReadOnly() {
    when(connectionMock.commitTransaction()).thenReturn(Mono.empty());
    when(connectionMock.setTransactionIsolationLevel(any())).thenReturn(Mono.empty());
    Statement statement = mock(Statement.class);
    when(connectionMock.createStatement(anyString())).thenReturn(statement);
    when(statement.execute()).thenReturn(Mono.empty());
    tm.setEnforceReadOnly(true);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setReadOnly(true);
    TransactionalOperator operator = TransactionalOperator.create(tm, definition);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verify(connectionMock).isAutoCommit();
    verify(connectionMock).beginTransaction();
    verify(connectionMock).createStatement("SET TRANSACTION READ ONLY");
    verify(connectionMock).commitTransaction();
    verify(connectionMock).close();
    verifyNoMoreInteractions(connectionMock);
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Statement(io.r2dbc.spi.Statement) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 2 with Statement

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

the class DefaultDatabaseClientUnitTests method shouldApplyStatementFilterFunctions.

@Test
void shouldApplyStatementFilterFunctions() {
    MockResult result = MockResult.builder().build();
    Statement statement = mockStatement(result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT").filter((s, next) -> next.execute(s.returnGeneratedValues("foo"))).filter((s, next) -> next.execute(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 3 with Statement

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

the class DefaultDatabaseClientUnitTests method executeShouldBindNamedNullValues.

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

Example 4 with Statement

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

the class DefaultDatabaseClientUnitTests method executeShouldBindSettableValues.

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

Example 5 with Statement

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

the class DefaultDatabaseClientUnitTests method executeShouldBindValues.

@Test
void executeShouldBindValues() {
    Statement statement = mockStatementFor("SELECT * FROM table WHERE key = $1");
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT * FROM table WHERE key = $1").bind(0, Parameter.from("foo")).then().as(StepVerifier::create).verifyComplete();
    verify(statement).bind(0, "foo");
    databaseClient.sql("SELECT * FROM table WHERE key = $1").bind("$1", "foo").then().as(StepVerifier::create).verifyComplete();
    verify(statement).bind("$1", "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