|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.teleport.v2.templates; |
| 17 | + |
| 18 | +import static org.apache.beam.it.truthmatchers.PipelineAsserts.assertThatResult; |
| 19 | + |
| 20 | +import com.google.cloud.spanner.Struct; |
| 21 | +import com.google.cloud.teleport.metadata.SkipDirectRunnerTest; |
| 22 | +import com.google.cloud.teleport.metadata.TemplateIntegrationTest; |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import org.apache.beam.it.common.PipelineLauncher; |
| 28 | +import org.apache.beam.it.common.PipelineOperator; |
| 29 | +import org.apache.beam.it.common.utils.ResourceManagerUtils; |
| 30 | +import org.apache.beam.it.gcp.spanner.SpannerResourceManager; |
| 31 | +import org.apache.beam.it.gcp.spanner.matchers.SpannerAsserts; |
| 32 | +import org.apache.beam.it.jdbc.PostgresResourceManager; |
| 33 | +import org.junit.AfterClass; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.experimental.categories.Category; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.junit.runners.JUnit4; |
| 39 | + |
| 40 | +/** |
| 41 | + * An integration test for {@link SourceDbToSpanner} Flex template which tests a migration from a |
| 42 | + * PostgreSQL database containing partitioned tables. |
| 43 | + */ |
| 44 | +@Category({TemplateIntegrationTest.class, SkipDirectRunnerTest.class}) |
| 45 | +@TemplateIntegrationTest(SourceDbToSpanner.class) |
| 46 | +@RunWith(JUnit4.class) |
| 47 | +public class PostgreSQLPartitionedTablesIT extends SourceDbToSpannerITBase { |
| 48 | + |
| 49 | + private static boolean initialized = false; |
| 50 | + private static PipelineLauncher.LaunchInfo jobInfo; |
| 51 | + |
| 52 | + public static PostgresResourceManager postgresSQLResourceManager; |
| 53 | + public static SpannerResourceManager gsqlSpannerResourceManager; |
| 54 | + public static SpannerResourceManager pgDialectSpannerResourceManager; |
| 55 | + |
| 56 | + private static final String POSTGRESQL_DDL_RESOURCE = "PartitionedTablesIT/postgresql-schema.sql"; |
| 57 | + private static final String SPANNER_GSQL_DDL_RESOURCE = |
| 58 | + "PartitionedTablesIT/spanner-gsql-schema.sql"; |
| 59 | + private static final String SPANNER_PG_DDL_RESOURCE = "PartitionedTablesIT/spanner-pg-schema.sql"; |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setUp() throws Exception { |
| 63 | + synchronized (PostgreSQLPartitionedTablesIT.class) { |
| 64 | + if (!initialized) { |
| 65 | + postgresSQLResourceManager = setUpPostgreSQLResourceManager(); |
| 66 | + gsqlSpannerResourceManager = setUpSpannerResourceManager(); |
| 67 | + pgDialectSpannerResourceManager = setUpPGDialectSpannerResourceManager(); |
| 68 | + |
| 69 | + loadSQLFileResource(postgresSQLResourceManager, POSTGRESQL_DDL_RESOURCE); |
| 70 | + |
| 71 | + initialized = true; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @AfterClass |
| 77 | + public static void cleanUp() { |
| 78 | + ResourceManagerUtils.cleanResources( |
| 79 | + postgresSQLResourceManager, gsqlSpannerResourceManager, pgDialectSpannerResourceManager); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testPostgreSQLPartitionedTablesGoogleSQLDialect() throws Exception { |
| 84 | + createSpannerDDL(gsqlSpannerResourceManager, SPANNER_GSQL_DDL_RESOURCE); |
| 85 | + jobInfo = |
| 86 | + launchDataflowJob( |
| 87 | + getClass().getSimpleName() + "GSQL", |
| 88 | + null, |
| 89 | + "measurements_range,employees_list,orders_hash", |
| 90 | + postgresSQLResourceManager, |
| 91 | + gsqlSpannerResourceManager, |
| 92 | + new HashMap<>(), |
| 93 | + null); |
| 94 | + |
| 95 | + PipelineOperator.Result result = pipelineOperator().waitUntilDone(createConfig(jobInfo)); |
| 96 | + assertThatResult(result).isLaunchFinished(); |
| 97 | + |
| 98 | + verifyData(gsqlSpannerResourceManager); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testPostgreSQLPartitionedTablesPostgreSQLDialect() throws Exception { |
| 103 | + createSpannerDDL(pgDialectSpannerResourceManager, SPANNER_PG_DDL_RESOURCE); |
| 104 | + jobInfo = |
| 105 | + launchDataflowJob( |
| 106 | + getClass().getSimpleName() + "PG", |
| 107 | + null, |
| 108 | + "measurements_range,employees_list,orders_hash", |
| 109 | + postgresSQLResourceManager, |
| 110 | + pgDialectSpannerResourceManager, |
| 111 | + new HashMap<>(), |
| 112 | + null); |
| 113 | + |
| 114 | + PipelineOperator.Result result = pipelineOperator().waitUntilDone(createConfig(jobInfo)); |
| 115 | + assertThatResult(result).isLaunchFinished(); |
| 116 | + |
| 117 | + verifyData(pgDialectSpannerResourceManager); |
| 118 | + } |
| 119 | + |
| 120 | + private void verifyData(SpannerResourceManager spannerResourceManager) { |
| 121 | + List<Map<String, Object>> measurementsPostgreSQL = |
| 122 | + postgresSQLResourceManager.runSQLQuery( |
| 123 | + "SELECT id, city_id, logdate, peaktemp FROM measurements_range"); |
| 124 | + ImmutableList<Struct> measurementsSpanner = |
| 125 | + spannerResourceManager.readTableRecords( |
| 126 | + "measurements_range", "id", "city_id", "logdate", "peaktemp"); |
| 127 | + SpannerAsserts.assertThatStructs(measurementsSpanner) |
| 128 | + .hasRecordsUnorderedCaseInsensitiveColumns(measurementsPostgreSQL); |
| 129 | + |
| 130 | + List<Map<String, Object>> employeesPostgreSQL = |
| 131 | + postgresSQLResourceManager.runSQLQuery("SELECT id, name, department FROM employees_list"); |
| 132 | + ImmutableList<Struct> employeesSpanner = |
| 133 | + spannerResourceManager.readTableRecords( |
| 134 | + "employees_list", "id", "name", "department"); |
| 135 | + SpannerAsserts.assertThatStructs(employeesSpanner) |
| 136 | + .hasRecordsUnorderedCaseInsensitiveColumns(employeesPostgreSQL); |
| 137 | + |
| 138 | + List<Map<String, Object>> ordersPostgreSQL = |
| 139 | + postgresSQLResourceManager.runSQLQuery( |
| 140 | + "SELECT order_id, customer_id, amount FROM orders_hash"); |
| 141 | + ImmutableList<Struct> ordersSpanner = |
| 142 | + spannerResourceManager.readTableRecords( |
| 143 | + "orders_hash", "order_id", "customer_id", "amount"); |
| 144 | + SpannerAsserts.assertThatStructs(ordersSpanner) |
| 145 | + .hasRecordsUnorderedCaseInsensitiveColumns(ordersPostgreSQL); |
| 146 | + } |
| 147 | +} |
0 commit comments