Skip to content

Commit 60e22da

Browse files
committed
Add test to verify interface projections through R2dbcEntityTemplate.
See #1690
1 parent fd011ec commit 60e22da

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplateUnitTests.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
20+
import static org.springframework.data.relational.core.query.Criteria.*;
2021

2122
import io.r2dbc.spi.R2dbcType;
2223
import io.r2dbc.spi.test.MockColumnMetadata;
@@ -87,8 +88,6 @@ void before() {
8788
@Test // gh-220
8889
void shouldCountBy() {
8990

90-
MockRowMetadata metadata = MockRowMetadata.builder()
91-
.columnMetadata(MockColumnMetadata.builder().name("name").type(R2dbcType.VARCHAR).build()).build();
9291
MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Long.class, 1L).build()).build();
9392

9493
recorder.addStubbing(s -> s.startsWith("SELECT"), result);
@@ -104,11 +103,29 @@ void shouldCountBy() {
104103
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
105104
}
106105

106+
@Test
107+
// GH-1690
108+
void shouldApplyInterfaceProjection() {
109+
110+
MockRowMetadata metadata = MockRowMetadata.builder()
111+
.columnMetadata(MockColumnMetadata.builder().name("THE_NAME").type(R2dbcType.VARCHAR).build()).build();
112+
MockResult result = MockResult.builder()
113+
.row(MockRow.builder().identified("THE_NAME", Object.class, "Walter").metadata(metadata).build()).build();
114+
115+
recorder.addStubbing(s -> s.startsWith("SELECT"), result);
116+
117+
entityTemplate.select(Person.class) //
118+
.from("foo") //
119+
.as(PersonProjection.class) //
120+
.matching(Query.query(Criteria.where("name").is("Walter"))) //
121+
.all() //
122+
.as(StepVerifier::create) //
123+
.assertNext(actual -> assertThat(actual.getName()).isEqualTo("Walter")).verifyComplete();
124+
}
125+
107126
@Test // gh-469
108127
void shouldProjectExistsResult() {
109128

110-
MockRowMetadata metadata = MockRowMetadata.builder()
111-
.columnMetadata(MockColumnMetadata.builder().name("name").type(R2dbcType.VARCHAR).build()).build();
112129
MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Object.class, null).build()).build();
113130

114131
recorder.addStubbing(s -> s.startsWith("SELECT"), result);
@@ -271,7 +288,7 @@ void shouldDeleteByQuery() {
271288

272289
recorder.addStubbing(s -> s.startsWith("DELETE"), result);
273290

274-
entityTemplate.delete(Query.query(Criteria.where("name").is("Walter")), Person.class) //
291+
entityTemplate.delete(Query.query(where("name").is("Walter")), Person.class) //
275292
.as(StepVerifier::create) //
276293
.expectNext(1L) //
277294
.verifyComplete();
@@ -564,6 +581,11 @@ public Person withDescription(String description) {
564581
}
565582
}
566583

584+
interface PersonProjection {
585+
586+
String getName();
587+
}
588+
567589
record VersionedPerson(@Id String id, @Version long version, String name) {
568590

569591
public VersionedPerson withId(String id) {

0 commit comments

Comments
 (0)