liferoad commented on PR #36605: URL: https://github.com/apache/beam/pull/36605#issuecomment-3439394850
can we add a test to cover this? for example: ``` # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import datetime import os import secrets import time import unittest import uuid import pytest import apache_beam as beam from apache_beam.io.gcp.bigquery_tools import BigQueryWrapper from apache_beam.io.gcp.bigquery_tools import beam_row_from_dict from apache_beam.io.gcp.bigquery_tools import ( get_beam_typehints_from_tableschema) from apache_beam.testing.test_pipeline import TestPipeline from apache_beam.transforms import managed from apache_beam.typehints.row_type import RowTypeConstraint from apache_beam.utils.timestamp import Timestamp @pytest.mark.uses_io_java_expansion_service @unittest.skipUnless( os.environ.get('EXPANSION_JARS'), "EXPANSION_JARS environment var is not provided, " "indicating that jars have not been built") class ManagedBigQueryIT(unittest.TestCase): BIG_QUERY_DATASET_ID = 'python_managed_bq_' def setUp(self): self.test_pipeline = TestPipeline(is_integration_test=True) self.project = self.test_pipeline.get_option('project') self.args = self.test_pipeline.get_full_options_as_args() self.args.extend([ '--experiments=enable_managed_transforms', ]) # Create a unique dataset for this test self.dataset_id = '%s%d%s' % ( self.BIG_QUERY_DATASET_ID, int(time.time()), secrets.token_hex(3)) self.bigquery_client = BigQueryWrapper() self.bigquery_client.get_or_create_dataset(self.project, self.dataset_id) def tearDown(self): # Clean up the dataset after test try: self.bigquery_client._delete_dataset( self.project, self.dataset_id, delete_contents=True) except Exception: # Dataset might not exist or already deleted pass def test_managed_bigquery_write(self): """Test writing data to BigQuery using managed transforms.""" table_name = 'test_table_' + uuid.uuid4().hex table_spec = f"{self.project}.{self.dataset_id}.{table_name}" # Define BigQuery table schema schema = { "fields": [ {"name": "id", "type": "INTEGER", "mode": "REQUIRED"}, {"name": "name", "type": "STRING", "mode": "REQUIRED"}, {"name": "details", "type": "RECORD", "mode": "NULLABLE", "fields": [ {"name": "color", "type": "STRING", "mode": "NULLABLE"}, {"name": "size", "type": "FLOAT", "mode": "NULLABLE"} ]}, {"name": "ts", "type": "TIMESTAMP", "mode": "NULLABLE"} ] } # Test data test_data = [ { "id": 1, "name": "test_row_1", "details": { "color": "red", "size": 1.5 }, "ts": Timestamp.from_utc_datetime( datetime.datetime( 2025, 1, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)) }, { "id": 2, "name": "test_row_2", "details": { "color": "blue", "size": 2.0 }, "ts": Timestamp.from_utc_datetime( datetime.datetime( 2025, 1, 1, 2, 0, 0, tzinfo=datetime.timezone.utc)) }, ] with TestPipeline(argv=self.args) as pipeline: rows = ( pipeline | "CreateRows" >> beam.Create(test_data) | "ToRow" >> beam.Map(lambda row_dict: beam_row_from_dict(row_dict, schema) ).with_output_types( RowTypeConstraint.from_fields( get_beam_typehints_from_tableschema(schema)))) _ = rows | "WriteToBQ" >> managed.Write( managed.BIGQUERY, config={ "table": table_spec, "write_disposition": "WRITE_APPEND", } ) # Verify data was written by querying the table query = f"SELECT COUNT(*) as count FROM `{table_spec}`" query_job = self.bigquery_client.client.query(query) results = list(query_job.result()) self.assertEqual(results[0].count, 2) if __name__ == '__main__': unittest.main() ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
