This is an automated email from the ASF dual-hosted git repository.
vincbeck 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 217c89325a Fix warning message in `Connection.get_hook` in case of
ImportError (#36005)
217c89325a is described below
commit 217c89325af1bad5abfea676a8bc2773a48156e3
Author: Andrey Anshin <[email protected]>
AuthorDate: Fri Dec 1 18:31:29 2023 +0400
Fix warning message in `Connection.get_hook` in case of ImportError (#36005)
---
airflow/models/connection.py | 2 +-
tests/always/test_connection.py | 29 +++++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/airflow/models/connection.py b/airflow/models/connection.py
index 4e8e3c7aaf..521a5d880e 100644
--- a/airflow/models/connection.py
+++ b/airflow/models/connection.py
@@ -364,7 +364,7 @@ class Connection(Base, LoggingMixin):
try:
hook_class = import_string(hook.hook_class_name)
except ImportError:
- warnings.warn(
+ log.error(
"Could not import %s when discovering %s %s",
hook.hook_class_name,
hook.hook_name,
diff --git a/tests/always/test_connection.py b/tests/always/test_connection.py
index e01907a8d3..bfd7360eb3 100644
--- a/tests/always/test_connection.py
+++ b/tests/always/test_connection.py
@@ -31,6 +31,7 @@ from airflow.exceptions import AirflowException
from airflow.hooks.base import BaseHook
from airflow.models import Connection, crypto
from airflow.providers.sqlite.hooks.sqlite import SqliteHook
+from airflow.providers_manager import HookInfo
from tests.test_utils.config import conf_vars
ConnectionParts = namedtuple("ConnectionParts", ["conn_type", "login",
"password", "host", "port", "schema"])
@@ -846,3 +847,31 @@ class TestConnection:
assert restored_conn.schema == conn.schema
assert restored_conn.port == conn.port
assert restored_conn.extra_dejson == conn.extra_dejson
+
+ def test_get_hook_not_found(self):
+ with mock.patch("airflow.providers_manager.ProvidersManager") as m:
+ m.return_value.hooks = {}
+ with pytest.raises(AirflowException, match='Unknown hook type
"awesome-test-conn-type"'):
+ Connection(conn_type="awesome-test-conn-type").get_hook()
+
+ def test_get_hook_import_error(self, caplog):
+ with mock.patch("airflow.providers_manager.ProvidersManager") as m:
+ m.return_value.hooks = {
+ "awesome-test-conn-type": HookInfo(
+ hook_class_name="foo.bar.AwesomeTest",
+ connection_id_attribute_name="conn-id",
+ package_name="test.package",
+ hook_name="Awesome Connection",
+ connection_type="awesome-test-conn-type",
+ connection_testable=False,
+ )
+ }
+ caplog.clear()
+ caplog.set_level("ERROR", "airflow.models.connection")
+ with pytest.raises(ImportError):
+ Connection(conn_type="awesome-test-conn-type").get_hook()
+
+ assert caplog.messages
+ assert caplog.messages[0] == (
+ "Could not import foo.bar.AwesomeTest when discovering Awesome
Connection test.package"
+ )