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_BLOB_VALUE_PLACEHOLDER ;
24
+ import static io .r2dbc .spi .test .TestKit .TestStatement .INSERT_CLOB_VALUE_PLACEHOLDER ;
23
25
import static io .r2dbc .spi .test .TestKit .TestStatement .INSERT_TWO_COLUMNS ;
24
26
import static io .r2dbc .spi .test .TestKit .TestStatement .INSERT_VALUE100 ;
25
27
import static io .r2dbc .spi .test .TestKit .TestStatement .INSERT_VALUE200 ;
26
28
import static io .r2dbc .spi .test .TestKit .TestStatement .INSERT_VALUE_PLACEHOLDER ;
29
+ import static io .r2dbc .spi .test .TestKit .TestStatement .SELECT_VALUE ;
27
30
import static io .r2dbc .spi .test .TestKit .TestStatement .SELECT_VALUE_TWO_COLUMNS ;
28
31
import static org .mockito .ArgumentMatchers .any ;
29
- import static org .mockito .ArgumentMatchers .anyString ;
30
32
import static org .mockito .Mockito .doAnswer ;
31
33
import static org .mockito .Mockito .mock ;
32
- import static org .mockito .Mockito .when ;
33
34
34
35
import com .google .cloud .ServiceOptions ;
35
36
import com .google .cloud .spanner .DatabaseAdminClient ;
44
45
import io .r2dbc .spi .Result ;
45
46
import io .r2dbc .spi .Statement ;
46
47
import io .r2dbc .spi .test .TestKit ;
47
- import java .nio .charset .StandardCharsets ;
48
48
import java .util .Arrays ;
49
49
import java .util .Collection ;
50
50
import java .util .Collections ;
51
51
import java .util .HashMap ;
52
52
import java .util .Map ;
53
53
import java .util .function .Function ;
54
- import java .util .stream .IntStream ;
55
54
import org .junit .jupiter .api .AfterEach ;
56
55
import org .junit .jupiter .api .BeforeAll ;
57
56
import org .junit .jupiter .api .BeforeEach ;
60
59
import org .slf4j .Logger ;
61
60
import org .slf4j .LoggerFactory ;
62
61
import org .springframework .jdbc .core .JdbcOperations ;
63
- import org .springframework .jdbc .core .PreparedStatementCallback ;
64
62
import reactor .core .publisher .Flux ;
65
63
import reactor .core .publisher .Mono ;
66
64
import reactor .test .StepVerifier ;
67
65
68
66
/**
69
- * R2DBC TCK test implementation.
67
+ * Cloud Spanner R2DBC TCK test implementation.
70
68
*/
71
69
public class SpannerTestKit implements TestKit <String > {
72
70
@@ -77,6 +75,10 @@ public class SpannerTestKit implements TestKit<String> {
77
75
STATEMENTS .put (INSERT_VALUE200 , args -> "INSERT INTO test (value) VALUES (200)" );
78
76
STATEMENTS .put (INSERT_TWO_COLUMNS ,
79
77
args -> "INSERT INTO test_two_column (col1,col2) VALUES (100, 'hello')" );
78
+ STATEMENTS .put (INSERT_BLOB_VALUE_PLACEHOLDER , args ->
79
+ String .format ("INSERT INTO blob_test VALUES (?)" , args ));
80
+ STATEMENTS .put (INSERT_CLOB_VALUE_PLACEHOLDER , args ->
81
+ String .format ("INSERT INTO clob_test VALUES (?)" , args ));
80
82
STATEMENTS .put (INSERT_VALUE_PLACEHOLDER ,
81
83
args -> String .format ("INSERT INTO test (value) VALUES (%s)" , args ));
82
84
@@ -110,22 +112,6 @@ static void setUp() {
110
112
return null ;
111
113
}).when (jdbcOperations ).execute ((String ) any ());
112
114
113
- doAnswer (invocation -> {
114
- String query = invocation .getArgument (0 );
115
-
116
- // The TCK uses java.sql JDBC classes that we have no implemented, but only in two cases
117
- // that we can detect and substitute here.
118
- if (query .equalsIgnoreCase ("INSERT INTO clob_test VALUES (?)" )) {
119
- executeDml (c -> c .createStatement ("INSERT INTO clob_test (value) VALUES (@val)" )
120
- .bind ("val" , "test-value" ));
121
- } else if (query .equalsIgnoreCase ("INSERT INTO blob_test VALUES (?)" )) {
122
- executeDml (c -> c .createStatement ("INSERT INTO blob_test (value) VALUES (@val)" ).bind ("val" ,
123
- StandardCharsets .UTF_8 .encode ("test-value" ).array ()));
124
- }
125
-
126
- return null ;
127
- }).when (jdbcOperations ).execute ((String ) any (), (PreparedStatementCallback ) any ());
128
-
129
115
SpannerOptions options = SpannerOptions .newBuilder ().build ();
130
116
Spanner spanner = options .getService ();
131
117
dbAdminClient = spanner .getDatabaseAdminClient ();
@@ -246,31 +232,9 @@ public void bindFails() {
246
232
*/
247
233
}
248
234
249
- // override to fix DDL for Spanner.
250
- @ Override
251
- @ Test
252
- public void prepareStatement () {
253
- Mono .from (getConnectionFactory ().create ())
254
- .delayUntil (c -> c .beginTransaction ())
255
- .flatMapMany (connection -> {
256
- Statement statement = connection .createStatement (
257
- String .format ("INSERT INTO test (value) VALUES(%s)" , getPlaceholder (0 )));
258
-
259
- IntStream .range (0 , 10 )
260
- .forEach (i -> statement .bind (getIdentifier (0 ), i ).add ());
261
-
262
- return Flux .from (statement
263
- .execute ())
264
- .concatWith (close (connection ));
265
- })
266
- .as (StepVerifier ::create )
267
- .expectNextCount (10 ).as ("values from insertions" )
268
- .verifyComplete ();
269
- }
270
-
271
235
/* Overrides parent test because
272
236
* 1) column names are case-sensitive in Spanner
273
- * 2) Spanner has Long instead of Integer
237
+ * 2) Spanner returns Long instead of Integer
274
238
*/
275
239
@ Override
276
240
@ Test
@@ -423,6 +387,7 @@ Compound statements (statements with more than 1 semi-colon) are not supported.
423
387
*/
424
388
}
425
389
390
+ /* Overrides parent test because Spanner returns Long instead of Integer */
426
391
@ Override
427
392
@ Test
428
393
public void changeAutoCommitCommitsTransaction () {
@@ -433,7 +398,7 @@ public void changeAutoCommitCommitsTransaction() {
433
398
.thenMany (connection .createStatement (expand (INSERT_VALUE200 )).execute ())
434
399
.flatMap (Result ::getRowsUpdated )
435
400
.thenMany (connection .setAutoCommit (true ))
436
- .thenMany (connection .createStatement ("SELECT value FROM test" ).execute ())
401
+ .thenMany (connection .createStatement (expand ( SELECT_VALUE ) ).execute ())
437
402
.flatMap (it -> it .map ((row , metadata ) -> row .get ("value" )))
438
403
.concatWith (close (connection ))
439
404
)
0 commit comments