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 31e93acab2f Make smtp provider tests db independent (#54674)
31e93acab2f is described below

commit 31e93acab2f7f48a9d05c7e2ee63a9c1b81163d6
Author: GPK <[email protected]>
AuthorDate: Tue Aug 19 22:04:13 2025 +0100

    Make smtp provider tests db independent (#54674)
---
 .pre-commit-config.yaml                            |  1 +
 devel-common/src/tests_common/pytest_plugin.py     | 24 ++++++++
 .../tests/unit/smtp/notifications/test_smtp.py     | 65 +++++++++++-----------
 3 files changed, 59 insertions(+), 31 deletions(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 78ad73e8322..33e9e027aef 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -644,6 +644,7 @@ repos:
             ^providers/sendgrid/.*\.py$|
             ^providers/singularity/.*\.py$|
             ^providers/slack/.*\.py$|
+            ^providers/smtp/.*\.py$|
             ^providers/tableau/.*\.py$|
             ^providers/teradata/.*\.py$|
             ^providers/trino/.*\.py$|
diff --git a/devel-common/src/tests_common/pytest_plugin.py 
b/devel-common/src/tests_common/pytest_plugin.py
index a4ce73c61c5..e8858b9fe2d 100644
--- a/devel-common/src/tests_common/pytest_plugin.py
+++ b/devel-common/src/tests_common/pytest_plugin.py
@@ -2622,3 +2622,27 @@ def create_dag_without_db():
         return DAG(dag_id=dag_id, schedule=None, 
render_template_as_native_obj=True)
 
     return create_dag
+
+
[email protected]
+def mock_task_instance():
+    def _create_mock_task_instance(
+        task_id: str = "test_task",
+        dag_id: str = "test_dag",
+        run_id: str = "test_run",
+        try_number: int = 0,
+        state: str = "running",
+        max_tries: int = 0,
+    ):
+        from airflow.models import TaskInstance
+
+        mock_ti = mock.MagicMock(spec=TaskInstance)
+        mock_ti.task_id = task_id
+        mock_ti.dag_id = dag_id
+        mock_ti.run_id = run_id
+        mock_ti.try_number = try_number
+        mock_ti.state = state
+        mock_ti.max_tries = max_tries
+        return mock_ti
+
+    return _create_mock_task_instance
diff --git a/providers/smtp/tests/unit/smtp/notifications/test_smtp.py 
b/providers/smtp/tests/unit/smtp/notifications/test_smtp.py
index 22092b17a0b..4dfffab36e0 100644
--- a/providers/smtp/tests/unit/smtp/notifications/test_smtp.py
+++ b/providers/smtp/tests/unit/smtp/notifications/test_smtp.py
@@ -20,17 +20,11 @@ from __future__ import annotations
 import tempfile
 from unittest import mock
 
-import pytest
-
 from airflow.providers.smtp.hooks.smtp import SmtpHook
 from airflow.providers.smtp.notifications.smtp import (
     SmtpNotifier,
     send_smtp_notification,
 )
-from airflow.providers.standard.operators.empty import EmptyOperator
-from airflow.utils import timezone
-
-pytestmark = pytest.mark.db_test
 
 SMTP_API_DEFAULT_CONN_ID = SmtpHook.default_conn_name
 
@@ -40,16 +34,14 @@ NUM_TRY = 0
 
 class TestSmtpNotifier:
     @mock.patch("airflow.providers.smtp.notifications.smtp.SmtpHook")
-    def test_notifier(self, mock_smtphook_hook, dag_maker):
-        with dag_maker("test_notifier") as dag:
-            EmptyOperator(task_id="task1")
+    def test_notifier(_self, mock_smtphook_hook, create_dag_without_db):
         notifier = send_smtp_notification(
             from_email="[email protected]",
             to="[email protected]",
             subject="subject",
             html_content="body",
         )
-        notifier({"dag": dag})
+        notifier({"dag": create_dag_without_db("test_notifier")})
         
mock_smtphook_hook.return_value.__enter__().send_email_smtp.assert_called_once_with(
             from_email="[email protected]",
             to="[email protected]",
@@ -65,16 +57,14 @@ class TestSmtpNotifier:
         )
 
     @mock.patch("airflow.providers.smtp.notifications.smtp.SmtpHook")
-    def test_notifier_with_notifier_class(self, mock_smtphook_hook, dag_maker):
-        with dag_maker("test_notifier") as dag:
-            EmptyOperator(task_id="task1")
+    def test_notifier_with_notifier_class(self, mock_smtphook_hook, 
create_dag_without_db):
         notifier = SmtpNotifier(
             from_email="[email protected]",
             to="[email protected]",
             subject="subject",
             html_content="body",
         )
-        notifier({"dag": dag})
+        notifier({"dag": create_dag_without_db("test_notifier")})
         
mock_smtphook_hook.return_value.__enter__().send_email_smtp.assert_called_once_with(
             from_email="[email protected]",
             to="[email protected]",
@@ -90,17 +80,14 @@ class TestSmtpNotifier:
         )
 
     @mock.patch("airflow.providers.smtp.notifications.smtp.SmtpHook")
-    def test_notifier_templated(self, mock_smtphook_hook, dag_maker):
-        with dag_maker("test_notifier") as dag:
-            EmptyOperator(task_id="task1")
-
+    def test_notifier_templated(self, mock_smtphook_hook, 
create_dag_without_db):
         notifier = SmtpNotifier(
             from_email="[email protected] {{dag.dag_id}}",
             to="[email protected] {{dag.dag_id}}",
             subject="subject {{dag.dag_id}}",
             html_content="body {{dag.dag_id}}",
         )
-        context = {"dag": dag}
+        context = {"dag": create_dag_without_db("test_notifier")}
         notifier(context)
         
mock_smtphook_hook.return_value.__enter__().send_email_smtp.assert_called_once_with(
             from_email="[email protected] test_notifier",
@@ -117,9 +104,18 @@ class TestSmtpNotifier:
         )
 
     @mock.patch("airflow.providers.smtp.notifications.smtp.SmtpHook")
-    def test_notifier_with_defaults(self, mock_smtphook_hook, 
create_task_instance):
-        ti = create_task_instance(dag_id="dag", task_id="op", 
logical_date=timezone.datetime(2018, 1, 1))
-        context = {"dag": ti.dag_run.dag, "ti": ti}
+    def test_notifier_with_defaults(self, mock_smtphook_hook, 
create_dag_without_db, mock_task_instance):
+        # TODO: we can use create_runtime_ti fixture in place of 
mock_task_instance once provider has minimum AF to Airflow 3.0+
+        mock_ti = mock_task_instance(
+            dag_id="test_dag",
+            task_id="op",
+            run_id="test",
+            try_number=NUM_TRY,
+            max_tries=0,
+            state=None,
+        )
+
+        context = {"dag": create_dag_without_db("test_dag"), "ti": mock_ti}
         notifier = SmtpNotifier(
             from_email="any email",
             to="[email protected]",
@@ -130,7 +126,7 @@ class TestSmtpNotifier:
         
mock_smtphook_hook.return_value.__enter__().send_email_smtp.assert_called_once_with(
             from_email="any email",
             to="[email protected]",
-            subject="DAG dag - Task op - Run ID test in State None",
+            subject="DAG test_dag - Task op - Run ID test in State None",
             html_content=mock.ANY,
             smtp_conn_id="smtp_default",
             files=None,
@@ -144,9 +140,19 @@ class TestSmtpNotifier:
         assert f"{NUM_TRY} of 1" in content
 
     @mock.patch("airflow.providers.smtp.notifications.smtp.SmtpHook")
-    def test_notifier_with_nondefault_connection_extra(self, 
mock_smtphook_hook, create_task_instance):
-        ti = create_task_instance(dag_id="dag", task_id="op", 
logical_date=timezone.datetime(2018, 1, 1))
-        context = {"dag": ti.dag_run.dag, "ti": ti}
+    def test_notifier_with_nondefault_connection_extra(
+        self, mock_smtphook_hook, create_dag_without_db, mock_task_instance
+    ):
+        # TODO: we can use create_runtime_ti fixture in place of 
mock_task_instance once provider has minimum AF to Airflow 3.0+
+        ti = mock_task_instance(
+            dag_id="test_dag",
+            task_id="op",
+            run_id="test_run",
+            try_number=NUM_TRY,
+            max_tries=0,
+            state=None,
+        )
+        context = {"dag": create_dag_without_db("test_dag"), "ti": ti}
 
         with (
             tempfile.NamedTemporaryFile(mode="wt", suffix=".txt") as f_subject,
@@ -179,10 +185,7 @@ class TestSmtpNotifier:
             )
 
     @mock.patch("airflow.providers.smtp.notifications.smtp.SmtpHook")
-    def test_notifier_oauth2_passes_auth_type(self, mock_smtphook_hook, 
dag_maker):
-        with dag_maker("test_notifier_oauth2") as dag:
-            EmptyOperator(task_id="task1")
-
+    def test_notifier_oauth2_passes_auth_type(self, mock_smtphook_hook, 
create_dag_without_db):
         notifier = SmtpNotifier(
             from_email="[email protected]",
             to="[email protected]",
@@ -191,7 +194,7 @@ class TestSmtpNotifier:
             html_content="body",
         )
 
-        notifier({"dag": dag})
+        notifier({"dag": create_dag_without_db("test_notifier")})
 
         mock_smtphook_hook.assert_called_once_with(
             smtp_conn_id="smtp_default",

Reply via email to