Skip to content

Commit d132138

Browse files
committed
test: autocommit_dml_mode with PG
1 parent d2f7f06 commit d132138

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/test/java/liquibase/ext/spanner/AbstractMockServerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,19 @@ protected static Connection createConnection() throws SQLException {
452452
return DriverManager.getConnection(createConnectionUrl());
453453
}
454454

455+
protected static Connection createConnection(String database) throws SQLException {
456+
return DriverManager.getConnection(createConnectionUrl(database));
457+
}
458+
455459
protected static String createConnectionUrl() {
460+
return createConnectionUrl("/projects/p/instances/i/databases/d");
461+
}
462+
463+
protected static String createConnectionUrl(String database) {
456464
return new StringBuilder("jdbc:cloudspanner://localhost:")
457465
.append(server.getPort())
458-
.append("/projects/p/instances/i/databases/d;usePlainText=true;minSessions=0")
466+
.append(database)
467+
.append(";usePlainText=true;minSessions=0")
459468
.toString();
460469
}
461470
}

src/test/java/liquibase/ext/spanner/SimpleMockServerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import static com.google.common.truth.Truth.assertThat;
44

5+
import com.google.cloud.spanner.Dialect;
56
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
7+
import com.google.cloud.spanner.connection.SpannerPool;
68
import com.google.common.collect.ImmutableList;
79
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest;
10+
import com.google.spanner.v1.BeginTransactionRequest;
11+
import com.google.spanner.v1.ExecuteSqlRequest;
812
import java.sql.Connection;
913
import java.sql.SQLException;
14+
import java.sql.Statement;
1015
import liquibase.Liquibase;
1116
import org.junit.jupiter.api.Test;
1217
import org.junit.jupiter.api.parallel.Execution;
@@ -34,6 +39,29 @@ void testCreateTableStatement() throws SQLException {
3439
receivedRequestWithNonLiquibaseToken.set(false);
3540
}
3641

42+
43+
@Test
44+
void testAutocommitDmlMode() throws SQLException {
45+
mockSpanner.putStatementResult(StatementResult.update(com.google.cloud.spanner.Statement.of("update foo set bar=1 where true"), 10000L));
46+
47+
// Configure the mock server to return POSTGRESQL as the dialect that is used by the database.
48+
mockSpanner.putStatementResult(StatementResult.detectDialectResult(Dialect.POSTGRESQL));
49+
50+
try (Connection con = createConnection("/projects/p/instances/i/databases/pg_db"); Statement stmt = con.createStatement()) {
51+
stmt.execute("set spanner.autocommit_dml_mode='partitioned_non_atomic'");
52+
stmt.execute("update foo set bar=1 where true");
53+
}
54+
assertThat(mockSpanner.getRequestsOfType(BeginTransactionRequest.class)).hasSize(1);
55+
BeginTransactionRequest request = mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0);
56+
assertThat(request.getOptions().hasPartitionedDml()).isTrue();
57+
58+
// This test does not use Liquibase but JDBC directly, so it is expected to send requests that
59+
// do not contain a Liquibase client lib token.
60+
assertThat(receivedRequestWithNonLiquibaseToken.get()).isTrue();
61+
// Clear the flag to prevent the check after each test to fail.
62+
receivedRequestWithNonLiquibaseToken.set(false);
63+
}
64+
3765
@Test
3866
void testInitLiquibase() throws Exception {
3967
mockSpanner.putStatementResult(

0 commit comments

Comments
 (0)