Source code for tests.system.google.cloud.translate.example_translate_dataset
## 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."""Example Airflow DAG that translates text in Google Cloud Translate using V3 API versionservice in the Google Cloud."""from__future__importannotationsimportosfromdatetimeimportdatetimefromairflow.models.dagimportDAGfromairflow.providers.google.cloud.operators.gcsimportGCSCreateBucketOperator,GCSDeleteBucketOperatorfromairflow.providers.google.cloud.operators.translateimport(TranslateCreateDatasetOperator,TranslateDatasetsListOperator,TranslateDeleteDatasetOperator,TranslateImportDataOperator,)fromairflow.providers.google.cloud.transfers.gcs_to_gcsimportGCSToGCSOperatorfromairflow.utils.trigger_ruleimportTriggerRule
withDAG(DAG_ID,schedule="@once",# Override to match your needsstart_date=datetime(2024,11,1),catchup=False,tags=["example","translate_dataset",],)asdag:
copy_dataset_source_tsv=GCSToGCSOperator(task_id="copy_dataset_file",source_bucket=RESOURCE_DATA_BUCKET,source_object=RESOURCE_PATH,destination_bucket=DATA_SAMPLE_GCS_BUCKET_NAME,destination_object=DST_PATH,)# [START howto_operator_translate_automl_create_dataset]create_dataset_op=TranslateCreateDatasetOperator(task_id="translate_v3_ds_create",dataset=DATASET,project_id=PROJECT_ID,location=REGION,)# [END howto_operator_translate_automl_create_dataset]# [START howto_operator_translate_automl_import_data]import_ds_data_op=TranslateImportDataOperator(task_id="translate_v3_ds_import_data",dataset_id=create_dataset_op.output["dataset_id"],input_config={"input_files":[{"usage":"UNASSIGNED","gcs_source":{"input_uri":DATASET_DATA_PATH}}]},project_id=PROJECT_ID,location=REGION,)# [END howto_operator_translate_automl_import_data]# [START howto_operator_translate_automl_list_datasets]list_datasets_op=TranslateDatasetsListOperator(task_id="translate_v3_list_ds",project_id=PROJECT_ID,location=REGION,)# [END howto_operator_translate_automl_list_datasets]# [START howto_operator_translate_automl_delete_dataset]delete_ds_op=TranslateDeleteDatasetOperator(task_id="translate_v3_ds_delete",dataset_id=create_dataset_op.output["dataset_id"],project_id=PROJECT_ID,location=REGION,)# [END howto_operator_translate_automl_delete_dataset]delete_bucket=GCSDeleteBucketOperator(task_id="delete_bucket",bucket_name=DATA_SAMPLE_GCS_BUCKET_NAME,trigger_rule=TriggerRule.ALL_DONE,)(# TEST SETUP[create_bucket>>copy_dataset_source_tsv]# TEST BODY>>create_dataset_op>>import_ds_data_op>>list_datasets_op>>delete_ds_op# TEST TEARDOWN>>delete_bucket)fromtests_common.test_utils.watcherimportwatcher# This test needs watcher in order to properly mark success/failure# when "tearDown" task with trigger rule is part of the DAGlist(dag.tasks)>>watcher()fromtests_common.test_utils.system_testsimportget_test_run# noqa: E402# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)