|
16 | 16 | # under the License. |
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +import datetime |
19 | 20 | from unittest import mock |
20 | 21 |
|
21 | 22 | import pytest |
22 | 23 |
|
| 24 | +from airflow import DAG |
| 25 | +from airflow.exceptions import AirflowProviderDeprecationWarning |
23 | 26 | from airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs import AzureFileShareToGCSOperator |
24 | 27 |
|
25 | 28 | pytestmark = pytest.mark.filterwarnings("ignore::FutureWarning") |
26 | 29 |
|
| 30 | +DEFAULT_DATE = datetime.datetime(2024, 1, 1) |
27 | 31 | TASK_ID = "test-azure-fileshare-to-gcs" |
28 | 32 | AZURE_FILESHARE_SHARE = "test-share" |
29 | 33 | AZURE_FILESHARE_DIRECTORY_PATH = "/path/to/dir" |
@@ -56,6 +60,104 @@ def test_init(self): |
56 | 60 | assert operator.dest_gcs == GCS_PATH_PREFIX |
57 | 61 | assert operator.google_impersonation_chain == IMPERSONATION_CHAIN |
58 | 62 |
|
| 63 | + @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.AzureFileShareHook") |
| 64 | + @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.GCSHook") |
| 65 | + def test_directory_name_alias_uses_rendered_value(self, gcs_mock_hook, azure_fileshare_mock_hook): |
| 66 | + """A templated directory_name is aliased to directory_path using its rendered value, not the Jinja expression.""" |
| 67 | + dag = DAG("test_azure_fileshare_alias", schedule=None, start_date=DEFAULT_DATE) |
| 68 | + with pytest.warns(AirflowProviderDeprecationWarning, match="Use 'directory_path' instead"): |
| 69 | + operator = AzureFileShareToGCSOperator( |
| 70 | + task_id=TASK_ID, |
| 71 | + share_name=AZURE_FILESHARE_SHARE, |
| 72 | + directory_name="{{ params.legacy_dir }}", |
| 73 | + params={"legacy_dir": "rendered/dir"}, |
| 74 | + azure_fileshare_conn_id=AZURE_FILESHARE_CONN_ID, |
| 75 | + gcp_conn_id=GCS_CONN_ID, |
| 76 | + dest_gcs=GCS_PATH_PREFIX, |
| 77 | + return_gcs_uris=True, |
| 78 | + dag=dag, |
| 79 | + ) |
| 80 | + assert operator.directory_path is None |
| 81 | + |
| 82 | + operator.render_template_fields({"params": {"legacy_dir": "rendered/dir"}}) |
| 83 | + assert operator.directory_path is None |
| 84 | + |
| 85 | + azure_fileshare_mock_hook.return_value.list_files.return_value = MOCK_FILES |
| 86 | + operator.execute(None) |
| 87 | + azure_fileshare_mock_hook.assert_any_call( |
| 88 | + share_name=AZURE_FILESHARE_SHARE, |
| 89 | + azure_fileshare_conn_id=AZURE_FILESHARE_CONN_ID, |
| 90 | + directory_path="rendered/dir", |
| 91 | + ) |
| 92 | + |
| 93 | + @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.AzureFileShareHook") |
| 94 | + @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.GCSHook") |
| 95 | + def test_native_directory_path_rendering_to_none_is_not_aliased( |
| 96 | + self, gcs_mock_hook, azure_fileshare_mock_hook |
| 97 | + ): |
| 98 | + """ |
| 99 | + Behaviour-preservation guard: the alias decision must come from the un-rendered values. |
| 100 | +
|
| 101 | + With render_template_as_native_obj an explicitly supplied directory_path can render to None, |
| 102 | + and re-deciding the alias after rendering would wrongly fall back to the deprecated |
| 103 | + directory_name. Deciding in __init__ (pre-render) keeps this case an explicit error. |
| 104 | + """ |
| 105 | + dag = DAG( |
| 106 | + "test_azure_fileshare_native", |
| 107 | + schedule=None, |
| 108 | + start_date=DEFAULT_DATE, |
| 109 | + render_template_as_native_obj=True, |
| 110 | + ) |
| 111 | + operator = AzureFileShareToGCSOperator( |
| 112 | + task_id=TASK_ID, |
| 113 | + share_name=AZURE_FILESHARE_SHARE, |
| 114 | + directory_path="{{ params.p }}", |
| 115 | + directory_name="legacy", |
| 116 | + params={"p": None}, |
| 117 | + azure_fileshare_conn_id=AZURE_FILESHARE_CONN_ID, |
| 118 | + gcp_conn_id=GCS_CONN_ID, |
| 119 | + dest_gcs=GCS_PATH_PREFIX, |
| 120 | + return_gcs_uris=True, |
| 121 | + dag=dag, |
| 122 | + ) |
| 123 | + |
| 124 | + operator.render_template_fields({"params": {"p": None}}) |
| 125 | + assert operator.directory_path is None |
| 126 | + |
| 127 | + # The deprecated alias must not silently substitute directory_name; a genuinely unset |
| 128 | + # directory surfaces as the operator's own error instead of listing the wrong directory. |
| 129 | + azure_fileshare_mock_hook.return_value.list_files.return_value = MOCK_FILES |
| 130 | + with pytest.raises(RuntimeError, match="directory_name must be set"): |
| 131 | + operator.execute(None) |
| 132 | + |
| 133 | + @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.AzureFileShareHook") |
| 134 | + @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.GCSHook") |
| 135 | + def test_mapped_task_aliases_directory_name(self, gcs_mock_hook, azure_fileshare_mock_hook): |
| 136 | + """ |
| 137 | + Mapped tasks reach execute() through MappedOperator.unmap(), never through |
| 138 | + render_template_fields overrides, so the alias must not depend on one. |
| 139 | + """ |
| 140 | + mapped = AzureFileShareToGCSOperator.partial( |
| 141 | + task_id=TASK_ID, |
| 142 | + share_name=AZURE_FILESHARE_SHARE, |
| 143 | + directory_name=AZURE_FILESHARE_DIRECTORY_PATH, |
| 144 | + azure_fileshare_conn_id=AZURE_FILESHARE_CONN_ID, |
| 145 | + gcp_conn_id=GCS_CONN_ID, |
| 146 | + dest_gcs=GCS_PATH_PREFIX, |
| 147 | + return_gcs_uris=True, |
| 148 | + ).expand(prefix=["sub/a/", "sub/b/"]) |
| 149 | + |
| 150 | + with pytest.warns(AirflowProviderDeprecationWarning, match="Use 'directory_path' instead"): |
| 151 | + operator = mapped.unmap({"prefix": "sub/a/"}) |
| 152 | + |
| 153 | + azure_fileshare_mock_hook.return_value.list_files.return_value = MOCK_FILES |
| 154 | + operator.execute(None) |
| 155 | + azure_fileshare_mock_hook.assert_any_call( |
| 156 | + share_name=AZURE_FILESHARE_SHARE, |
| 157 | + azure_fileshare_conn_id=AZURE_FILESHARE_CONN_ID, |
| 158 | + directory_path=AZURE_FILESHARE_DIRECTORY_PATH, |
| 159 | + ) |
| 160 | + |
59 | 161 | @pytest.mark.parametrize("return_gcs_uris", [True, False]) |
60 | 162 | @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.AzureFileShareHook") |
61 | 163 | @mock.patch("airflow.providers.google.cloud.transfers.azure_fileshare_to_gcs.GCSHook") |
|
0 commit comments