You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by go...@apache.org on 2025/08/16 23:07:50 UTC

(airflow) branch main updated: Make opsgenie tests db independent (#54577)

This is an automated email from the ASF dual-hosted git repository.

gopidesu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 6b22dedc472 Make opsgenie tests db independent (#54577)
6b22dedc472 is described below

commit 6b22dedc4721d8b1f8f6e8a271c64a18724ddd77
Author: GPK <go...@gmail.com>
AuthorDate: Sun Aug 17 00:07:43 2025 +0100

    Make opsgenie tests db independent (#54577)
    
    * Make opsgenie tests db independent
    
    * Fix tests
---
 .pre-commit-config.yaml                            |  1 +
 devel-common/src/tests_common/pytest_plugin.py     | 10 +++++
 .../tests/unit/opsgenie/hooks/test_opsgenie.py     |  4 +-
 .../unit/opsgenie/notifications/test_opsgenie.py   | 46 ++++++++++++----------
 4 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index eec81cf26e2..2f33a3930c9 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -639,6 +639,7 @@ repos:
             ^providers/odbc/.*\.py$|
             ^providers/openai/.*\.py$|
             ^providers/openfaas/.*\.py$|
+            ^providers/opsgenie/.*\.py$|
             ^providers/oracle/.*\.py$|
             ^providers/pagerduty/.*\.py$|
             ^providers/pgvector/.*\.py$|
diff --git a/devel-common/src/tests_common/pytest_plugin.py b/devel-common/src/tests_common/pytest_plugin.py
index 23cc2c3d0ca..91fd829d6b6 100644
--- a/devel-common/src/tests_common/pytest_plugin.py
+++ b/devel-common/src/tests_common/pytest_plugin.py
@@ -2583,3 +2583,13 @@ def _import_timezone():
         except ModuleNotFoundError:
             from airflow.utils import timezone
     return timezone
+
+
+@pytest.fixture
+def create_dag_without_db():
+    def create_dag(dag_id: str):
+        from airflow.models.dag import DAG
+
+        return DAG(dag_id=dag_id, schedule=None, render_template_as_native_obj=True)
+
+    return create_dag
diff --git a/providers/opsgenie/tests/unit/opsgenie/hooks/test_opsgenie.py b/providers/opsgenie/tests/unit/opsgenie/hooks/test_opsgenie.py
index db05bab01ad..33bc11f0b6e 100644
--- a/providers/opsgenie/tests/unit/opsgenie/hooks/test_opsgenie.py
+++ b/providers/opsgenie/tests/unit/opsgenie/hooks/test_opsgenie.py
@@ -106,7 +106,8 @@ class TestOpsgenieAlertHook:
             == "eb243592-faa2-4ba2-a551q-1afdf565c889"
         )
 
-    def test_create_alert_api_key_not_set(self, create_connection_without_db):
+    @mock.patch.object(AlertApi, "create_alert")
+    def test_create_alert_api_key_not_set(self, mock_create_alert, create_connection_without_db):
         create_connection_without_db(
             Connection(
                 conn_id="opsgenie_without_api_key",
@@ -114,6 +115,7 @@ class TestOpsgenieAlertHook:
                 host="https://api.opsgenie.com/",
             )
         )
+        mock_create_alert.side_effect = AuthenticationException
         hook = OpsgenieAlertHook(opsgenie_conn_id="opsgenie_without_api_key")
         with pytest.raises(AuthenticationException):
             hook.create_alert(payload=self._create_alert_payload)
diff --git a/providers/opsgenie/tests/unit/opsgenie/notifications/test_opsgenie.py b/providers/opsgenie/tests/unit/opsgenie/notifications/test_opsgenie.py
index 9f93ba7e609..911a3cab408 100644
--- a/providers/opsgenie/tests/unit/opsgenie/notifications/test_opsgenie.py
+++ b/providers/opsgenie/tests/unit/opsgenie/notifications/test_opsgenie.py
@@ -21,12 +21,23 @@ from unittest import mock
 
 import pytest
 
+from airflow.models import Connection
 from airflow.providers.opsgenie.hooks.opsgenie import OpsgenieAlertHook
 from airflow.providers.opsgenie.notifications.opsgenie import OpsgenieNotifier, send_opsgenie_notification
-from airflow.providers.standard.operators.empty import EmptyOperator
 
 
 class TestOpsgenieNotifier:
+    @pytest.fixture(autouse=True)
+    def setup_connections(self, create_connection_without_db):
+        create_connection_without_db(
+            Connection(
+                conn_id="opsgenie_default",
+                conn_type="opsgenie",
+                host="https://api.opsgenie.com/",
+                password="eb243592-faa2-4ba2-a551q-1afdf565c889",
+            )
+        )
+
     _config = {
         "message": "An example alert message",
         "alias": "Life is too short for no alias",
@@ -74,29 +85,22 @@ class TestOpsgenieNotifier:
     }
 
     @mock.patch.object(OpsgenieAlertHook, "get_conn")
-    @pytest.mark.db_test
-    def test_notifier(self, mock_opsgenie_alert_hook, dag_maker):
-        with dag_maker("test_notifier") as dag:
-            EmptyOperator(task_id="task1")
+    def test_notifier(self, mock_opsgenie_alert_hook):
         notifier = send_opsgenie_notification(payload=self._config)
-        notifier({"dag": dag})
-        mock_opsgenie_alert_hook.return_value.create_alert.assert_called_once_with(self.expected_payload_dict)
+        notifier.notify({})
+        args, _ = mock_opsgenie_alert_hook.return_value.create_alert.call_args
+        assert args[0] == self.expected_payload_dict
 
     @mock.patch.object(OpsgenieAlertHook, "get_conn")
-    @pytest.mark.db_test
-    def test_notifier_with_notifier_class(self, mock_opsgenie_alert_hook, dag_maker):
-        with dag_maker("test_notifier") as dag:
-            EmptyOperator(task_id="task1")
+    def test_notifier_with_notifier_class(self, mock_opsgenie_alert_hook):
         notifier = OpsgenieNotifier(payload=self._config)
-        notifier({"dag": dag})
-        mock_opsgenie_alert_hook.return_value.create_alert.assert_called_once_with(self.expected_payload_dict)
+        notifier.notify({})
+        args, _ = mock_opsgenie_alert_hook.return_value.create_alert.call_args
+        assert args[0] == self.expected_payload_dict
 
     @mock.patch.object(OpsgenieAlertHook, "get_conn")
-    @pytest.mark.db_test
-    def test_notifier_templated(self, mock_opsgenie_alert_hook, dag_maker):
+    def test_notifier_templated(self, mock_opsgenie_alert_hook, create_dag_without_db):
         dag_id = "test_notifier"
-        with dag_maker(dag_id) as dag:
-            EmptyOperator(task_id="task1")
 
         template_fields = ("message", "alias", "description", "entity", "priority", "note")
         templated_config = {}
@@ -114,7 +118,7 @@ class TestOpsgenieNotifier:
                 templated_expected_payload_dict[key] = value
 
         notifier = OpsgenieNotifier(payload=templated_config)
-        notifier({"dag": dag})
-        mock_opsgenie_alert_hook.return_value.create_alert.assert_called_once_with(
-            templated_expected_payload_dict
-        )
+
+        notifier({"dag": create_dag_without_db(dag_id=dag_id)})
+        args, _ = mock_opsgenie_alert_hook.return_value.create_alert.call_args
+        assert args[0] == templated_expected_payload_dict