Skip to content

Commit b89e070

Browse files
schaudermp911de
authored andcommitted
By default R2DBC uses quoted identifiers.
Most tests got fixed by reverting to non quoted identifiers for the test. Interesting things that became obvious: - SpEL expressions get the transformed (e.g. upper case) and quoted table name!? See TableNameQueryPreprocessorUnitTests and SqlInspectingR2dbcRepositoryUnitTests Closes #1993 Original pull request #2066
1 parent d8e4768 commit b89e070

22 files changed

+253
-174
lines changed

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/mapping/R2dbcMappingContext.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525
* R2DBC-specific extension to {@link RelationalMappingContext}.
2626
*
2727
* @author Mark Paluch
28+
* @author Jens Schauder
2829
*/
2930
public class R2dbcMappingContext extends RelationalMappingContext {
3031

3132
/**
3233
* Create a new {@link R2dbcMappingContext}.
3334
*/
34-
public R2dbcMappingContext() {
35-
setForceQuote(false);
36-
}
35+
public R2dbcMappingContext() {}
3736

3837
/**
3938
* Create a new {@link R2dbcMappingContext} using the given {@link NamingStrategy}.
@@ -42,7 +41,6 @@ public R2dbcMappingContext() {
4241
*/
4342
public R2dbcMappingContext(NamingStrategy namingStrategy) {
4443
super(namingStrategy);
45-
setForceQuote(false);
4644
}
4745

4846
@Override

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/convert/MappingR2dbcConverterUnitTests.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@
1515
*/
1616
package org.springframework.data.r2dbc.convert;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
1821
import io.r2dbc.spi.R2dbcType;
1922
import io.r2dbc.spi.Row;
2023
import io.r2dbc.spi.test.MockColumnMetadata;
2124
import io.r2dbc.spi.test.MockRow;
2225
import io.r2dbc.spi.test.MockRowMetadata;
26+
27+
import java.time.Instant;
28+
import java.time.LocalDateTime;
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
import java.util.Map;
32+
2333
import org.junit.jupiter.api.BeforeEach;
2434
import org.junit.jupiter.api.Test;
2535
import org.springframework.beans.factory.annotation.Value;
@@ -37,19 +47,11 @@
3747
import org.springframework.data.relational.core.sql.SqlIdentifier;
3848
import org.springframework.r2dbc.core.Parameter;
3949

40-
import java.time.Instant;
41-
import java.time.LocalDateTime;
42-
import java.util.Arrays;
43-
import java.util.Collections;
44-
import java.util.Map;
45-
46-
import static org.assertj.core.api.Assertions.*;
47-
import static org.mockito.Mockito.*;
48-
4950
/**
5051
* Unit tests for {@link MappingR2dbcConverter}.
5152
*
5253
* @author Mark Paluch
54+
* @author Jens Schauder
5355
*/
5456
public class MappingR2dbcConverterUnitTests {
5557

@@ -65,7 +67,7 @@ void before() {
6567
StringToSimplePersonConverter.INSTANCE));
6668

6769
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
68-
70+
mappingContext.setForceQuote(false);
6971
converter = new MappingR2dbcConverter(mappingContext, conversions);
7072
}
7173

@@ -261,8 +263,7 @@ void writeShouldObtainIdFromIdentifierAccessor() {
261263
}
262264

