|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner; |
| 18 | + |
| 19 | +import static com.example.spanner.SampleRunner.runSample; |
| 20 | +import static com.google.common.truth.Truth.assertThat; |
| 21 | + |
| 22 | +import com.google.api.gax.longrunning.OperationFuture; |
| 23 | +import com.google.cloud.spanner.DatabaseClient; |
| 24 | +import com.google.cloud.spanner.DatabaseId; |
| 25 | +import com.google.cloud.spanner.Dialect; |
| 26 | +import com.google.cloud.spanner.KeySet; |
| 27 | +import com.google.cloud.spanner.Mutation; |
| 28 | +import com.google.common.collect.ImmutableList; |
| 29 | +import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.junit.runners.JUnit4; |
| 37 | + |
| 38 | +/** Integration tests for {@link PgLastStatementSample} */ |
| 39 | +@RunWith(JUnit4.class) |
| 40 | +public class PgLastStatementSampleIT extends SampleTestBase { |
| 41 | + |
| 42 | + private static DatabaseId databaseId; |
| 43 | + |
| 44 | + @BeforeClass |
| 45 | + public static void createTestDatabase() throws Exception { |
| 46 | + final String database = idGenerator.generateDatabaseId(); |
| 47 | + databaseAdminClient |
| 48 | + .createDatabase( |
| 49 | + databaseAdminClient |
| 50 | + .newDatabaseBuilder(DatabaseId.of(projectId, instanceId, database)) |
| 51 | + .setDialect(Dialect.POSTGRESQL) |
| 52 | + .build(), |
| 53 | + Collections.emptyList()) |
| 54 | + .get(10, TimeUnit.MINUTES); |
| 55 | + final OperationFuture<Void, UpdateDatabaseDdlMetadata> updateOperation = |
| 56 | + databaseAdminClient.updateDatabaseDdl( |
| 57 | + instanceId, |
| 58 | + database, |
| 59 | + ImmutableList.of( |
| 60 | + "CREATE TABLE Singers (" |
| 61 | + + " SingerId bigint NOT NULL," |
| 62 | + + " FirstName character varying(1024)," |
| 63 | + + " LastName character varying(1024)," |
| 64 | + + " PRIMARY KEY (SingerId)" |
| 65 | + + ")"), |
| 66 | + null); |
| 67 | + updateOperation.get(10, TimeUnit.MINUTES); |
| 68 | + databaseId = DatabaseId.of(projectId, instanceId, database); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testSetLastStatementOptionSample() throws Exception { |
| 73 | + final DatabaseClient client = spanner.getDatabaseClient(databaseId); |
| 74 | + String out = |
| 75 | + runSample( |
| 76 | + () -> PgLastStatementSample.insertAndUpdateUsingLastStatement(client)); |
| 77 | + assertThat(out).contains("New singer inserted."); |
| 78 | + assertThat(out).contains("Singer last name updated."); |
| 79 | + } |
| 80 | +} |
0 commit comments