|
| 1 | +/* |
| 2 | + * Copyright 2024 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.cloud.spanner.DatabaseClient; |
| 23 | +import com.google.cloud.spanner.DatabaseId; |
| 24 | +import com.google.cloud.spanner.KeySet; |
| 25 | +import com.google.cloud.spanner.Mutation; |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collections; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.junit.runners.JUnit4; |
| 35 | + |
| 36 | +/** Integration tests for {@link ChangeStreamsTxnExclusionSample} */ |
| 37 | +@RunWith(JUnit4.class) |
| 38 | +public class ChangeStreamsTxnExclusionSampleIT extends SampleTestBase { |
| 39 | + |
| 40 | + private static DatabaseId databaseId; |
| 41 | + |
| 42 | + @BeforeClass |
| 43 | + public static void createTestDatabase() throws Exception { |
| 44 | + final String database = idGenerator.generateDatabaseId(); |
| 45 | + databaseAdminClient |
| 46 | + .createDatabase( |
| 47 | + instanceId, |
| 48 | + database, |
| 49 | + ImmutableList.of( |
| 50 | + "CREATE TABLE Singers (" |
| 51 | + + " SingerId INT64 NOT NULL," |
| 52 | + + " FirstName STRING(1024)," |
| 53 | + + " LastName STRING(1024)," |
| 54 | + + " SingerInfo BYTES(MAX)" |
| 55 | + + ") PRIMARY KEY (SingerId)")) |
| 56 | + .get(); |
| 57 | + databaseId = DatabaseId.of(projectId, instanceId, database); |
| 58 | + } |
| 59 | + |
| 60 | + @Before |
| 61 | + public void insertTestData() { |
| 62 | + final DatabaseClient client = spanner.getDatabaseClient(databaseId); |
| 63 | + client.write( |
| 64 | + Arrays.asList( |
| 65 | + Mutation.newInsertBuilder("Singers") |
| 66 | + .set("SingerId") |
| 67 | + .to(1L) |
| 68 | + .set("FirstName") |
| 69 | + .to("first name 1") |
| 70 | + .set("LastName") |
| 71 | + .to("last name 1") |
| 72 | + .build(), |
| 73 | + Mutation.newInsertBuilder("Singers") |
| 74 | + .set("SingerId") |
| 75 | + .to(2L) |
| 76 | + .set("FirstName") |
| 77 | + .to("first name 2") |
| 78 | + .set("LastName") |
| 79 | + .to("last name 2") |
| 80 | + .build())); |
| 81 | + } |
| 82 | + |
| 83 | + @After |
| 84 | + public void removeTestData() { |
| 85 | + final DatabaseClient client = spanner.getDatabaseClient(databaseId); |
| 86 | + client.write(Collections.singletonList(Mutation.delete("Singers", KeySet.all()))); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testSetExcludeTxnFromChangeStreamsSampleSample() throws Exception { |
| 91 | + final DatabaseClient client = spanner.getDatabaseClient(databaseId); |
| 92 | + String out = |
| 93 | + runSample(() -> ChangeStreamsTxnExclusionSample.rwTxnExcludedFromChangeStreams(client)); |
| 94 | + assertThat(out).contains("New singer inserted."); |
| 95 | + assertThat(out).contains("Singer first name updated."); |
| 96 | + } |
| 97 | +} |
0 commit comments