|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import os |
| 19 | +from datetime import datetime |
| 20 | + |
| 21 | +from airflow import models |
| 22 | +from airflow.providers.google.cloud.operators.gcs import GCSCreateBucketOperator, GCSDeleteBucketOperator |
| 23 | +from airflow.providers.google.cloud.transfers.oracle_to_gcs import OracleToGCSOperator |
| 24 | +from airflow.utils.trigger_rule import TriggerRule |
| 25 | + |
| 26 | +ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID") |
| 27 | +PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT") |
| 28 | +DAG_ID = "example_oracle_to_gcs" |
| 29 | + |
| 30 | +BUCKET_NAME = f"bucket_{DAG_ID}_{ENV_ID}" |
| 31 | +FILENAME = 'test_file' |
| 32 | +SQL_QUERY = "SELECT * from test_table" |
| 33 | + |
| 34 | +with models.DAG( |
| 35 | + DAG_ID, |
| 36 | + schedule_interval='@once', |
| 37 | + start_date=datetime(2021, 1, 1), |
| 38 | + catchup=False, |
| 39 | + tags=['example', 'oracle'], |
| 40 | +) as dag: |
| 41 | + create_bucket = GCSCreateBucketOperator( |
| 42 | + task_id="create_bucket", bucket_name=BUCKET_NAME, project_id=PROJECT_ID |
| 43 | + ) |
| 44 | + |
| 45 | + # [START howto_operator_oracle_to_gcs] |
| 46 | + upload_oracle_to_gcs = OracleToGCSOperator( |
| 47 | + task_id='oracle_to_gcs', sql=SQL_QUERY, bucket=BUCKET_NAME, filename=FILENAME, export_format='csv' |
| 48 | + ) |
| 49 | + # [END howto_operator_oracle_to_gcs] |
| 50 | + |
| 51 | + delete_bucket = GCSDeleteBucketOperator( |
| 52 | + task_id="delete_bucket", bucket_name=BUCKET_NAME, trigger_rule=TriggerRule.ALL_DONE |
| 53 | + ) |
| 54 | + |
| 55 | + ( |
| 56 | + # TEST SETUP |
| 57 | + create_bucket |
| 58 | + # TEST BODY |
| 59 | + >> upload_oracle_to_gcs |
| 60 | + # TEST TEARDOWN |
| 61 | + >> delete_bucket |
| 62 | + ) |
| 63 | + |
| 64 | + from tests.system.utils.watcher import watcher |
| 65 | + |
| 66 | + # This test needs watcher in order to properly mark success/failure |
| 67 | + # when "tearDown" task with trigger rule is part of the DAG |
| 68 | + list(dag.tasks) >> watcher() |
| 69 | + |
| 70 | +from tests.system.utils import get_test_run # noqa: E402 |
| 71 | + |
| 72 | +# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest) |
| 73 | +test_run = get_test_run(dag) |
0 commit comments