Skip to content

Commit 43a74ca

Browse files
sdeleuzemp911de
authored andcommitted
#86 - Add non-nullable variant to RowsFetchSpec extensions.
This commit is a follow-up of gh-63. Original pull request: #86.
1 parent 7c9e777 commit 43a74ca

File tree

2 files changed

+116
-7
lines changed

2 files changed

+116
-7
lines changed

src/main/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensions.kt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,39 @@
1616
package org.springframework.data.r2dbc.function
1717

1818
import kotlinx.coroutines.reactive.awaitFirstOrNull
19+
import kotlinx.coroutines.reactive.awaitSingle
1920

2021
/**
21-
* Coroutines variant of [RowsFetchSpec.one].
22+
* Non-nullable Coroutines variant of [RowsFetchSpec.one].
2223
*
2324
* @author Sebastien Deleuze
2425
*/
25-
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T?
26+
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T
27+
= one().awaitSingle()
28+
29+
/**
30+
* Nullable Coroutines variant of [RowsFetchSpec.one].
31+
*
32+
* @author Sebastien Deleuze
33+
*/
34+
suspend fun <T> RowsFetchSpec<T>.awaitOneOrNull(): T?
2635
= one().awaitFirstOrNull()
2736

2837
/**
29-
* Coroutines variant of [RowsFetchSpec.first].
38+
* Non-nullable Coroutines variant of [RowsFetchSpec.first].
39+
*
40+
* @author Sebastien Deleuze
41+
*/
42+
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T
43+
= first().awaitSingle()
44+
45+
/**
46+
* Nullable Coroutines variant of [RowsFetchSpec.first].
3047
*
3148
* @author Sebastien Deleuze
3249
*/
33-
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T?
34-
= first().awaitFirstOrNull()
50+
suspend fun <T> RowsFetchSpec<T>.awaitFirstOrNull(): T?
51+
= first().awaitFirstOrNull()
3552

3653
// TODO Coroutines variant of [RowsFetchSpec.all], depends on [kotlinx.coroutines#254](https://github.com/Kotlin/kotlinx.coroutines/issues/254).
3754
// suspend fun <T> RowsFetchSpec<T>.awaitAll() = all()...

src/test/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensionsTests.kt

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import io.mockk.mockk
2020
import io.mockk.verify
2121
import kotlinx.coroutines.runBlocking
2222
import org.assertj.core.api.Assertions.assertThat
23+
import org.assertj.core.api.Assertions.assertThatExceptionOfType
2324
import org.junit.Test
2425
import reactor.core.publisher.Mono
26+
import java.lang.NullPointerException
2527

2628
/**
2729
* Unit tests for [RowsFetchSpec] extensions.
@@ -31,7 +33,7 @@ import reactor.core.publisher.Mono
3133
class RowsFetchSpecExtensionsTests {
3234

3335
@Test // gh-63
34-
fun awaitOne() {
36+
fun awaitOneWithValue() {
3537

3638
val spec = mockk<RowsFetchSpec<String>>()
3739
every { spec.one() } returns Mono.just("foo")
@@ -46,7 +48,52 @@ class RowsFetchSpecExtensionsTests {
4648
}
4749

4850
@Test // gh-63
49-
fun awaitFirst() {
51+
fun awaitOneWithNull() {
52+
53+
val spec = mockk<RowsFetchSpec<String>>()
54+
every { spec.one() } returns Mono.empty()
55+
56+
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
57+
runBlocking { spec.awaitOne() }
58+
}
59+
60+
verify {
61+
spec.one()
62+
}
63+
}
64+
65+
@Test // gh-63
66+
fun awaitOneOrNullWithValue() {
67+
68+
val spec = mockk<RowsFetchSpec<String>>()
69+
every { spec.one() } returns Mono.just("foo")
70+
71+
runBlocking {
72+
assertThat(spec.awaitOneOrNull()).isEqualTo("foo")
73+
}
74+
75+
verify {
76+
spec.one()
77+
}
78+
}
79+
80+
@Test // gh-63
81+
fun awaitOneOrNullWithNull() {
82+
83+
val spec = mockk<RowsFetchSpec<String>>()
84+
every { spec.one() } returns Mono.empty()
85+
86+
runBlocking {
87+
assertThat(spec.awaitOneOrNull()).isNull()
88+
}
89+
90+
verify {
91+
spec.one()
92+
}
93+
}
94+
95+
@Test // gh-63
96+
fun awaitFirstWithValue() {
5097

5198
val spec = mockk<RowsFetchSpec<String>>()
5299
every { spec.first() } returns Mono.just("foo")
@@ -59,4 +106,49 @@ class RowsFetchSpecExtensionsTests {
59106
spec.first()
60107
}
61108
}
109+
110+
@Test // gh-63
111+
fun awaitFirstWithNull() {
112+
113+
val spec = mockk<RowsFetchSpec<String>>()
114+
every { spec.first() } returns Mono.empty()
115+
116+
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
117+
runBlocking { spec.awaitFirst() }
118+
}
119+
120+
verify {
121+
spec.first()
122+
}
123+
}
124+
125+
@Test // gh-63
126+
fun awaitFirstOrNullWithValue() {
127+
128+
val spec = mockk<RowsFetchSpec<String>>()
129+
every { spec.first() } returns Mono.just("foo")
130+
131+
runBlocking {
132+
assertThat(spec.awaitFirstOrNull()).isEqualTo("foo")
133+
}
134+
135+
verify {
136+
spec.first()
137+
}
138+
}
139+
140+
@Test // gh-63
141+
fun awaitFirstOrNullWithNull() {
142+
143+
val spec = mockk<RowsFetchSpec<String>>()
144+
every { spec.first() } returns Mono.empty()
145+
146+
runBlocking {
147+
assertThat(spec.awaitFirstOrNull()).isNull()
148+
}
149+
150+
verify {
151+
spec.first()
152+
}
153+
}
62154
}

0 commit comments

Comments
 (0)