263265
static class Person {
264-
@Id
265-
String id;
266+
@Id String id;
266267
String firstname, lastname;
267268
Instant instant;
268269
LocalDateTime localDateTime;
@@ -298,8 +299,7 @@ public void setLastname(String lastname) {
298299
}
299300

300301
static class WithEnum {
301-
@Id
302-
String id;
302+
@Id String id;
303303
Condition condition;
304304

305305
public WithEnum(String id, Condition condition) {
@@ -313,8 +313,7 @@ enum Condition {
313313
}
314314

315315
static class PersonWithConversions {
316-
@Id
317-
String id;
316+
@Id String id;
318317
Map<String, String> nested;
319318
NonMappableEntity unsupported;
320319

@@ -325,8 +324,7 @@ public PersonWithConversions(String id, Map<String, String> nested, NonMappableE
325324
}
326325
}
327326

328-
record WithPrimitiveId (
329-
@Id long id){
327+
record WithPrimitiveId(@Id long id) {
330328
}
331329

332330
static class CustomConversionPerson {

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/convert/MySqlMappingR2dbcConverterUnitTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
3535
import org.springframework.data.r2dbc.testing.OutboundRowAssert;
3636
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
37+
import org.springframework.data.relational.core.sql.SqlIdentifier;
3738

3839
/**
3940
* MySQL-specific unit tests for {@link MappingR2dbcConverter}.
4041
*
4142
* @author Mark Paluch
43+
* @author Jens Schauder
4244
*/
4345
class MySqlMappingR2dbcConverterUnitTests {
4446

@@ -68,8 +70,9 @@ void shouldWriteBooleanToByte() {
6870

6971
converter.write(object, row);
7072

71-
OutboundRowAssert.assertThat(row).containsColumnWithValue("flag1", (byte) 1).containsColumnWithValue("flag2",
72-
(byte) 0);
73+
OutboundRowAssert.assertThat(row) //
74+
.containsColumnWithValue(SqlIdentifier.quoted("FLAG1"), (byte) 1) //
75+
.containsColumnWithValue(SqlIdentifier.quoted("FLAG2"), (byte) 0);
7376
}
7477

7578
@Test // gh-589
@@ -96,18 +99,15 @@ void shouldPreserveByteValue() {
9699

97100
converter.write(object, row);
98101

99-
OutboundRowAssert.assertThat(row).containsColumnWithValue("state", (byte) 3);
102+
OutboundRowAssert.assertThat(row).containsColumnWithValue(SqlIdentifier.quoted("STATE"), (byte) 3);
100103
}
101104

102105
record BooleanMapping(
103106

104107
Integer id, boolean flag1, boolean flag2) {
105108
}
106109

107-
record WithByte (
108-
109-
Integer id,
110-
byte state){
110+
record WithByte(Integer id, byte state) {
111111
}
112112

113113
}

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/convert/PostgresMappingR2dbcConverterUnitTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* Postgres-specific unit tests for {@link MappingR2dbcConverter}.
5050
*
5151
* @author Mark Paluch
52+
* @author Jens Schauder
5253
*/
5354
class PostgresMappingR2dbcConverterUnitTests {
5455

@@ -79,7 +80,7 @@ void shouldPassThruJson() {
7980
OutboundRow row = new OutboundRow();
8081
converter.write(person, row);
8182

82-
assertThat(row).containsEntry(SqlIdentifier.unquoted("json_value"), Parameter.from(person.jsonValue));
83+
assertThat(row).containsEntry(SqlIdentifier.quoted("JSON_VALUE"), Parameter.from(person.jsonValue));
8384
}
8485

8586
@Test // gh-453
@@ -127,24 +128,18 @@ void shouldApplyCustomWritingConverter() {
127128
OutboundRow row = new OutboundRow();
128129
converter.write(object, row);
129130

130-
Parameter parameter = row.get(SqlIdentifier.unquoted("holder"));
131+
Parameter parameter = row.get(SqlIdentifier.quoted("HOLDER"));
131132
assertThat(parameter).isNotNull();
132133
assertThat(parameter.getValue()).isInstanceOf(Json.class);
133134
}
134135

135-
record JsonPerson(
136-
@Id Long id,
137-
Json jsonValue) {
136+
record JsonPerson(@Id Long id, Json jsonValue) {
138137
}
139138

140-
record ConvertedJson(
141-
@Id Long id,
142-
String jsonString,
143-
byte[] jsonBytes) {
139+
record ConvertedJson(@Id Long id, String jsonString, byte[] jsonBytes) {
144140
}
145141

146-
record WithJsonHolder(
147-
JsonHolder holder) {
142+
record WithJsonHolder(JsonHolder holder) {
148143
}
149144

150145
@ReadingConverter

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
import org.springframework.data.convert.ReadingConverter;
3434
import org.springframework.data.convert.WritingConverter;
3535
import org.springframework.data.r2dbc.convert.EnumWriteSupport;
36+
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
37+
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
3638
import org.springframework.data.r2dbc.core.StatementMapper.InsertSpec;
3739
import org.springframework.data.r2dbc.dialect.PostgresDialect;
3840
import org.springframework.data.r2dbc.mapping.OutboundRow;
41+
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
3942
import org.springframework.data.relational.core.sql.SqlIdentifier;
4043
import org.springframework.r2dbc.core.Parameter;
4144
import org.springframework.r2dbc.core.PreparedOperation;
@@ -45,14 +48,34 @@
4548
* {@link PostgresDialect} specific tests for {@link ReactiveDataAccessStrategy}.
4649
*
4750
* @author Mark Paluch
51+
* @author Jens Schauder
4852
*/
4953
public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessStrategyTestSupport {
5054

51-
private final ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
52-
Arrays.asList(DurationToIntervalConverter.INSTANCE, IntervalToDurationConverter.INSTANCE));
55+
private R2dbcMappingContext context;
56+
private final ReactiveDataAccessStrategy strategy;
57+
58+
{
59+
context = new R2dbcMappingContext();
60+
context.setForceQuote(false);
61+
62+
DefaultReactiveDataAccessStrategy strategy1 = createReactiveDataAccessStrategy(DurationToIntervalConverter.INSTANCE,
63+
IntervalToDurationConverter.INSTANCE);
64+
strategy = strategy1;
65+
}
66+
67+
private DefaultReactiveDataAccessStrategy createReactiveDataAccessStrategy(Object... converters) {
68+
R2dbcCustomConversions customConversions = R2dbcCustomConversions.of(PostgresDialect.INSTANCE, converters);
69+
70+
MappingR2dbcConverter converter = new MappingR2dbcConverter(context, customConversions);
71+
DefaultReactiveDataAccessStrategy strategy1 = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
72+
converter);
73+
return strategy1;
74+
}
5375

5476
@Override
5577
protected ReactiveDataAccessStrategy getStrategy() {
78+
5679
return strategy;
5780
}
5881

@@ -98,8 +121,6 @@ void shouldConvertCollectionToArray() {
98121
@Test // gh-139
99122
void shouldConvertToArray() {
100123

101-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
102-
103124
WithArray withArray = new WithArray();
104125
withArray.stringArray = new String[] { "hello", "world" };
105126
withArray.stringList = Arrays.asList("hello", "world");
@@ -113,8 +134,7 @@ void shouldConvertToArray() {
113134
@Test // gh-139
114135
void shouldApplyCustomConversion() {
115136

116-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
117-
Collections.singletonList(MyObjectsToStringConverter.INSTANCE));
137+
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(MyObjectsToStringConverter.INSTANCE);
118138

119139
WithConversion withConversion = new WithConversion();
120140
withConversion.myObjects = Arrays.asList(new MyObject("one"), new MyObject("two"));
@@ -127,8 +147,7 @@ void shouldApplyCustomConversion() {
127147
@Test // gh-139
128148
void shouldApplyCustomConversionForNull() {
129149

130-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
131-
Collections.singletonList(MyObjectsToStringConverter.INSTANCE));
150+
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(MyObjectsToStringConverter.INSTANCE);
132151

133152
WithConversion withConversion = new WithConversion();
134153
withConversion.myObjects = null;
@@ -152,8 +171,6 @@ void shouldApplyCustomConversionForEmptyList() {
152171
@Test // gh-252, gh-593
153172
void shouldConvertCollectionOfEnumToString() {
154173

155-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
156-
157174
WithEnumCollections withEnums = new WithEnumCollections();
158175
withEnums.enumSet = EnumSet.of(MyEnum.ONE, MyEnum.TWO);
159176
withEnums.enumList = Arrays.asList(MyEnum.ONE, MyEnum.TWO);
@@ -170,8 +187,6 @@ void shouldConvertCollectionOfEnumToString() {
170187
@Test // gh-593
171188
void shouldCorrectlyWriteConvertedEnumNullValues() {
172189

173-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
174-
175190
WithEnumCollections withEnums = new WithEnumCollections();
176191

177192
OutboundRow outboundRow = strategy.getOutboundRow(withEnums);
@@ -185,8 +200,6 @@ void shouldCorrectlyWriteConvertedEnumNullValues() {
185200
@Test // gh-1544
186201
void shouldCorrectlyWriteConvertedEmptyEnumCollections() {
187202

188-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);
189-
190203
WithEnumCollections withEnums = new WithEnumCollections();
191204
withEnums.enumArray = new MyEnum[0];
192205
withEnums.enumList = Collections.emptyList();
@@ -203,8 +216,7 @@ void shouldCorrectlyWriteConvertedEmptyEnumCollections() {
203216
@Test // gh-593
204217
void shouldConvertCollectionOfEnumNatively() {
205218

206-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
207-
Collections.singletonList(new MyEnumSupport()));
219+
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(new MyEnumSupport());
208220

209221
WithEnumCollections withEnums = new WithEnumCollections();
210222
withEnums.enumSet = EnumSet.of(MyEnum.ONE, MyEnum.TWO);
@@ -222,8 +234,7 @@ void shouldConvertCollectionOfEnumNatively() {
222234
@Test // gh-593
223235
void shouldCorrectlyWriteNativeEnumNullValues() {
224236

225-
DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
226-
Collections.singletonList(new MyEnumSupport()));
237+
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(new MyEnumSupport());
227238

228239
WithEnumCollections withEnums = new WithEnumCollections();
229240

0 commit comments

Comments
 (0)