@ -25,6 +25,7 @@ import io.r2dbc.spi.test.MockRow;
import io.r2dbc.spi.test.MockRowMetadata ;
import io.r2dbc.spi.test.MockRowMetadata ;
import lombok.Value ;
import lombok.Value ;
import lombok.With ;
import lombok.With ;
import org.springframework.data.relational.core.mapping.InsertOnlyProperty ;
import reactor.core.publisher.Mono ;
import reactor.core.publisher.Mono ;
import reactor.test.StepVerifier ;
import reactor.test.StepVerifier ;
@ -69,6 +70,7 @@ import org.springframework.util.CollectionUtils;
* @author Mark Paluch
* @author Mark Paluch
* @author Jose Luis Leon
* @author Jose Luis Leon
* @author Robert Heim
* @author Robert Heim
* @author Jens Schauder
* /
* /
public class R2dbcEntityTemplateUnitTests {
public class R2dbcEntityTemplateUnitTests {
@ -85,7 +87,8 @@ public class R2dbcEntityTemplateUnitTests {
entityTemplate = new R2dbcEntityTemplate ( client , PostgresDialect . INSTANCE ) ;
entityTemplate = new R2dbcEntityTemplate ( client , PostgresDialect . INSTANCE ) ;
}
}
@Test // gh-220
@Test
// gh-220
void shouldCountBy ( ) {
void shouldCountBy ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( )
MockRowMetadata metadata = MockRowMetadata . builder ( )
@ -105,7 +108,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-469
@Test
// gh-469
void shouldProjectExistsResult ( ) {
void shouldProjectExistsResult ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( )
MockRowMetadata metadata = MockRowMetadata . builder ( )
@ -122,7 +126,8 @@ public class R2dbcEntityTemplateUnitTests {
. verifyComplete ( ) ;
. verifyComplete ( ) ;
}
}
@Test // gh-1310
@Test
// gh-1310
void shouldProjectExistsResultWithoutId ( ) {
void shouldProjectExistsResultWithoutId ( ) {
MockResult result = MockResult . builder ( ) . row ( MockRow . builder ( ) . identified ( 0 , Object . class , null ) . build ( ) ) . build ( ) ;
MockResult result = MockResult . builder ( ) . row ( MockRow . builder ( ) . identified ( 0 , Object . class , null ) . build ( ) ) . build ( ) ;
@ -134,7 +139,8 @@ public class R2dbcEntityTemplateUnitTests {
. expectNext ( true ) . verifyComplete ( ) ;
. expectNext ( true ) . verifyComplete ( ) ;
}
}
@Test // gh-1310
@Test
// gh-1310
void shouldProjectCountResultWithoutId ( ) {
void shouldProjectCountResultWithoutId ( ) {
MockResult result = MockResult . builder ( ) . row ( MockRow . builder ( ) . identified ( 0 , Long . class , 1L ) . build ( ) ) . build ( ) ;
MockResult result = MockResult . builder ( ) . row ( MockRow . builder ( ) . identified ( 0 , Long . class , 1L ) . build ( ) ) . build ( ) ;
@ -146,7 +152,8 @@ public class R2dbcEntityTemplateUnitTests {
. expectNext ( 1L ) . verifyComplete ( ) ;
. expectNext ( 1L ) . verifyComplete ( ) ;
}
}
@Test // gh-469
@Test
// gh-469
void shouldExistsByCriteria ( ) {
void shouldExistsByCriteria ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( )
MockRowMetadata metadata = MockRowMetadata . builder ( )
@ -166,7 +173,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-220
@Test
// gh-220
void shouldSelectByCriteria ( ) {
void shouldSelectByCriteria ( ) {
recorder . addStubbing ( s - > s . startsWith ( "SELECT" ) , Collections . emptyList ( ) ) ;
recorder . addStubbing ( s - > s . startsWith ( "SELECT" ) , Collections . emptyList ( ) ) ;
@ -182,7 +190,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-215
@Test
// gh-215
void selectShouldInvokeCallback ( ) {
void selectShouldInvokeCallback ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( )
MockRowMetadata metadata = MockRowMetadata . builder ( )
@ -208,7 +217,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( callback . getValues ( ) ) . hasSize ( 1 ) ;
assertThat ( callback . getValues ( ) ) . hasSize ( 1 ) ;
}
}
@Test // gh-220
@Test
// gh-220
void shouldSelectOne ( ) {
void shouldSelectOne ( ) {
recorder . addStubbing ( s - > s . startsWith ( "SELECT" ) , Collections . emptyList ( ) ) ;
recorder . addStubbing ( s - > s . startsWith ( "SELECT" ) , Collections . emptyList ( ) ) ;
@ -224,7 +234,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-220, gh-758
@Test
// gh-220, gh-758
void shouldSelectOneDoNotOverrideExistingLimit ( ) {
void shouldSelectOneDoNotOverrideExistingLimit ( ) {
recorder . addStubbing ( s - > s . startsWith ( "SELECT" ) , Collections . emptyList ( ) ) ;
recorder . addStubbing ( s - > s . startsWith ( "SELECT" ) , Collections . emptyList ( ) ) ;
@ -241,7 +252,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-220
@Test
// gh-220
void shouldUpdateByQuery ( ) {
void shouldUpdateByQuery ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( )
MockRowMetadata metadata = MockRowMetadata . builder ( )
@ -263,7 +275,8 @@ public class R2dbcEntityTemplateUnitTests {
Parameter . from ( "Walter" ) ) ;
Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-220
@Test
// gh-220
void shouldDeleteByQuery ( ) {
void shouldDeleteByQuery ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( )
MockRowMetadata metadata = MockRowMetadata . builder ( )
@ -283,7 +296,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-220
@Test
// gh-220
void shouldDeleteEntity ( ) {
void shouldDeleteEntity ( ) {
Person person = Person . empty ( ) //
Person person = Person . empty ( ) //
@ -300,7 +314,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "Walter" ) ) ;
}
}
@Test // gh-365
@Test
// gh-365
void shouldInsertVersioned ( ) {
void shouldInsertVersioned ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -321,7 +336,8 @@ public class R2dbcEntityTemplateUnitTests {
Parameter . from ( 1L ) ) ;
Parameter . from ( 1L ) ) ;
}
}
@Test // gh-557, gh-402
@Test
// gh-557, gh-402
void shouldSkipDefaultIdValueOnInsert ( ) {
void shouldSkipDefaultIdValueOnInsert ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -339,7 +355,8 @@ public class R2dbcEntityTemplateUnitTests {
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "bar" ) ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 1 ) . containsEntry ( 0 , Parameter . from ( "bar" ) ) ;
}
}
@Test // gh-557, gh-402
@Test
// gh-557, gh-402
void shouldSkipDefaultIdValueOnVersionedInsert ( ) {
void shouldSkipDefaultIdValueOnVersionedInsert ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -361,7 +378,8 @@ public class R2dbcEntityTemplateUnitTests {
Parameter . from ( "bar" ) ) ;
Parameter . from ( "bar" ) ) ;
}
}
@Test // gh-451
@Test
// gh-451
void shouldInsertCorrectlyVersionedAndAudited ( ) {
void shouldInsertCorrectlyVersionedAndAudited ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -389,7 +407,8 @@ public class R2dbcEntityTemplateUnitTests {
"INSERT INTO with_auditing_and_optimistic_locking (version, name, created_date, last_modified_date) VALUES ($1, $2, $3, $4)" ) ;
"INSERT INTO with_auditing_and_optimistic_locking (version, name, created_date, last_modified_date) VALUES ($1, $2, $3, $4)" ) ;
}
}
@Test // gh-451
@Test
// gh-451
void shouldUpdateCorrectlyVersionedAndAudited ( ) {
void shouldUpdateCorrectlyVersionedAndAudited ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -418,7 +437,8 @@ public class R2dbcEntityTemplateUnitTests {
"UPDATE with_auditing_and_optimistic_locking SET version = $1, name = $2, created_date = $3, last_modified_date = $4" ) ;
"UPDATE with_auditing_and_optimistic_locking SET version = $1, name = $2, created_date = $3, last_modified_date = $4" ) ;
}
}
@Test // gh-215
@Test
// gh-215
void insertShouldInvokeCallback ( ) {
void insertShouldInvokeCallback ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -446,7 +466,8 @@ public class R2dbcEntityTemplateUnitTests {
Parameter . from ( "before-save" ) ) ;
Parameter . from ( "before-save" ) ) ;
}
}
@Test // gh-365
@Test
// gh-365
void shouldUpdateVersioned ( ) {
void shouldUpdateVersioned ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -468,7 +489,8 @@ public class R2dbcEntityTemplateUnitTests {
Parameter . from ( 1L ) ) ;
Parameter . from ( 1L ) ) ;
}
}
@Test // gh-215
@Test
// gh-215
void updateShouldInvokeCallback ( ) {
void updateShouldInvokeCallback ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
@ -501,6 +523,48 @@ public class R2dbcEntityTemplateUnitTests {
Parameter . from ( "before-save" ) ) ;
Parameter . from ( "before-save" ) ) ;
}
}
@Test
// gh-637
void insertIncludesInsertOnlyColumns ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockResult result = MockResult . builder ( ) . rowMetadata ( metadata ) . rowsUpdated ( 1 ) . build ( ) ;
recorder . addStubbing ( s - > s . startsWith ( "INSERT" ) , result ) ;
entityTemplate . insert ( new WithInsertOnly ( null , "Alfred" , "insert this" ) ) . as ( StepVerifier : : create ) //
. expectNextCount ( 1 ) //
. verifyComplete ( ) ;
StatementRecorder . RecordedStatement statement = recorder . getCreatedStatement ( s - > s . startsWith ( "INSERT" ) ) ;
assertThat ( statement . getSql ( ) ) . isEqualTo ( "INSERT INTO with_insert_only (name, insert_only) VALUES ($1, $2)" ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 2 )
. containsEntry ( 0 , Parameter . from ( "Alfred" ) )
. containsEntry ( 1 , Parameter . from ( "insert this" ) ) ;
}
@Test
// gh-637
void updateExcludesInsertOnlyColumns ( ) {
MockRowMetadata metadata = MockRowMetadata . builder ( ) . build ( ) ;
MockResult result = MockResult . builder ( ) . rowMetadata ( metadata ) . rowsUpdated ( 1 ) . build ( ) ;
recorder . addStubbing ( s - > s . startsWith ( "UPDATE" ) , result ) ;
entityTemplate . update ( new WithInsertOnly ( 23L , "Alfred" , "don't update this" ) ) . as ( StepVerifier : : create ) //
. expectNextCount ( 1 ) //
. verifyComplete ( ) ;
StatementRecorder . RecordedStatement statement = recorder . getCreatedStatement ( s - > s . startsWith ( "UPDATE" ) ) ;
assertThat ( statement . getSql ( ) ) . isEqualTo ( "UPDATE with_insert_only SET name = $1 WHERE with_insert_only.id = $2" ) ;
assertThat ( statement . getBindings ( ) ) . hasSize ( 2 )
. containsEntry ( 0 , Parameter . from ( "Alfred" ) )
. containsEntry ( 1 , Parameter . from ( 23L ) ) ;
}
@Value
@Value
static class WithoutId {
static class WithoutId {
@ -511,9 +575,11 @@ public class R2dbcEntityTemplateUnitTests {
@With
@With
static class Person {
static class Person {
@Id String id ;
@Id
String id ;
@Column ( "THE_NAME" ) String name ;
@Column ( "THE_NAME" )
String name ;
String description ;
String description ;
@ -526,9 +592,11 @@ public class R2dbcEntityTemplateUnitTests {
@With
@With
private static class VersionedPerson {
private static class VersionedPerson {
@Id String id ;
@Id
String id ;
@Version long version ;
@Version
long version ;
String name ;
String name ;
}
}
@ -537,7 +605,8 @@ public class R2dbcEntityTemplateUnitTests {
@With
@With
private static class PersonWithPrimitiveId {
private static class PersonWithPrimitiveId {
@Id int id ;
@Id
int id ;
String name ;
String name ;
}
}
@ -546,9 +615,11 @@ public class R2dbcEntityTemplateUnitTests {
@With
@With
private static class VersionedPersonWithPrimitiveId {
private static class VersionedPersonWithPrimitiveId {
@Id int id ;
@Id
int id ;
@Version long version ;
@Version
long version ;
String name ;
String name ;
}
}
@ -557,14 +628,29 @@ public class R2dbcEntityTemplateUnitTests {
@With
@With
private static class WithAuditingAndOptimisticLocking {
private static class WithAuditingAndOptimisticLocking {
@Id String id ;
@Id
String id ;
@Version
long version ;
String name ;
@CreatedDate
LocalDateTime createdDate ;
@LastModifiedDate
LocalDateTime lastModifiedDate ;
}
@Version long version ;
@Value
private static class WithInsertOnly {
@Id
Long id ;
String name ;
String name ;
@CreatedDate LocalDateTime createdDate ;
@InsertOnlyProperty
@LastModifiedDate LocalDateTime lastModifiedDate ;
String insertOnly ;
}
}
static class ValueCapturingEntityCallback < T > {
static class ValueCapturingEntityCallback < T > {