-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8987ef1
commit bed60ca
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import json | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from airflow.models.connection import Connection | ||
|
||
from cosmos import ProfileConfig | ||
from cosmos.operators._asynchronous.base import DbtRunAirflowAsyncFactoryOperator, _create_async_operator_class | ||
from cosmos.operators._asynchronous.bigquery import DbtRunAirflowAsyncBigqueryOperator | ||
from cosmos.operators.local import DbtRunLocalOperator | ||
from cosmos.profiles import get_automatic_profile_mapping | ||
|
||
|
||
@pytest.fixture() | ||
def mock_bigquery_conn(): # type: ignore | ||
""" | ||
Mocks and returns an Airflow BigQuery connection. | ||
""" | ||
extra = { | ||
"project": "my_project", | ||
"key_path": "my_key_path.json", | ||
} | ||
conn = Connection( | ||
conn_id="my_bigquery_connection", | ||
conn_type="google_cloud_platform", | ||
extra=json.dumps(extra), | ||
) | ||
|
||
with patch("airflow.hooks.base.BaseHook.get_connection", return_value=conn): | ||
yield conn | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"profile_type, dbt_class, expected_operator_class", | ||
[ | ||
("bigquery", "DbtRun", DbtRunAirflowAsyncBigqueryOperator), | ||
("snowflake", "DbtRun", DbtRunLocalOperator), | ||
("bigquery", "DbtTest", DbtRunLocalOperator), | ||
], | ||
) | ||
def test_create_async_operator_class_success(profile_type, dbt_class, expected_operator_class): | ||
"""Test the successful loading of the async operator class.""" | ||
|
||
operator_class = _create_async_operator_class(profile_type, dbt_class) | ||
|
||
assert operator_class == expected_operator_class | ||
|
||
|
||
@patch("cosmos.operators._asynchronous.bigquery.DbtRunAirflowAsyncBigqueryOperator.drop_table_sql") | ||
@patch("cosmos.operators._asynchronous.bigquery.DbtRunAirflowAsyncBigqueryOperator.get_remote_sql") | ||
@patch("cosmos.operators._asynchronous.bigquery.BigQueryInsertJobOperator.execute") | ||
def test_factory_async_class(mock_execute, get_remote_sql, drop_table_sql, mock_bigquery_conn): | ||
profile_mapping = get_automatic_profile_mapping( | ||
mock_bigquery_conn.conn_id, | ||
profile_args={ | ||
"dataset": "my_dataset", | ||
}, | ||
) | ||
bigquery_profile_config = ProfileConfig( | ||
profile_name="my_profile", target_name="dev", profile_mapping=profile_mapping | ||
) | ||
factory_class = DbtRunAirflowAsyncFactoryOperator( | ||
task_id="run", | ||
project_dir="/tmp", | ||
profile_config=bigquery_profile_config, | ||
full_refresh=True, | ||
extra_context={"dbt_node_config": {"resource_name": "customer"}}, | ||
) | ||
|
||
async_operator = factory_class.create_async_operator() | ||
assert async_operator == DbtRunAirflowAsyncBigqueryOperator | ||
|
||
factory_class.execute(context={}) | ||
|
||
mock_execute.assert_called_once_with({}) |