Skip to content

Commit 0f5ca24

Browse files
committed
test connection failure log
Signed-off-by: Sai Shree Pradhan <saishree.pradhan@databricks.com>
1 parent 474bdfc commit 0f5ca24

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

src/databricks/sql/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ def read(self) -> Optional[OAuthToken]:
270270
error_message=str(e),
271271
host_url=server_hostname,
272272
http_path=http_path,
273-
port=self.session.port,
274-
user_agent=self.session.useragent_header if self.session else None,
273+
port=self.session.port if hasattr(self, "session") else None,
274+
user_agent=self.session.useragent_header
275+
if hasattr(self, "session")
276+
else None,
275277
)
276278
raise e
277279

tests/unit/test_telemetry.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -290,36 +290,36 @@ def test_factory_shutdown_flow(self):
290290
assert TelemetryClientFactory._initialized is False
291291
assert TelemetryClientFactory._executor is None
292292

293-
# @patch("databricks.sql.client.Session")
294-
# @patch("databricks.sql.telemetry.telemetry_client.TelemetryClient._send_telemetry")
295-
# def test_connection_failure_sends_correct_telemetry_payload(
296-
# self, mock_send_telemetry, mock_session
297-
# ):
298-
# """
299-
# Verify that a connection failure constructs and sends the correct
300-
# telemetry payload via _send_telemetry.
301-
# """
302-
303-
# error_message = "Could not connect to host"
304-
# mock_session.side_effect = Exception(error_message)
305-
306-
# try:
307-
# from databricks.sql.client import Connection
308-
# Connection(server_hostname="test-host", http_path="/test-path")
309-
# except Exception as e:
310-
# assert str(e) == error_message
311-
312-
# mock_send_telemetry.assert_called_once()
293+
@patch("databricks.sql.client.Session")
294+
@patch("databricks.sql.telemetry.telemetry_client.TelemetryClient._send_telemetry")
295+
def test_connection_failure_sends_correct_telemetry_payload(
296+
self, mock_send_telemetry, mock_session
297+
):
298+
"""
299+
Verify that a connection failure constructs and sends the correct
300+
telemetry payload via _send_telemetry.
301+
"""
302+
303+
error_message = "Could not connect to host"
304+
mock_session.side_effect = Exception(error_message)
305+
306+
try:
307+
from databricks.sql.client import Connection
308+
Connection(server_hostname="test-host", http_path="/test-path")
309+
except Exception as e:
310+
assert str(e) == error_message
311+
312+
mock_send_telemetry.assert_called_once()
313313

314-
# call_arguments = mock_send_telemetry.call_args.args
315-
# sent_events = call_arguments[0]
314+
call_arguments = mock_send_telemetry.call_args.args
315+
sent_events = call_arguments[0]
316316

317-
# assert len(sent_events) == 1
318-
# telemetry_log = sent_events[0]
317+
assert len(sent_events) == 1
318+
telemetry_log = sent_events[0]
319319

320-
# assert telemetry_log.entry.sql_driver_log is not None
320+
assert telemetry_log.entry.sql_driver_log is not None
321321

322-
# error_info = telemetry_log.entry.sql_driver_log.error_info
323-
# assert error_info is not None
324-
# assert error_info.error_name == "Exception"
325-
# assert error_info.stack_trace == error_message
322+
error_info = telemetry_log.entry.sql_driver_log.error_info
323+
assert error_info is not None
324+
assert error_info.error_name == "Exception"
325+
assert error_info.stack_trace == error_message

0 commit comments

Comments
 (0)