Skip to content

Commit 7b79b81

Browse files
authored
Keep Campaign Manager delete report provision check in __init__ (#70530)
The check only asks which of report_name / report_id was supplied, never what the values are, so it belongs in the constructor: a static authoring mistake should surface once at Dag parse time rather than on every task instance and retry on a worker. Moving it to execute() would also misreport a supplied argument as missing under render_template_as_native_obj=True, where a provided field can render to None. See #70296 for the rule this follows.
1 parent b61f359 commit 7b79b81

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎generated/known_airflow_exceptions.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ providers/google/src/airflow/providers/google/leveldb/hooks/leveldb.py::4
319319
providers/google/src/airflow/providers/google/marketing_platform/hooks/campaign_manager.py::2
320320
providers/google/src/airflow/providers/google/marketing_platform/hooks/search_ads.py::1
321321
providers/google/src/airflow/providers/google/marketing_platform/operators/bid_manager.py::3
322-
providers/google/src/airflow/providers/google/marketing_platform/operators/campaign_manager.py::2
323322
providers/google/src/airflow/providers/google/marketing_platform/sensors/display_video.py::1
324323
providers/google/src/airflow/providers/google/suite/hooks/calendar.py::1
325324
providers/google/src/airflow/providers/google/suite/hooks/sheets.py::1

‎providers/google/src/airflow/providers/google/marketing_platform/operators/campaign_manager.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727

2828
from googleapiclient import http
2929

30-
from airflow.providers.common.compat.sdk import AirflowException
3130
from airflow.providers.google.cloud.hooks.gcs import GCSHook
3231
from airflow.providers.google.marketing_platform.hooks.campaign_manager import GoogleCampaignManagerHook
3332
from airflow.providers.google.version_compat import BaseOperator
33+
from airflow.utils.helpers import exactly_one
3434

3535
if TYPE_CHECKING:
3636
from airflow.providers.common.compat.sdk import Context
@@ -84,11 +84,8 @@ def __init__(
8484
**kwargs,
8585
) -> None:
8686
super().__init__(**kwargs)
87-
if not (report_name or report_id):
88-
raise AirflowException("Please provide `report_name` or `report_id`.")
89-
if report_name and report_id:
90-
raise AirflowException("Please provide only one parameter `report_name` or `report_id`.")
91-
87+
if not exactly_one(report_name is not None, report_id is not None):
88+
raise ValueError("Please provide exactly one of `report_name` or `report_id`.")
9289
self.profile_id = profile_id
9390
self.report_name = report_name
9491
self.report_id = report_id

‎providers/google/tests/unit/google/marketing_platform/operators/test_campaign_manager.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_execute(self, mock_base_op, hook_mock):
7777
api_version=API_VERSION,
7878
task_id="test_task",
7979
)
80-
op.execute(context=None)
80+
op.execute(context={})
8181
hook_mock.assert_called_once_with(
8282
gcp_conn_id=GCP_CONN_ID,
8383
api_version=API_VERSION,
@@ -87,6 +87,26 @@ def test_execute(self, mock_base_op, hook_mock):
8787
profile_id=PROFILE_ID, report_id=REPORT_ID
8888
)
8989

90+
@pytest.mark.parametrize(
91+
("report_name", "report_id"),
92+
[
93+
pytest.param(None, None, id="both-missing"),
94+
pytest.param(REPORT_NAME, REPORT_ID, id="both-provided"),
95+
],
96+
)
97+
def test_missing_or_conflicting_report_params_fail_at_construction(self, report_name, report_id):
98+
# Provision checks on templated fields stay in `__init__` (rewritten with
99+
# `is not None` polarity) so static authoring mistakes surface at Dag parse
100+
# time — see https://github.com/apache/airflow/issues/70296.
101+
with pytest.raises(ValueError, match="Please provide exactly one of `report_name` or `report_id`"):
102+
GoogleCampaignManagerDeleteReportOperator(
103+
profile_id=PROFILE_ID,
104+
report_name=report_name,
105+
report_id=report_id,
106+
api_version=API_VERSION,
107+
task_id="test_task",
108+
)
109+
90110

91111
@pytest.mark.db_test
92112
class TestGoogleCampaignManagerDownloadReportOperator:

‎scripts/ci/prek/validate_operators_init_exemptions.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ providers/google/src/airflow/providers/google/cloud/sensors/bigquery_dts.py::Big
1919
providers/google/src/airflow/providers/google/cloud/sensors/cloud_composer.py::CloudComposerExternalTaskSensor
2020
providers/google/src/airflow/providers/google/cloud/transfers/azure_fileshare_to_gcs.py::AzureFileShareToGCSOperator
2121
providers/google/src/airflow/providers/google/cloud/transfers/gcs_to_bigquery.py::GCSToBigQueryOperator
22-
providers/google/src/airflow/providers/google/marketing_platform/operators/campaign_manager.py::GoogleCampaignManagerDeleteReportOperator
2322
providers/microsoft/psrp/src/airflow/providers/microsoft/psrp/operators/psrp.py::PsrpOperator

0 commit comments

Comments
 (0)