Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Snowflake system tests to new design #22434 #24151

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions airflow/providers/snowflake/example_dags/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion docs/apache-airflow-providers-snowflake/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Content
:maxdepth: 1
:caption: Resources

Example DAGs <https://github.com/apache/airflow/tree/main/airflow/providers/snowflake/example_dags>
Example DAGs <https://github.com/apache/airflow/tree/main/tests/system/providers/snowflake>
PyPI Repository <https://pypi.org/project/apache-airflow-providers-snowflake/>
Installing from sources <installing-providers-from-sources>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ a file format (see `docs <https://docs.snowflake.com/en/sql-reference/sql/create

An example usage of the S3ToSnowflakeOperator is as follows:

.. exampleinclude:: /../../airflow/providers/snowflake/example_dags/example_snowflake.py
.. exampleinclude:: /../../tests/system/providers/snowflake/example_snowflake.py
:language: python
:start-after: [START howto_operator_s3_to_snowflake]
:end-before: [END howto_operator_s3_to_snowflake]
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ the connection metadata is structured as follows:

An example usage of the SnowflakeOperator is as follows:

.. exampleinclude:: /../../airflow/providers/snowflake/example_dags/example_snowflake.py
.. exampleinclude:: /../../tests/system/providers/snowflake/example_snowflake.py
:language: python
:start-after: [START howto_operator_snowflake]
:end-before: [END howto_operator_snowflake]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ and contain the resulting dataset (e.g. ASCII formatted dataframe).

An example usage of the SnowflakeToSlackOperator is as follows:

.. exampleinclude:: /../../airflow/providers/snowflake/example_dags/example_snowflake.py
.. exampleinclude:: /../../tests/system/providers/snowflake/example_snowflake.py
:language: python
:start-after: [START howto_operator_snowflake_to_slack]
:end-before: [END howto_operator_snowflake_to_slack]
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
Example use of Snowflake related operators.
"""
import os
from datetime import datetime

from airflow import DAG
Expand Down Expand Up @@ -47,89 +48,91 @@
SNOWFLAKE_SLACK_MESSAGE = (
"Results in an ASCII table:\n```{{ results_df | tabulate(tablefmt='pretty', headers='keys') }}```"
)
ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID")
DAG_ID = "example_snowflake"

# [START howto_operator_snowflake]

dag = DAG(
'example_snowflake',
with DAG(
DAG_ID,
start_date=datetime(2021, 1, 1),
default_args={'snowflake_conn_id': SNOWFLAKE_CONN_ID},
tags=['example'],
catchup=False,
)


snowflake_op_sql_str = SnowflakeOperator(
task_id='snowflake_op_sql_str',
dag=dag,
sql=CREATE_TABLE_SQL_STRING,
warehouse=SNOWFLAKE_WAREHOUSE,
database=SNOWFLAKE_DATABASE,
schema=SNOWFLAKE_SCHEMA,
role=SNOWFLAKE_ROLE,
)

snowflake_op_with_params = SnowflakeOperator(
task_id='snowflake_op_with_params',
dag=dag,
sql=SQL_INSERT_STATEMENT,
parameters={"id": 56},
warehouse=SNOWFLAKE_WAREHOUSE,
database=SNOWFLAKE_DATABASE,
schema=SNOWFLAKE_SCHEMA,
role=SNOWFLAKE_ROLE,
)

snowflake_op_sql_list = SnowflakeOperator(task_id='snowflake_op_sql_list', dag=dag, sql=SQL_LIST)

snowflake_op_sql_multiple_stmts = SnowflakeOperator(
task_id='snowflake_op_sql_multiple_stmts',
dag=dag,
sql=SQL_MULTIPLE_STMTS,
)

snowflake_op_template_file = SnowflakeOperator(
task_id='snowflake_op_template_file',
dag=dag,
sql='/path/to/sql/<filename>.sql',
)

# [END howto_operator_snowflake]

# [START howto_operator_s3_to_snowflake]

copy_into_table = S3ToSnowflakeOperator(
task_id='copy_into_table',
s3_keys=[S3_FILE_PATH],
table=SNOWFLAKE_SAMPLE_TABLE,
schema=SNOWFLAKE_SCHEMA,
stage=SNOWFLAKE_STAGE,
file_format="(type = 'CSV',field_delimiter = ';')",
dag=dag,
)

# [END howto_operator_s3_to_snowflake]

# [START howto_operator_snowflake_to_slack]

slack_report = SnowflakeToSlackOperator(
task_id="slack_report",
sql=SNOWFLAKE_SLACK_SQL,
slack_message=SNOWFLAKE_SLACK_MESSAGE,
slack_conn_id=SLACK_CONN_ID,
dag=dag,
)

# [END howto_operator_snowflake_to_slack]

(
snowflake_op_sql_str
>> [
snowflake_op_with_params,
snowflake_op_sql_list,
snowflake_op_template_file,
copy_into_table,
snowflake_op_sql_multiple_stmts,
]
>> slack_report
)
) as dag:
# [START snowflake_example_dag]
snowflake_op_sql_str = SnowflakeOperator(
task_id='snowflake_op_sql_str',
sql=CREATE_TABLE_SQL_STRING,
warehouse=SNOWFLAKE_WAREHOUSE,
database=SNOWFLAKE_DATABASE,
schema=SNOWFLAKE_SCHEMA,
role=SNOWFLAKE_ROLE,
)

snowflake_op_with_params = SnowflakeOperator(
task_id='snowflake_op_with_params',
sql=SQL_INSERT_STATEMENT,
parameters={"id": 56},
warehouse=SNOWFLAKE_WAREHOUSE,
database=SNOWFLAKE_DATABASE,
schema=SNOWFLAKE_SCHEMA,
role=SNOWFLAKE_ROLE,
)

snowflake_op_sql_list = SnowflakeOperator(task_id='snowflake_op_sql_list', sql=SQL_LIST)

snowflake_op_sql_multiple_stmts = SnowflakeOperator(
task_id='snowflake_op_sql_multiple_stmts',
sql=SQL_MULTIPLE_STMTS,
)

snowflake_op_template_file = SnowflakeOperator(
task_id='snowflake_op_template_file',
sql='/path/to/sql/<filename>.sql',
)

# [END howto_operator_snowflake]

# [START howto_operator_s3_to_snowflake]

copy_into_table = S3ToSnowflakeOperator(
task_id='copy_into_table',
s3_keys=[S3_FILE_PATH],
table=SNOWFLAKE_SAMPLE_TABLE,
schema=SNOWFLAKE_SCHEMA,
stage=SNOWFLAKE_STAGE,
file_format="(type = 'CSV',field_delimiter = ';')",
)

# [END howto_operator_s3_to_snowflake]

# [START howto_operator_snowflake_to_slack]

slack_report = SnowflakeToSlackOperator(
task_id="slack_report",
sql=SNOWFLAKE_SLACK_SQL,
slack_message=SNOWFLAKE_SLACK_MESSAGE,
slack_conn_id=SLACK_CONN_ID,
)

# [END howto_operator_snowflake_to_slack]

(
snowflake_op_sql_str
>> [
snowflake_op_with_params,
snowflake_op_sql_list,
snowflake_op_template_file,
copy_into_table,
snowflake_op_sql_multiple_stmts,
]
>> slack_report
)
# [END snowflake_example_dag]


from tests.system.utils import get_test_run # noqa: E402

# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)
test_run = get_test_run(dag)