Search in sources :

Example 6 with Result

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

the class DefaultDatabaseClientUnitTests method selectOneShouldFailWithException.

@Test
void selectOneShouldFailWithException() {
    MockRowMetadata metadata = MockRowMetadata.builder().columnMetadata(MockColumnMetadata.builder().name("name").javaType(String.class).build()).build();
    MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Object.class, "Walter").metadata(metadata).build(), MockRow.builder().identified(0, Object.class, "White").metadata(metadata).build()).build();
    mockStatementFor("SELECT * FROM person", result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT * FROM person").map(row -> row.get(0)).one().as(StepVerifier::create).verifyError(IncorrectResultSizeDataAccessException.class);
}
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) MockRowMetadata(io.r2dbc.spi.test.MockRowMetadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 7 with Result

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

the class DefaultDatabaseClientUnitTests method rowsUpdatedShouldEmitSingleValue.

@Test
@SuppressWarnings("unchecked")
void rowsUpdatedShouldEmitSingleValue() {
    Result result = mock(Result.class);
    when(result.getRowsUpdated()).thenReturn(Mono.empty(), Mono.just(2), Flux.just(1, 2, 3));
    mockStatementFor("DROP TABLE tab;", result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("DROP TABLE tab;").fetch().rowsUpdated().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    databaseClient.sql("DROP TABLE tab;").fetch().rowsUpdated().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    databaseClient.sql("DROP TABLE tab;").fetch().rowsUpdated().as(StepVerifier::create).expectNextCount(1).verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) Result(io.r2dbc.spi.Result) MockResult(io.r2dbc.spi.test.MockResult) Test(org.junit.jupiter.api.Test)

Example 8 with Result

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

the class DefaultDatabaseClientUnitTests method shouldApplyExecuteFunction.

@Test
void shouldApplyExecuteFunction() {
    Statement statement = mockStatement();
    MockResult result = mockSingleColumnResult(MockRow.builder().identified(0, Object.class, "Walter"));
    DatabaseClient databaseClient = databaseClientBuilder.executeFunction(stmnt -> Mono.just(result)).build();
    databaseClient.sql("SELECT").fetch().all().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verifyNoInteractions(statement);
}
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) Statement(io.r2dbc.spi.Statement) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 9 with Result

use of io.r2dbc.spi.Result in project jOOQ by jOOQ.

the class R2DBC method block.

@SuppressWarnings("unchecked")
static final <T> T block(Publisher<? extends T> publisher) {
    Object complete = new Object();
    LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>();
    publisher.subscribe(subscriber(s -> s.request(1), queue::add, queue::add, () -> queue.add(complete)));
    try {
        Object result = queue.take();
        if (result instanceof Throwable)
            throw new DataAccessException("Exception when blocking on publisher", (Throwable) result);
        else if (result == complete)
            return null;
        else
            return (T) result;
    } catch (InterruptedException e) {
        throw new DataAccessException("Exception when blocking on publisher", e);
    }
}
Also used : Time(java.sql.Time) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) BiFunction(java.util.function.BiFunction) Batch(io.r2dbc.spi.Batch) ByteBuffer(java.nio.ByteBuffer) Array(java.sql.Array) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultResultSet(org.jooq.tools.jdbc.DefaultResultSet) LocalTime(java.time.LocalTime) SQLType(java.sql.SQLType) Result(io.r2dbc.spi.Result) DataTypeException(org.jooq.exception.DataTypeException) SQLDialect(org.jooq.SQLDialect) DataType(org.jooq.DataType) JooqLogger(org.jooq.tools.JooqLogger) JDBCUtils.safeClose(org.jooq.tools.jdbc.JDBCUtils.safeClose) Timestamp(java.sql.Timestamp) Builder(io.r2dbc.spi.ConnectionFactoryOptions.Builder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Field(org.jooq.Field) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cursor(org.jooq.Cursor) Option(io.r2dbc.spi.Option) Connection(io.r2dbc.spi.Connection) LocalDate(java.time.LocalDate) StringUtils.defaultIfNull(org.jooq.tools.StringUtils.defaultIfNull) Query(org.jooq.Query) Internal.subscriber(org.jooq.impl.Internal.subscriber) Guard(org.jooq.impl.ThreadGuard.Guard) ResultSetMetaData(java.sql.ResultSetMetaData) Types(java.sql.Types) DefaultPreparedStatement(org.jooq.tools.jdbc.DefaultPreparedStatement) ConnectionFactories(io.r2dbc.spi.ConnectionFactories) Row(io.r2dbc.spi.Row) XML(org.jooq.XML) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) Tools.visitAll(org.jooq.impl.Tools.visitAll) LocalDateTime(java.time.LocalDateTime) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tools.recordFactory(org.jooq.impl.Tools.recordFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) BindingGetResultSetContext(org.jooq.BindingGetResultSetContext) ConcurrentMap(java.util.concurrent.ConcurrentMap) RowMetadata(io.r2dbc.spi.RowMetadata) SQLException(java.sql.SQLException) ColumnMetadata(io.r2dbc.spi.ColumnMetadata) EMPTY_PARAM(org.jooq.impl.Tools.EMPTY_PARAM) Tools.abstractDMLQuery(org.jooq.impl.Tools.abstractDMLQuery) Subscriber(org.reactivestreams.Subscriber) MockArray(org.jooq.tools.jdbc.MockArray) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException) Properties(java.util.Properties) Tools.translate(org.jooq.impl.Tools.translate) SettingsTools(org.jooq.conf.SettingsTools) Publisher(org.reactivestreams.Publisher) JSON(org.jooq.JSON) Settings(org.jooq.conf.Settings) Tools.abstractResultQuery(org.jooq.impl.Tools.abstractResultQuery) Converter(org.jooq.Converter) Date(java.sql.Date) Consumer(java.util.function.Consumer) Param(org.jooq.Param) AtomicLong(java.util.concurrent.atomic.AtomicLong) Configuration(org.jooq.Configuration) NAMED(org.jooq.conf.ParamType.NAMED) Tools.fields(org.jooq.impl.Tools.fields) Rendered(org.jooq.impl.DefaultRenderContext.Rendered) Subscription(org.reactivestreams.Subscription) JSONB(org.jooq.JSONB) Statement(io.r2dbc.spi.Statement) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) DataAccessException(org.jooq.exception.DataAccessException)

Aggregations

Result (io.r2dbc.spi.Result)9 Connection (io.r2dbc.spi.Connection)7 Test (org.junit.jupiter.api.Test)7 StepVerifier (reactor.test.StepVerifier)7 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)6 Statement (io.r2dbc.spi.Statement)6 MockResult (io.r2dbc.spi.test.MockResult)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Publisher (org.reactivestreams.Publisher)6 Subscription (org.reactivestreams.Subscription)6 Flux (reactor.core.publisher.Flux)6 Mono (reactor.core.publisher.Mono)6 MockColumnMetadata (io.r2dbc.spi.test.MockColumnMetadata)5 MockRow (io.r2dbc.spi.test.MockRow)5 MockRowMetadata (io.r2dbc.spi.test.MockRowMetadata)5 Arrays (java.util.Arrays)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)5