Skip to content

Commit

Permalink
Add test_connection method to Trino hook (apache#24583)
Browse files Browse the repository at this point in the history
  • Loading branch information
phanikumv authored Jun 21, 2022
1 parent 14893b8 commit 122d2f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions airflow/providers/trino/hooks/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,19 @@ def insert_rows(
commit_every = 0

super().insert_rows(table, rows, target_fields, commit_every, replace)

def test_connection(self):
"""Tests the connection from UI using Trino specific query"""
status, message = False, ''
try:
with closing(self.get_conn()) as conn:
with closing(conn.cursor()) as cur:
cur.execute("select 1")
if cur.fetchone():
status = True
message = 'Connection successfully tested'
except Exception as e:
status = False
message = str(e)

return status, message
13 changes: 13 additions & 0 deletions tests/providers/trino/hooks/test_trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ def test_run(self, mock_run):
self.db_hook.run(sql, autocommit, parameters, list)
mock_run.assert_called_once_with(sql, autocommit, parameters, handler)

def test_connection_success(self):
status, msg = self.db_hook.test_connection()
assert status is True
assert msg == 'Connection successfully tested'

@patch('airflow.providers.trino.hooks.trino.TrinoHook.get_conn')
def test_connection_failure(self, mock_conn):
mock_conn.side_effect = Exception('Test')
self.db_hook.get_conn = mock_conn
status, msg = self.db_hook.test_connection()
assert status is False
assert msg == 'Test'


class TestTrinoHookIntegration(unittest.TestCase):
@pytest.mark.integration("trino")
Expand Down

0 comments on commit 122d2f6

Please sign in to comment.