Skip to content

Commit 40ba2c9

Browse files
committed
Add test to verify interface projections through R2dbcEntityTemplate.
See #1690
1 parent 0da08e7 commit 40ba2c9

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

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

+26-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,28 @@ void shouldCountBy() {
104103
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
105104
}
106105

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

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

114130
recorder.addStubbing(s -> s.startsWith("SELECT"), result);
@@ -271,7 +287,7 @@ void shouldDeleteByQuery() {
271287

272288
recorder.addStubbing(s -> s.startsWith("DELETE"), result);
273289

274-
entityTemplate.delete(Query.query(Criteria.where("name").is("Walter")), Person.class) //
290+
entityTemplate.delete(Query.query(where("name").is("Walter")), Person.class) //
275291
.as(StepVerifier::create) //
276292
.expectNext(1L) //
277293
.verifyComplete();
@@ -564,6 +580,11 @@ public Person withDescription(String description) {
564580
}
565581
}
566582

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

569590
public VersionedPerson withId(String id) {

0 commit comments

Comments
 (0)