|
| 1 | +/* |
| 2 | + * Copyright 2020 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.repository |
| 17 | + |
| 18 | +import io.r2dbc.spi.test.MockResult |
| 19 | +import kotlinx.coroutines.runBlocking |
| 20 | +import org.junit.jupiter.api.BeforeEach |
| 21 | +import org.junit.jupiter.api.Test |
| 22 | +import org.springframework.data.annotation.Id |
| 23 | +import org.springframework.data.r2dbc.core.DatabaseClient |
| 24 | +import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy |
| 25 | +import org.springframework.data.r2dbc.core.R2dbcEntityTemplate |
| 26 | +import org.springframework.data.r2dbc.dialect.PostgresDialect |
| 27 | +import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory |
| 28 | +import org.springframework.data.r2dbc.testing.StatementRecorder |
| 29 | +import org.springframework.data.repository.kotlin.CoroutineCrudRepository |
| 30 | + |
| 31 | +/** |
| 32 | + * Unit tests for [CoroutineCrudRepository]. |
| 33 | + * |
| 34 | + * @author Mark Paluch |
| 35 | + */ |
| 36 | +class CoroutineRepositoryUnitTests { |
| 37 | + |
| 38 | + lateinit var client: DatabaseClient |
| 39 | + lateinit var entityTemplate: R2dbcEntityTemplate |
| 40 | + lateinit var recorder: StatementRecorder |
| 41 | + lateinit var repositoryFactory: R2dbcRepositoryFactory |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + fun before() { |
| 45 | + recorder = StatementRecorder.newInstance() |
| 46 | + client = DatabaseClient.builder().connectionFactory(recorder) |
| 47 | + .dataAccessStrategy(DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE)).build() |
| 48 | + entityTemplate = R2dbcEntityTemplate(client) |
| 49 | + repositoryFactory = R2dbcRepositoryFactory(entityTemplate) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun shouldIssueDeleteQuery() { |
| 54 | + |
| 55 | + val result = MockResult.builder().rowsUpdated(2).build() |
| 56 | + recorder.addStubbing({ s: String -> s.startsWith("DELETE") }, result) |
| 57 | + |
| 58 | + val repository = repositoryFactory.getRepository(PersonRepository::class.java) |
| 59 | + |
| 60 | + runBlocking { |
| 61 | + repository.deleteUserAssociation(2) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + interface PersonRepository : CoroutineCrudRepository<Person, Long> { |
| 66 | + |
| 67 | + @Modifying |
| 68 | + @Query("DELETE FROM person WHERE id = :id ") |
| 69 | + suspend fun deleteUserAssociation(userId: Int) |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + data class Person(@Id var id: Long, var name: String) |
| 74 | +} |
0 commit comments