fdemiane commented on code in PR #32606:
URL: https://github.com/apache/airflow/pull/32606#discussion_r1272741940


##########
tests/providers/google/cloud/triggers/test_cloud_batch.py:
##########
@@ -0,0 +1,159 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+from google.cloud.batch_v1 import Job, JobStatus
+
+from airflow.providers.google.cloud.triggers.cloud_batch import \
+    CloudBatchJobFinishedTrigger
+from airflow.triggers.base import TriggerEvent
+
+JOB_NAME = 'jobName'
+PROJECT_ID = 'projectId'
+LOCATION = 'us-central1'
+GCP_CONNECTION_ID = 'gcp_connection_id'
+POLL_SLEEP = 0.01
+TIMEOUT = 0.02
+IMPERSONATION_CHAIN = 'impersonation_chain'
+
+
[email protected]
+def trigger():
+    return CloudBatchJobFinishedTrigger(
+        job_name=JOB_NAME,
+        project_id=PROJECT_ID,
+        location=LOCATION,
+        gcp_conn_id=GCP_CONNECTION_ID,
+        polling_period_seconds=POLL_SLEEP,
+        timeout=TIMEOUT,
+        impersonation_chain=IMPERSONATION_CHAIN
+    )
+
+
+class TestCloudBatchJobFinishedTrigger:
+    def test_serialization(self, trigger):
+        classpath, kwargs = trigger.serialize()
+        assert classpath == 
"airflow.providers.google.cloud.triggers.cloud_batch.CloudBatchJobFinishedTrigger"
+        assert kwargs == {
+            "project_id": PROJECT_ID,
+            "job_name": JOB_NAME,
+            "location": LOCATION,
+            "gcp_conn_id": GCP_CONNECTION_ID,
+            "polling_period_seconds": POLL_SLEEP,
+            "timeout": TIMEOUT,
+            "impersonation_chain": IMPERSONATION_CHAIN,
+        }
+
+    @pytest.mark.asyncio
+    
@mock.patch("airflow.providers.google.cloud.triggers.cloud_batch.CloudBatchAsyncHook")
+    async def test_trigger_on_success_yield_successfully(self, mock_hook, 
trigger: CloudBatchJobFinishedTrigger):
+        """
+        Tests the CloudBuildCreateBuildTrigger fires once the job execution 
reaches a successful state.
+        """
+        state = JobStatus.State.SUCCEEDED
+        mock_hook.return_value.get_build_job.return_value = 
self._mock_job_with_state(
+            state)
+        generator = trigger.run()
+        actual = await generator.asend(None)
+        assert (
+            TriggerEvent(
+                {
+                    "job_name": JOB_NAME,
+                    "status": "success",
+                    "message": "Job completed",
+                }
+            )
+            == actual
+        )
+
+    @pytest.mark.asyncio
+    
@mock.patch("airflow.providers.google.cloud.triggers.cloud_batch.CloudBatchAsyncHook")
+    async def test_trigger_on_deleted_yield_successfully(self, mock_hook, 
trigger: CloudBatchJobFinishedTrigger):
+        """
+        Tests the CloudBuildCreateBuildTrigger fires once the job execution 
reaches a successful state.
+        """
+        state = JobStatus.State.DELETION_IN_PROGRESS
+        mock_hook.return_value.get_build_job.return_value = 
self._mock_job_with_state(
+            state)
+        generator = trigger.run()
+        actual = await generator.asend(None)
+        assert (
+            TriggerEvent(
+                {
+                    "job_name": JOB_NAME,
+                    "status": "deleted",
+                    "message": f"Batch job with name {JOB_NAME} is being 
deleted",
+                }
+            )
+            == actual
+        )
+
+    @pytest.mark.asyncio
+    
@mock.patch("airflow.providers.google.cloud.triggers.cloud_batch.CloudBatchAsyncHook")
+    async def test_trigger_on_deleted_yield_exception(self, mock_hook, 
trigger: CloudBatchJobFinishedTrigger):
+        """
+        Tests the CloudBuildCreateBuildTrigger fires once the job execution
+        reaches an state with an error message.
+        """
+        mock_hook.return_value.get_build_job.side_effect = Exception(
+            "Test Exception")
+        generator = trigger.run()
+        actual = await generator.asend(None)
+        assert (
+            TriggerEvent(
+                {
+                    "status": "error",
+                    "message": "Test Exception",
+                }
+            )
+            == actual
+        )
+
+    @pytest.mark.asyncio
+    
@mock.patch("airflow.providers.google.cloud.triggers.cloud_batch.CloudBatchAsyncHook")
+    async def test_trigger_timeout(self, mock_hook, trigger: 
CloudBatchJobFinishedTrigger):
+        """
+        Tests the CloudBuildCreateBuildTrigger fires once the job execution 
times out with an error message.

Review Comment:
   Comment to self: CloudBatch*



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