daesung9511 commented on code in PR #70454:
URL: https://github.com/apache/airflow/pull/70454#discussion_r3651970501
##########
providers/google/tests/unit/google/cloud/operators/test_compute.py:
##########
@@ -190,71 +198,84 @@ def
test_insert_instance_should_not_throw_ex_when_project_id_none(self, mock_hoo
project_id=None,
)
- def test_insert_instance_should_throw_ex_when_missing_zone(self):
+ @mock.patch(COMPUTE_ENGINE_HOOK_PATH)
+ def test_insert_instance_should_throw_ex_when_missing_zone(self,
mock_hook):
+ op = ComputeEngineInsertInstanceOperator(
+ resource_id=GCE_RESOURCE_ID,
+ body=GCE_INSTANCE_BODY_API_CALL,
+ zone="",
+ task_id=TASK_ID,
+ retry=RETRY,
+ timeout=TIMEOUT,
+ metadata=METADATA,
+ gcp_conn_id=GCP_CONN_ID,
+ impersonation_chain=IMPERSONATION_CHAIN,
+ )
with pytest.raises(AirflowException, match=r"The required parameter
'zone' is missing"):
- ComputeEngineInsertInstanceOperator(
- resource_id=GCE_RESOURCE_ID,
- body=GCE_INSTANCE_BODY_API_CALL,
- zone="",
- task_id=TASK_ID,
- retry=RETRY,
- timeout=TIMEOUT,
- metadata=METADATA,
- gcp_conn_id=GCP_CONN_ID,
- impersonation_chain=IMPERSONATION_CHAIN,
- )
+ op.execute(context=mock.MagicMock())
+
+ mock_hook.assert_not_called()
- def test_insert_instance_should_throw_ex_when_missing_resource_id(self):
+ @mock.patch(COMPUTE_ENGINE_HOOK_PATH)
+ def test_insert_instance_should_throw_ex_when_missing_resource_id(self,
mock_hook):
+ op = ComputeEngineInsertInstanceOperator(
+ project_id=GCP_PROJECT_ID,
+ zone=GCE_ZONE,
+ body=GCE_INSTANCE_BODY_WITHOUT_NAME_API_CALL,
+ task_id=TASK_ID,
+ resource_id="",
+ gcp_conn_id=GCP_CONN_ID,
+ impersonation_chain=IMPERSONATION_CHAIN,
+ )
with pytest.raises(
AirflowException,
match=r"The required parameters 'resource_id' and "
r"body\['name'\] are missing\. Please, provide "
r"at least one of them",
):
- ComputeEngineInsertInstanceOperator(
- project_id=GCP_PROJECT_ID,
- zone=GCE_ZONE,
- body=GCE_INSTANCE_BODY_WITHOUT_NAME_API_CALL,
- task_id=TASK_ID,
- resource_id="",
- gcp_conn_id=GCP_CONN_ID,
- impersonation_chain=IMPERSONATION_CHAIN,
- )
+ op.execute(context=mock.MagicMock())
+
+ mock_hook.assert_not_called()
+ @pytest.mark.db_test
@mock.patch(COMPUTE_ENGINE_HOOK_PATH)
- def test_insert_instance_should_not_throw_ex_when_name_is_templated(self,
mock_hook):
+ def test_insert_instance_should_not_throw_ex_when_name_is_templated(
+ self,
+ mock_hook,
+ create_task_instance_of_operator,
+ session,
+ ):
+ dag_id = "templated-instance-name"
+
get_instance_obj_mock = mock.MagicMock()
get_instance_obj_mock.__class__ = Instance
mock_hook.return_value.get_instance.side_effect = [
NotFound("Error message"),
get_instance_obj_mock,
]
- body_with_templated_name = deepcopy(GCE_INSTANCE_BODY_API_CALL)
- body_with_templated_name["name"] = "{{ logical_date }}"
- op = ComputeEngineInsertInstanceOperator(
+
+ body = deepcopy(GCE_INSTANCE_BODY_API_CALL)
+ body["name"] = "{{ dag.dag_id }}"
+
+ ti = create_task_instance_of_operator(
+ ComputeEngineInsertInstanceOperator,
+ dag_id=dag_id,
project_id=GCP_PROJECT_ID,
- resource_id=GCE_RESOURCE_ID,
- body=body_with_templated_name,
+ body=body,
zone=GCE_ZONE,
task_id=TASK_ID,
- retry=RETRY,
- timeout=TIMEOUT,
- metadata=METADATA,
- gcp_conn_id=GCP_CONN_ID,
- impersonation_chain=IMPERSONATION_CHAIN,
- )
- op.execute(context=mock.MagicMock())
- mock_hook.assert_called_once_with(
- api_version=API_VERSION,
gcp_conn_id=GCP_CONN_ID,
impersonation_chain=IMPERSONATION_CHAIN,
)
- mock_hook.return_value.insert_instance.assert_called_once_with(
- project_id=GCP_PROJECT_ID,
- body=body_with_templated_name,
- zone=GCE_ZONE,
- request_id=None,
- )
Review Comment:
Thank you for your feedback! I’ve restored it at the end of the method.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]