20
20
import static com .google .cloud .spanner .r2dbc .SpannerConnectionFactoryProvider .INSTANCE ;
21
21
import static io .r2dbc .spi .ConnectionFactoryOptions .DATABASE ;
22
22
import static io .r2dbc .spi .ConnectionFactoryOptions .DRIVER ;
23
+ import static io .r2dbc .spi .test .TestKit .TestStatement .INSERT_TWO_COLUMNS ;
24
+ import static io .r2dbc .spi .test .TestKit .TestStatement .SELECT_VALUE_TWO_COLUMNS ;
23
25
import static org .mockito .ArgumentMatchers .any ;
24
26
import static org .mockito .Mockito .doAnswer ;
25
27
import static org .mockito .Mockito .mock ;
37
39
import io .r2dbc .spi .Result ;
38
40
import io .r2dbc .spi .Statement ;
39
41
import io .r2dbc .spi .test .TestKit ;
40
- import java .nio .charset .StandardCharsets ;
41
42
import java .util .Arrays ;
42
43
import java .util .Collection ;
43
44
import java .util .Collections ;
44
45
import java .util .List ;
45
46
import java .util .function .Function ;
46
- import java .util .stream .IntStream ;
47
47
import org .junit .jupiter .api .AfterEach ;
48
48
import org .junit .jupiter .api .BeforeAll ;
49
49
import org .junit .jupiter .api .BeforeEach ;
52
52
import org .slf4j .Logger ;
53
53
import org .slf4j .LoggerFactory ;
54
54
import org .springframework .jdbc .core .JdbcOperations ;
55
- import org .springframework .jdbc .core .PreparedStatementCallback ;
56
55
import reactor .core .publisher .Flux ;
57
56
import reactor .core .publisher .Mono ;
58
57
import reactor .test .StepVerifier ;
@@ -85,28 +84,10 @@ static void setUp() {
85
84
jdbcOperations = mock (JdbcOperations .class );
86
85
87
86
doAnswer (invocation -> {
88
- String query = invocation .getArgument (0 );
89
- executeDml (c -> c .createStatement (query .replace ("INTO test " , "INTO test (value) " )
90
- .replace ("INTO test_two_column" , "INTO test_two_column (col1,col2)" )));
87
+ executeDml (c -> c .createStatement (invocation .getArgument (0 )));
91
88
return null ;
92
89
}).when (jdbcOperations ).execute ((String ) any ());
93
90
94
- doAnswer (invocation -> {
95
- String query = invocation .getArgument (0 );
96
-
97
- // The TCK uses java.sql JDBC classes that we have no implemented, but only in two cases
98
- // that we can detect and substitute here.
99
- if (query .equalsIgnoreCase ("INSERT INTO clob_test VALUES (?)" )) {
100
- executeDml (c -> c .createStatement ("INSERT INTO clob_test (value) VALUES (@val)" )
101
- .bind ("val" , "test-value" ));
102
- } else if (query .equalsIgnoreCase ("INSERT INTO blob_test VALUES (?)" )) {
103
- executeDml (c -> c .createStatement ("INSERT INTO blob_test (value) VALUES (@val)" ).bind ("val" ,
104
- StandardCharsets .UTF_8 .encode ("test-value" ).array ()));
105
- }
106
-
107
- return null ;
108
- }).when (jdbcOperations ).execute ((String ) any (), (PreparedStatementCallback ) any ());
109
-
110
91
SpannerOptions options = SpannerOptions .newBuilder ().build ();
111
92
Spanner spanner = options .getService ();
112
93
dbAdminClient = spanner .getDatabaseAdminClient ();
@@ -120,7 +101,6 @@ static void setUp() {
120
101
createTableIfNeeded (id , "clob_test" , " ( value BYTES(MAX) ) PRIMARY KEY (value)" );
121
102
}
122
103
123
-
124
104
private static void createTableIfNeeded (DatabaseId id , String tableName , String definition ) {
125
105
Boolean tableExists = Mono .from (connectionFactory .create ())
126
106
.flatMapMany (c -> c .createStatement (
@@ -227,38 +207,16 @@ public void bindFails() {
227
207
*/
228
208
}
229
209
230
- // override to fix DDL for Spanner.
231
- @ Override
232
- @ Test
233
- public void prepareStatement () {
234
- Mono .from (getConnectionFactory ().create ())
235
- .delayUntil (c -> c .beginTransaction ())
236
- .flatMapMany (connection -> {
237
- Statement statement = connection .createStatement (
238
- String .format ("INSERT INTO test (value) VALUES(%s)" , getPlaceholder (0 )));
239
-
240
- IntStream .range (0 , 10 )
241
- .forEach (i -> statement .bind (getIdentifier (0 ), i ).add ());
242
-
243
- return Flux .from (statement
244
- .execute ())
245
- .concatWith (close (connection ));
246
- })
247
- .as (StepVerifier ::create )
248
- .expectNextCount (10 ).as ("values from insertions" )
249
- .verifyComplete ();
250
- }
251
-
252
210
// override. column names are case-sensitive in Spanner.
253
211
@ Override
254
212
@ Test
255
213
public void duplicateColumnNames () {
256
- getJdbcOperations ().execute ("INSERT INTO test_two_column VALUES (100, 'hello')" );
214
+ getJdbcOperations ().execute (expand ( INSERT_TWO_COLUMNS ) );
257
215
258
216
Mono .from (getConnectionFactory ().create ())
259
217
.flatMapMany (connection -> Flux .from (connection
260
218
261
- .createStatement ("SELECT col1 AS value, col2 AS VALUE FROM test_two_column" )
219
+ .createStatement (expand ( SELECT_VALUE_TWO_COLUMNS ) )
262
220
.execute ())
263
221
264
222
.flatMap (result -> result
@@ -275,9 +233,8 @@ public void duplicateColumnNames() {
275
233
// override. column names are case-sensitive in Spanner.
276
234
@ Override
277
235
@ Test
278
- @ Disabled // TODO: GH-252
279
236
public void columnMetadata () {
280
- getJdbcOperations ().execute ("INSERT INTO test_two_column VALUES (100, 'hello')" );
237
+ getJdbcOperations ().execute (expand ( INSERT_TWO_COLUMNS ) );
281
238
282
239
Mono .from (getConnectionFactory ().create ())
283
240
.flatMapMany (connection -> Flux .from (connection
@@ -393,28 +350,28 @@ public void prepareStatementWithIncompleteBatchFails() {
393
350
*/
394
351
}
395
352
396
- // DML syntax needed to be fixed .
353
+ // Returns Long instead of Integer. Equivalent test in V1 did not need an override .
397
354
@ Override
398
355
@ Test
399
356
public void transactionCommit () {
400
- executeDml (c -> c .createStatement ("INSERT INTO test (value) VALUES (100)" ));
357
+ executeDml (c -> c .createStatement (expand ( TestStatement . INSERT_VALUE100 ) ));
401
358
402
359
Mono .from (getConnectionFactory ().create ())
403
360
.<Object >flatMapMany (connection -> Mono .from (connection .beginTransaction ())
404
- .<Object >thenMany (Flux . from ( connection . createStatement ( "SELECT value FROM test" )
405
- .execute ())
406
- .flatMap (this ::extractColumnsLong ))
361
+ .<Object >thenMany (
362
+ Flux . from ( connection . createStatement ( expand ( TestStatement . SELECT_VALUE )) .execute ())
363
+ .flatMap (this ::extractColumnsLong ))
407
364
408
365
// NOTE: this defer is a from() in the original. needs a follow up to resolve
409
366
.concatWith (Flux .from (connection .createStatement (
410
- String .format ("INSERT INTO test (value) VALUES (%s)" , getPlaceholder (0 )))
367
+ String .format (expand ( TestStatement . INSERT_VALUE_PLACEHOLDER , getPlaceholder (0 ) )))
411
368
.bind (getIdentifier (0 ), 200 )
412
369
.execute ())
413
- .flatMap (TestKit ::extractRowsUpdated ))
414
- .concatWith (Flux .from (connection .createStatement ("SELECT value FROM test" )
370
+ .flatMap (this ::extractRowsUpdated ))
371
+ .concatWith (Flux .from (connection .createStatement (expand ( TestStatement . SELECT_VALUE ) )
415
372
.execute ())
416
373
.flatMap (this ::extractColumnsLong ))
417
- .concatWith (Flux .from (connection .createStatement ("SELECT value FROM test" )
374
+ .concatWith (Flux .from (connection .createStatement (expand ( TestStatement . SELECT_VALUE ) )
418
375
.execute ())
419
376
.flatMap (this ::extractColumnsLong ))
420
377
.concatWith (close (connection )))
@@ -443,25 +400,6 @@ Compound statements (statements with more than 1 semi-colon) are not supported.
443
400
*/
444
401
}
445
402
446
- // DML syntax fix.
447
- @ Override
448
- @ Test
449
- public void bindNull () {
450
- Mono .from (getConnectionFactory ().create ())
451
- .delayUntil (c -> c .beginTransaction ())
452
- .flatMapMany (connection -> Flux .from (connection
453
-
454
- .createStatement (
455
- String .format ("INSERT INTO test (value) VALUES(%s)" , getPlaceholder (0 )))
456
- .bindNull (getIdentifier (0 ), Integer .class ).add ()
457
- .execute ())
458
-
459
- .concatWith (close (connection )))
460
- .as (StepVerifier ::create )
461
- .expectNextCount (1 ).as ("rows inserted" )
462
- .verifyComplete ();
463
- }
464
-
465
403
@ Override
466
404
@ Test
467
405
@ Disabled // TODO: GH-275
@@ -471,11 +409,11 @@ public void changeAutoCommitCommitsTransaction() {
471
409
Flux .from (connection .setAutoCommit (false ))
472
410
.thenMany (connection .beginTransaction ())
473
411
// DML syntax fix adding column list
474
- .thenMany (connection . createStatement (
475
- "INSERT INTO test (value) VALUES(200)" ).execute ())
412
+ .thenMany (
413
+ connection . createStatement ( expand ( TestStatement . INSERT_VALUE200 ) ).execute ())
476
414
.flatMap (Result ::getRowsUpdated )
477
415
.thenMany (connection .setAutoCommit (true ))
478
- .thenMany (connection .createStatement ("SELECT value FROM test" ).execute ())
416
+ .thenMany (connection .createStatement (expand ( TestStatement . SELECT_VALUE ) ).execute ())
479
417
.flatMap (it -> it .map ((row , metadata ) -> row .get ("value" )))
480
418
.concatWith (close (connection ))
481
419
)
@@ -494,8 +432,8 @@ public void sameAutoCommitLeavesTransactionUnchanged() {
494
432
.flatMapMany (connection ->
495
433
Flux .from (connection .setAutoCommit (false ))
496
434
.thenMany (connection .beginTransaction ())
497
- .thenMany (connection . createStatement (
498
- "INSERT INTO test (value) VALUES(200)" ).execute ())
435
+ .thenMany (
436
+ connection . createStatement ( expand ( TestStatement . INSERT_VALUE200 ) ).execute ())
499
437
.flatMap (Result ::getRowsUpdated )
500
438
.thenMany (connection .setAutoCommit (false ))
501
439
.thenMany (connection .rollbackTransaction ())
@@ -513,4 +451,8 @@ public void autoCommitByDefault() {
513
451
514
452
}
515
453
454
+ @ Override
455
+ public String expand (TestStatement statement , Object ... args ) {
456
+ return SpannerTestKitStatements .expand (statement , args );
457
+ }
516
458
}
0 commit comments