Skip to content

Commit a76226b

Browse files
authored
Test new tck framework (#270)
Remove TCK test overrides that were necessary due to DML syntax requiring column list in Spanner. R2DBC SPI 0.8.3 containing the TCK changes was released. I also upgraded Reactor to the same version that's in SPI. There are still two reasons for overrides: 1) Spanner returning `Long` even when `Integer` is requested -- work tracked in #276. 2) the driver breaking R2DBC spec by treating columns as case insensitive -- decision pending on #271.
1 parent 367f914 commit a76226b

File tree

4 files changed

+102
-208
lines changed

4 files changed

+102
-208
lines changed

cloud-spanner-r2dbc/src/test/java/com/google/cloud/spanner/r2dbc/it/SpannerClientLibraryTestKit.java

Lines changed: 24 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static com.google.cloud.spanner.r2dbc.SpannerConnectionFactoryProvider.INSTANCE;
2121
import static io.r2dbc.spi.ConnectionFactoryOptions.DATABASE;
2222
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;
2325
import static org.mockito.ArgumentMatchers.any;
2426
import static org.mockito.Mockito.doAnswer;
2527
import static org.mockito.Mockito.mock;
@@ -37,13 +39,11 @@
3739
import io.r2dbc.spi.Result;
3840
import io.r2dbc.spi.Statement;
3941
import io.r2dbc.spi.test.TestKit;
40-
import java.nio.charset.StandardCharsets;
4142
import java.util.Arrays;
4243
import java.util.Collection;
4344
import java.util.Collections;
4445
import java.util.List;
4546
import java.util.function.Function;
46-
import java.util.stream.IntStream;
4747
import org.junit.jupiter.api.AfterEach;
4848
import org.junit.jupiter.api.BeforeAll;
4949
import org.junit.jupiter.api.BeforeEach;
@@ -52,7 +52,6 @@
5252
import org.slf4j.Logger;
5353
import org.slf4j.LoggerFactory;
5454
import org.springframework.jdbc.core.JdbcOperations;
55-
import org.springframework.jdbc.core.PreparedStatementCallback;
5655
import reactor.core.publisher.Flux;
5756
import reactor.core.publisher.Mono;
5857
import reactor.test.StepVerifier;
@@ -85,28 +84,10 @@ static void setUp() {
8584
jdbcOperations = mock(JdbcOperations.class);
8685

8786
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)));
9188
return null;
9289
}).when(jdbcOperations).execute((String) any());
9390

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-
11091
SpannerOptions options = SpannerOptions.newBuilder().build();
11192
Spanner spanner = options.getService();
11293
dbAdminClient = spanner.getDatabaseAdminClient();
@@ -120,7 +101,6 @@ static void setUp() {
120101
createTableIfNeeded(id, "clob_test", " ( value BYTES(MAX) ) PRIMARY KEY (value)");
121102
}
122103

123-
124104
private static void createTableIfNeeded(DatabaseId id, String tableName, String definition) {
125105
Boolean tableExists = Mono.from(connectionFactory.create())
126106
.flatMapMany(c -> c.createStatement(
@@ -227,38 +207,16 @@ public void bindFails() {
227207
*/
228208
}
229209

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-
252210
// override. column names are case-sensitive in Spanner.
253211
@Override
254212
@Test
255213
public void duplicateColumnNames() {
256-
getJdbcOperations().execute("INSERT INTO test_two_column VALUES (100, 'hello')");
214+
getJdbcOperations().execute(expand(INSERT_TWO_COLUMNS));
257215

258216
Mono.from(getConnectionFactory().create())
259217
.flatMapMany(connection -> Flux.from(connection
260218

261-
.createStatement("SELECT col1 AS value, col2 AS VALUE FROM test_two_column")
219+
.createStatement(expand(SELECT_VALUE_TWO_COLUMNS))
262220
.execute())
263221

264222
.flatMap(result -> result
@@ -275,9 +233,8 @@ public void duplicateColumnNames() {
275233
// override. column names are case-sensitive in Spanner.
276234
@Override
277235
@Test
278-
@Disabled // TODO: GH-252
279236
public void columnMetadata() {
280-
getJdbcOperations().execute("INSERT INTO test_two_column VALUES (100, 'hello')");
237+
getJdbcOperations().execute(expand(INSERT_TWO_COLUMNS));
281238

282239
Mono.from(getConnectionFactory().create())
283240
.flatMapMany(connection -> Flux.from(connection
@@ -393,28 +350,28 @@ public void prepareStatementWithIncompleteBatchFails() {
393350
*/
394351
}
395352

396-
// DML syntax needed to be fixed.
353+
// Returns Long instead of Integer. Equivalent test in V1 did not need an override.
397354
@Override
398355
@Test
399356
public void transactionCommit() {
400-
executeDml(c -> c.createStatement("INSERT INTO test (value) VALUES (100)"));
357+
executeDml(c -> c.createStatement(expand(TestStatement.INSERT_VALUE100)));
401358

402359
Mono.from(getConnectionFactory().create())
403360
.<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))
407364

408365
// NOTE: this defer is a from() in the original. needs a follow up to resolve
409366
.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))))
411368
.bind(getIdentifier(0), 200)
412369
.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))
415372
.execute())
416373
.flatMap(this::extractColumnsLong))
417-
.concatWith(Flux.from(connection.createStatement("SELECT value FROM test")
374+
.concatWith(Flux.from(connection.createStatement(expand(TestStatement.SELECT_VALUE))
418375
.execute())
419376
.flatMap(this::extractColumnsLong))
420377
.concatWith(close(connection)))
@@ -443,25 +400,6 @@ Compound statements (statements with more than 1 semi-colon) are not supported.
443400
*/
444401
}
445402

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-
465403
@Override
466404
@Test
467405
@Disabled // TODO: GH-275
@@ -471,11 +409,11 @@ public void changeAutoCommitCommitsTransaction() {
471409
Flux.from(connection.setAutoCommit(false))
472410
.thenMany(connection.beginTransaction())
473411
// 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())
476414
.flatMap(Result::getRowsUpdated)
477415
.thenMany(connection.setAutoCommit(true))
478-
.thenMany(connection.createStatement("SELECT value FROM test").execute())
416+
.thenMany(connection.createStatement(expand(TestStatement.SELECT_VALUE)).execute())
479417
.flatMap(it -> it.map((row, metadata) -> row.get("value")))
480418
.concatWith(close(connection))
481419
)
@@ -494,8 +432,8 @@ public void sameAutoCommitLeavesTransactionUnchanged() {
494432
.flatMapMany(connection ->
495433
Flux.from(connection.setAutoCommit(false))
496434
.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())
499437
.flatMap(Result::getRowsUpdated)
500438
.thenMany(connection.setAutoCommit(false))
501439
.thenMany(connection.rollbackTransaction())
@@ -513,4 +451,8 @@ public void autoCommitByDefault() {
513451

514452
}
515453

454+
@Override
455+
public String expand(TestStatement statement, Object... args) {
456+
return SpannerTestKitStatements.expand(statement, args);
457+
}
516458
}

0 commit comments

Comments
 (0)