#86 - Add non-nullable variant to RowsFetchSpec extensions.

This commit is a follow-up of gh-63.

Original pull request: #86.
This commit is contained in:
Sebastien Deleuze
2019-04-03 08:58:29 +02:00
committed by Mark Paluch
parent 7c9e777d00
commit 43a74cae31
2 changed files with 116 additions and 7 deletions

View File

@@ -16,22 +16,39 @@
package org.springframework.data.r2dbc.function
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactive.awaitSingle
/**
* Coroutines variant of [RowsFetchSpec.one].
* Non-nullable Coroutines variant of [RowsFetchSpec.one].
*
* @author Sebastien Deleuze
*/
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T?
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T
= one().awaitSingle()
/**
* Nullable Coroutines variant of [RowsFetchSpec.one].
*
* @author Sebastien Deleuze
*/
suspend fun <T> RowsFetchSpec<T>.awaitOneOrNull(): T?
= one().awaitFirstOrNull()
/**
* Coroutines variant of [RowsFetchSpec.first].
* Non-nullable Coroutines variant of [RowsFetchSpec.first].
*
* @author Sebastien Deleuze
*/
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T?
= first().awaitFirstOrNull()
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T
= first().awaitSingle()
/**
* Nullable Coroutines variant of [RowsFetchSpec.first].
*
* @author Sebastien Deleuze
*/
suspend fun <T> RowsFetchSpec<T>.awaitFirstOrNull(): T?
= first().awaitFirstOrNull()
// TODO Coroutines variant of [RowsFetchSpec.all], depends on [kotlinx.coroutines#254](https://github.com/Kotlin/kotlinx.coroutines/issues/254).
// suspend fun <T> RowsFetchSpec<T>.awaitAll() = all()...

View File

@@ -20,8 +20,10 @@ import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.Test
import reactor.core.publisher.Mono
import java.lang.NullPointerException
/**
* Unit tests for [RowsFetchSpec] extensions.
@@ -31,7 +33,7 @@ import reactor.core.publisher.Mono
class RowsFetchSpecExtensionsTests {
@Test // gh-63
fun awaitOne() {
fun awaitOneWithValue() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.one() } returns Mono.just("foo")
@@ -46,7 +48,52 @@ class RowsFetchSpecExtensionsTests {
}
@Test // gh-63
fun awaitFirst() {
fun awaitOneWithNull() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.one() } returns Mono.empty()
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
runBlocking { spec.awaitOne() }
}
verify {
spec.one()
}
}
@Test // gh-63
fun awaitOneOrNullWithValue() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.one() } returns Mono.just("foo")
runBlocking {
assertThat(spec.awaitOneOrNull()).isEqualTo("foo")
}
verify {
spec.one()
}
}
@Test // gh-63
fun awaitOneOrNullWithNull() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.one() } returns Mono.empty()
runBlocking {
assertThat(spec.awaitOneOrNull()).isNull()
}
verify {
spec.one()
}
}
@Test // gh-63
fun awaitFirstWithValue() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.first() } returns Mono.just("foo")
@@ -59,4 +106,49 @@ class RowsFetchSpecExtensionsTests {
spec.first()
}
}
@Test // gh-63
fun awaitFirstWithNull() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.first() } returns Mono.empty()
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
runBlocking { spec.awaitFirst() }
}
verify {
spec.first()
}
}
@Test // gh-63
fun awaitFirstOrNullWithValue() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.first() } returns Mono.just("foo")
runBlocking {
assertThat(spec.awaitFirstOrNull()).isEqualTo("foo")
}
verify {
spec.first()
}
}
@Test // gh-63
fun awaitFirstOrNullWithNull() {
val spec = mockk<RowsFetchSpec<String>>()
every { spec.first() } returns Mono.empty()
runBlocking {
assertThat(spec.awaitFirstOrNull()).isNull()
}
verify {
spec.first()
}
}
}