Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit 8e161b3

Browse files
author
Jonas Bark
committed
spring-projects#122 - Improve Kotlin extensions for CriteriaStep and DatabaseClient.
1 parent 36c4c1d commit 8e161b3

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2018-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.core
17+
18+
import org.springframework.data.r2dbc.query.Criteria
19+
20+
/**
21+
* Extension for [Criteria.CriteriaStep.is] providing a
22+
* `eq(value)` variant.
23+
*
24+
* @author Jonas Bark
25+
*/
26+
infix fun Criteria.CriteriaStep.isEquals(value: Any): Criteria =
27+
`is`(value)
28+
29+
/**
30+
* Extension for [Criteria.CriteriaStep.in] providing a
31+
* `isIn(value)` variant.
32+
*
33+
* @author Jonas Bark
34+
*/
35+
fun Criteria.CriteriaStep.isIn(vararg value: Any): Criteria =
36+
`in`(value)
37+
38+
39+
/**
40+
* Extension for [Criteria.CriteriaStep.in] providing a
41+
* `isIn(value)` variant.
42+
*
43+
* @author Jonas Bark
44+
*/
45+
fun Criteria.CriteriaStep.isIn(values: Collection<Any>): Criteria =
46+
`in`(values)

src/main/kotlin/org/springframework/data/r2dbc/core/DatabaseClientExtensions.kt

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.r2dbc.core
1717

1818
import kotlinx.coroutines.reactive.awaitFirstOrNull
19+
import org.springframework.data.r2dbc.query.Criteria
1920

2021
/**
2122
* Coroutines variant of [DatabaseClient.GenericExecuteSpec.then].
@@ -80,3 +81,20 @@ suspend fun <T> DatabaseClient.InsertSpec<T>.await() {
8081
inline fun <reified T : Any> DatabaseClient.InsertIntoSpec.into(): DatabaseClient.TypedInsertSpec<T> =
8182
into(T::class.java)
8283

84+
/**
85+
* Extension for [DatabaseClient.SelectFromSpec.from] providing a
86+
* `from<Foo>()` variant.
87+
*
88+
* @author Jonas Bark
89+
*/
90+
inline fun <reified T : Any> DatabaseClient.SelectFromSpec.from(): DatabaseClient.TypedSelectSpec<T> =
91+
from(T::class.java)
92+
93+
/**
94+
* Extension for [DatabaseClient.SelectFromSpec.from] providing a
95+
* `from<Foo>()` variant.
96+
*
97+
* @author Jonas Bark
98+
*/
99+
inline fun <reified T : Any> DatabaseClient.DeleteFromSpec.from(): DatabaseClient.TypedDeleteSpec<T> =
100+
from(T::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2018-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.core
17+
18+
import io.mockk.every
19+
import io.mockk.mockk
20+
import io.mockk.spyk
21+
import io.mockk.verify
22+
import kotlinx.coroutines.runBlocking
23+
import org.assertj.core.api.Assertions.assertThat
24+
import org.junit.Test
25+
import org.springframework.data.r2dbc.query.Criteria
26+
import reactor.core.publisher.Mono
27+
28+
/**
29+
* Unit tests for [Criteria.CriteriaStep] extensions.
30+
*
31+
* @author Jonas Bark
32+
*/
33+
class CriteriaStepExtensionsTests {
34+
35+
@Test // gh-63
36+
fun eqIsCriteriaStep() {
37+
38+
val spec = mockk<Criteria.CriteriaStep>()
39+
val eqSpec = mockk<Criteria>()
40+
41+
every { spec.`is`("test") } returns eqSpec
42+
43+
runBlocking {
44+
assertThat(spec isEquals "test").isEqualTo(eqSpec)
45+
}
46+
47+
verify {
48+
spec.`is`("test")
49+
}
50+
}
51+
}

src/test/kotlin/org/springframework/data/r2dbc/core/DatabaseClientExtensionsTests.kt

+35
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ package org.springframework.data.r2dbc.core
1717

1818
import io.mockk.every
1919
import io.mockk.mockk
20+
import io.mockk.spyk
2021
import io.mockk.verify
2122
import kotlinx.coroutines.runBlocking
2223
import org.assertj.core.api.Assertions.assertThat
2324
import org.junit.Test
25+
import org.springframework.data.r2dbc.query.Criteria
2426
import reactor.core.publisher.Mono
2527

2628
/**
2729
* Unit tests for [DatabaseClient] extensions.
2830
*
2931
* @author Sebastien Deleuze
32+
* @author Jonas Bark
3033
*/
3134
class DatabaseClientExtensionsTests {
3235

@@ -137,4 +140,36 @@ class DatabaseClientExtensionsTests {
137140
spec.into(String::class.java)
138141
}
139142
}
143+
144+
@Test // gh-63
145+
fun selectFromSpecInto() {
146+
147+
val spec = mockk<DatabaseClient.SelectFromSpec>()
148+
val typedSpec: DatabaseClient.TypedSelectSpec<String> = mockk()
149+
every { spec.from(String::class.java) } returns typedSpec
150+
151+
runBlocking {
152+
assertThat(spec.from<String>()).isEqualTo(typedSpec)
153+
}
154+
155+
verify {
156+
spec.from(String::class.java)
157+
}
158+
}
159+
160+
@Test // gh-63
161+
fun deleteFromSpecInto() {
162+
163+
val spec = mockk<DatabaseClient.DeleteFromSpec>()
164+
val typedSpec: DatabaseClient.TypedDeleteSpec<String> = mockk()
165+
every { spec.from(String::class.java) } returns typedSpec
166+
167+
runBlocking {
168+
assertThat(spec.from<String>()).isEqualTo(typedSpec)
169+
}
170+
171+
verify {
172+
spec.from(String::class.java)
173+
}
174+
}
140175
}

0 commit comments

Comments
 (0)