Skip to content

Add non-nullable variant to RowsFetchSpec extensions #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import kotlinx.coroutines.reactive.awaitFirstOrNull
* @author Sebastien Deleuze
*/
suspend fun DatabaseClient.GenericExecuteSpec.await() {
then().awaitFirstOrNull()
then().awaitFirstOrNull()
}

/**
Expand All @@ -32,25 +32,25 @@ suspend fun DatabaseClient.GenericExecuteSpec.await() {
*
* @author Sebastien Deleuze
*/
inline fun <reified T : Any> DatabaseClient.GenericExecuteSpec.asType(): DatabaseClient.TypedExecuteSpec<T>
= `as`(T::class.java)
inline fun <reified T : Any> DatabaseClient.GenericExecuteSpec.asType(): DatabaseClient.TypedExecuteSpec<T> =
`as`(T::class.java)

/**
* Extension for [DatabaseClient.GenericSelectSpec.as] providing a
* `asType<Foo>()` variant.
*
* @author Sebastien Deleuze
*/
inline fun <reified T : Any> DatabaseClient.GenericSelectSpec.asType(): DatabaseClient.TypedSelectSpec<T>
= `as`(T::class.java)
inline fun <reified T : Any> DatabaseClient.GenericSelectSpec.asType(): DatabaseClient.TypedSelectSpec<T> =
`as`(T::class.java)

/**
* Coroutines variant of [DatabaseClient.TypedExecuteSpec.then].
*
* @author Sebastien Deleuze
*/
suspend fun <T> DatabaseClient.TypedExecuteSpec<T>.await() {
then().awaitFirstOrNull()
then().awaitFirstOrNull()
}

/**
Expand All @@ -59,16 +59,16 @@ suspend fun <T> DatabaseClient.TypedExecuteSpec<T>.await() {
*
* @author Sebastien Deleuze
*/
inline fun <reified T : Any> DatabaseClient.TypedExecuteSpec<T>.asType(): DatabaseClient.TypedExecuteSpec<T>
= `as`(T::class.java)
inline fun <reified T : Any> DatabaseClient.TypedExecuteSpec<T>.asType(): DatabaseClient.TypedExecuteSpec<T> =
`as`(T::class.java)

/**
* Coroutines variant of [DatabaseClient.InsertSpec.then].
*
* @author Sebastien Deleuze
*/
suspend fun <T> DatabaseClient.InsertSpec<T>.await() {
then().awaitFirstOrNull()
then().awaitFirstOrNull()
}

/**
Expand All @@ -77,6 +77,6 @@ suspend fun <T> DatabaseClient.InsertSpec<T>.await() {
*
* @author Sebastien Deleuze
*/
inline fun <reified T : Any> DatabaseClient.InsertIntoSpec.into(): DatabaseClient.TypedInsertSpec<T>
= into(T::class.java)
inline fun <reified T : Any> DatabaseClient.InsertIntoSpec.into(): DatabaseClient.TypedInsertSpec<T> =
into(T::class.java)

Original file line number Diff line number Diff line change
Expand Up @@ -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?
= one().awaitFirstOrNull()
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T =
one().awaitSingle()

/**
* Coroutines variant of [RowsFetchSpec.first].
* Nullable Coroutines variant of [RowsFetchSpec.one].
*
* @author Sebastien Deleuze
*/
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T?
= first().awaitFirstOrNull()
suspend fun <T> RowsFetchSpec<T>.awaitOneOrNull(): T? =
one().awaitFirstOrNull()

/**
* Non-nullable Coroutines variant of [RowsFetchSpec.first].
*
* @author Sebastien Deleuze
*/
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()...
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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()
}
}
}