Search in sources :

Example 21 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project java-firestore by googleapis.

the class FirestoreClientTest method beginTransactionTest.

@Test
public void beginTransactionTest() throws Exception {
    BeginTransactionResponse expectedResponse = BeginTransactionResponse.newBuilder().setTransaction(ByteString.EMPTY).build();
    mockFirestore.addResponse(expectedResponse);
    String database = "database1789464955";
    BeginTransactionResponse actualResponse = client.beginTransaction(database);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockFirestore.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BeginTransactionRequest actualRequest = ((BeginTransactionRequest) actualRequests.get(0));
    Assert.assertEquals(database, actualRequest.getDatabase());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) ByteString(com.google.protobuf.ByteString) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Test(org.junit.Test)

Example 22 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method runAsyncStackTraceCaptureTest.

private void runAsyncStackTraceCaptureTest(boolean asyncStackTraceCaptureEnabled) throws Exception {
    DatastoreServiceGlobalConfig.setConfig(DatastoreServiceGlobalConfig.builder().appId(APP_ID).emulatorHost("dummy-value-to-stub-out-credentials").asyncStackTraceCaptureEnabled(asyncStackTraceCaptureEnabled).build());
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 1);
    DatastoreException lastException = nonRetryableException();
    when(datastore.beginTransaction(req)).thenThrow(lastException);
    try {
        client.beginTransaction(req).get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
        if (asyncStackTraceCaptureEnabled) {
            assertThat(e).hasMessageThat().contains("stack trace when async call was initiated");
        } else {
            assertThat(e).hasMessageThat().contains("(stack trace capture for async call is disabled)");
        }
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method testRetries_NonRetryable.

@Test
public void testRetries_NonRetryable() throws Exception {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
    DatastoreException lastException = nonRetryableException();
    when(datastore.beginTransaction(req)).thenThrow(lastException);
    try {
        client.beginTransaction(req).get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 24 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project liquibase-spanner by cloudspannerecosystem.

the class MergeColumnsTest method testMergeSingersFirstNamdAndLastNameFromYaml.

@Test
void testMergeSingersFirstNamdAndLastNameFromYaml() throws Exception {
    String[] expectedSql = new String[] { "ALTER TABLE Singers ADD COLUMN FullName STRING(500)", "ALTER TABLE Singers DROP COLUMN FirstName", "ALTER TABLE Singers DROP COLUMN LastName" };
    for (String sql : expectedSql) {
        addUpdateDdlStatementsResponse(sql);
    }
    for (String file : new String[] { "merge-singers-firstname-and-lastname.spanner.yaml" }) {
        try (Connection con = createConnection();
            Liquibase liquibase = getLiquibase(con, file)) {
            liquibase.update(new Contexts("test"));
        }
    }
    assertThat(mockAdmin.getRequests()).hasSize(expectedSql.length);
    for (int i = 0; i < expectedSql.length; i++) {
        assertThat(mockAdmin.getRequests().get(i)).isInstanceOf(UpdateDatabaseDdlRequest.class);
        UpdateDatabaseDdlRequest request = (UpdateDatabaseDdlRequest) mockAdmin.getRequests().get(i);
        assertThat(request.getStatementsList()).hasSize(1);
        assertThat(request.getStatementsList().get(0)).isEqualTo(expectedSql[i]);
    }
    // Verify that the mock server received one BeginTransactionRequest for a PDML transaction.
    assertThat(mockSpanner.getRequests().stream().filter(new Predicate<AbstractMessage>() {

        @Override
        public boolean test(AbstractMessage t) {
            return t instanceof BeginTransactionRequest;
        }
    }).map(new Function<AbstractMessage, BeginTransactionRequest>() {

        @Override
        public BeginTransactionRequest apply(AbstractMessage t) {
            return (BeginTransactionRequest) t;
        }
    }).filter(new Predicate<BeginTransactionRequest>() {

        @Override
        public boolean test(BeginTransactionRequest t) {
            return t.hasOptions() && t.getOptions().hasPartitionedDml();
        }
    }).count()).isEqualTo(1L);
}
Also used : Liquibase(liquibase.Liquibase) AbstractMessage(com.google.protobuf.AbstractMessage) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) Contexts(liquibase.Contexts) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Predicate(java.util.function.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.Test)13 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)11 Transaction (com.google.spanner.v1.Transaction)11 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)5 ByteString (com.google.protobuf.ByteString)5 ExecutionException (java.util.concurrent.ExecutionException)5 DatastoreException (com.google.datastore.v1.client.DatastoreException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 Session (com.google.spanner.v1.Session)4 Connection (java.sql.Connection)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)3 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)2 CommitRequest (com.google.firestore.v1.CommitRequest)2