shahar1 commented on code in PR #70454:
URL: https://github.com/apache/airflow/pull/70454#discussion_r3651909582


##########
providers/google/tests/unit/google/cloud/operators/test_compute.py:
##########
@@ -453,17 +496,26 @@ def 
test_insert_instance_from_template_should_throw_ex_when_missing_body(self):
                 impersonation_chain=IMPERSONATION_CHAIN,
             )
 
+    @pytest.mark.db_test
     @mock.patch(COMPUTE_ENGINE_HOOK_PATH)
-    def 
test_insert_instance_from_template_should_not_throw_ex_when_name_is_templated(self,
 mock_hook):
+    def 
test_insert_instance_from_template_should_not_throw_ex_when_name_is_templated(
+        self, mock_hook, create_task_instance_of_operator, session

Review Comment:
   `session` doesn't seem to be in use



##########
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:
   These should be restored (to validate the the hook got the right call)



##########
providers/google/src/airflow/providers/google/cloud/operators/compute.py:
##########
@@ -1232,16 +1232,7 @@ def __init__(
     ) -> None:
         self.body_patch = body_patch
         self.request_id = request_id
-        self._field_validator = None  # GcpBodyFieldValidator | None
-        if "name" not in self.body_patch:
-            raise AirflowException(
-                f"The body '{body_patch}' should contain at least name for the 
new operator "
-                f"in the 'name' field"
-            )
-        if validate_body:
-            self._field_validator = GcpBodyFieldValidator(
-                GCE_INSTANCE_TEMPLATE_VALIDATION_PATCH_SPECIFICATION, 
api_version=api_version
-            )
+        self.validate_body = validate_body
         self._field_sanitizer = 
GcpBodyFieldSanitizer(GCE_INSTANCE_FIELDS_TO_SANITIZE)

Review Comment:
   It should be fine to remove now:
   ```suggestion
   ```



##########
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,

Review Comment:
   `session` doesn't seem to be in use



##########
providers/google/tests/unit/google/cloud/operators/test_compute.py:
##########
@@ -516,37 +580,48 @@ def 
test_delete_instance_should_execute_successfully(self, mock_hook):
             zone=GCE_ZONE,
         )
 
-    def test_delete_instance_should_throw_ex_when_missing_zone(self):
-        with pytest.raises(AirflowException, match=r"The required parameter 
'zone' is missing"):
-            ComputeEngineDeleteInstanceOperator(
-                resource_id=GCE_RESOURCE_ID,
-                zone="",
-                task_id=TASK_ID,
-                retry=RETRY,
-                timeout=TIMEOUT,
-                metadata=METADATA,
-                gcp_conn_id=GCP_CONN_ID,
-                impersonation_chain=IMPERSONATION_CHAIN,
-            )
+    @mock.patch(COMPUTE_ENGINE_HOOK_PATH)
+    def test_delete_instance_should_throw_ex_when_missing_zone(self, 
mock_hook):
+        op = ComputeEngineDeleteInstanceOperator(
+            project_id=GCP_PROJECT_ID,
+            zone="",
+            resource_id=GCE_RESOURCE_ID,
+            task_id=TASK_ID,
+            gcp_conn_id=GCP_CONN_ID,
+            impersonation_chain=IMPERSONATION_CHAIN,
+        )
+
+        with pytest.raises(
+            AirflowException,
+            match=r"The required parameter 'zone' is missing",
+        ):
+            op.execute(context=mock.MagicMock())
+
+        mock_hook.assert_not_called()
 
-    def test_delete_instance_should_throw_ex_when_missing_resource_id(self):
+    @mock.patch(COMPUTE_ENGINE_HOOK_PATH)
+    def test_delete_instance_should_throw_ex_when_missing_resource_id(self, 
mock_hook):
+        op = ComputeEngineDeleteInstanceOperator(
+            resource_id="",
+            zone=GCE_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 
'resource_id' is missing"):
-            ComputeEngineDeleteInstanceOperator(
-                resource_id="",
-                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_hook.MagicMock())

Review Comment:
   ```suggestion
               op.execute(context=mock.MagicMock())
   ```



-- 
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]

Reply via email to