Skip to content

Commit 149d4a8

Browse files
committed
added mocks to resolve the errors caused by log_latency decorator in tests
Signed-off-by: Sai Shree Pradhan <saishree.pradhan@databricks.com>
1 parent 45f74d0 commit 149d4a8

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

tests/unit/test_client.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@
88
from datetime import datetime, date
99
from uuid import UUID
1010

11-
def noop_log_latency_decorator(*args, **kwargs):
12-
"""
13-
This is a no-op decorator. It is used to patch the log_latency decorator
14-
during tests, so that the tests for client logic are not affected by the
15-
telemetry logging logic. It accepts any arguments and returns a decorator
16-
that returns the original function unmodified.
17-
"""
18-
def decorator(func):
19-
return func
20-
return decorator
21-
22-
patch('databricks.sql.telemetry.latency_logger.log_latency', new=noop_log_latency_decorator).start()
23-
2411
from databricks.sql.thrift_api.TCLIService.ttypes import (
2512
TOpenSessionResp,
2613
TExecuteStatementResp,
@@ -51,6 +38,10 @@ def new(cls):
5138
cls.apply_property_to_mock(ThriftBackendMock, staging_allowed_local_path=None)
5239
MockTExecuteStatementResp = MagicMock(spec=TExecuteStatementResp())
5340

41+
mock_retry_policy = Mock()
42+
mock_retry_policy.history = []
43+
cls.apply_property_to_mock(ThriftBackendMock, retry_policy=mock_retry_policy)
44+
5445
cls.apply_property_to_mock(
5546
MockTExecuteStatementResp,
5647
description=None,
@@ -331,7 +322,7 @@ def test_executing_multiple_commands_uses_the_most_recent_command(
331322
mock_result_sets[1].fetchall.assert_called_once_with()
332323

333324
def test_closed_cursor_doesnt_allow_operations(self):
334-
cursor = client.Cursor(Mock(), Mock())
325+
cursor = client.Cursor(Mock(), ThriftBackendMockFactory.new())
335326
cursor.close()
336327

337328
with self.assertRaises(Error) as e:
@@ -343,14 +334,19 @@ def test_closed_cursor_doesnt_allow_operations(self):
343334
self.assertIn("closed", e.msg)
344335

345336
def test_negative_fetch_throws_exception(self):
346-
result_set = client.ResultSet(Mock(), Mock(), Mock())
337+
mock_connection = Mock()
338+
mock_connection.get_session_id_hex.return_value = "test_session"
339+
mock_execute_response = Mock()
340+
mock_execute_response.command_handle = None
341+
342+
result_set = client.ResultSet(mock_connection, mock_execute_response, ThriftBackendMockFactory.new())
347343

348344
with self.assertRaises(ValueError) as e:
349345
result_set.fetchmany(-1)
350346

351347
def test_context_manager_closes_cursor(self):
352348
mock_close = Mock()
353-
with client.Cursor(Mock(), Mock()) as cursor:
349+
with client.Cursor(Mock(), ThriftBackendMockFactory.new()) as cursor:
354350
cursor.close = mock_close
355351
mock_close.assert_called_once_with()
356352

@@ -393,7 +389,7 @@ def test_get_schemas_parameters_passed_to_thrift_backend(self, mock_thrift_backe
393389
for req_args in req_args_combinations:
394390
req_args = {k: v for k, v in req_args.items() if v != "NOT_SET"}
395391
with self.subTest(req_args=req_args):
396-
mock_thrift_backend = Mock()
392+
mock_thrift_backend = ThriftBackendMockFactory.new()
397393

398394
cursor = client.Cursor(Mock(), mock_thrift_backend)
399395
cursor.schemas(**req_args)
@@ -416,7 +412,7 @@ def test_get_tables_parameters_passed_to_thrift_backend(self, mock_thrift_backen
416412
for req_args in req_args_combinations:
417413
req_args = {k: v for k, v in req_args.items() if v != "NOT_SET"}
418414
with self.subTest(req_args=req_args):
419-
mock_thrift_backend = Mock()
415+
mock_thrift_backend = ThriftBackendMockFactory.new()
420416

421417
cursor = client.Cursor(Mock(), mock_thrift_backend)
422418
cursor.tables(**req_args)
@@ -439,7 +435,7 @@ def test_get_columns_parameters_passed_to_thrift_backend(self, mock_thrift_backe
439435
for req_args in req_args_combinations:
440436
req_args = {k: v for k, v in req_args.items() if v != "NOT_SET"}
441437
with self.subTest(req_args=req_args):
442-
mock_thrift_backend = Mock()
438+
mock_thrift_backend = ThriftBackendMockFactory.new()
443439

444440
cursor = client.Cursor(Mock(), mock_thrift_backend)
445441
cursor.columns(**req_args)

tests/unit/test_fetches.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@
77
except ImportError:
88
pa = None
99

10-
def noop_log_latency_decorator(*args, **kwargs):
11-
"""
12-
This is a no-op decorator. It is used to patch the log_latency decorator
13-
during tests, so that the tests for fetches logic are not affected by the
14-
telemetry logging logic. It accepts any arguments and returns a decorator
15-
that returns the original function unmodified.
16-
"""
17-
def decorator(func):
18-
return func
19-
return decorator
20-
21-
patch('databricks.sql.telemetry.latency_logger.log_latency', new=noop_log_latency_decorator).start()
22-
2310
import databricks.sql.client as client
2411
from databricks.sql.utils import ExecuteResponse, ArrowQueue
2512

@@ -71,6 +58,14 @@ def make_dummy_result_set_from_initial_results(initial_results):
7158
for col_id in range(num_cols)
7259
]
7360
return rs
61+
62+
@staticmethod
63+
def mock_thrift_backend_with_retry_policy():
64+
mock_thrift_backend = Mock()
65+
mock_retry_policy = Mock()
66+
mock_retry_policy.history = []
67+
mock_thrift_backend.retry_policy = mock_retry_policy
68+
return mock_thrift_backend
7469

7570
@staticmethod
7671
def make_dummy_result_set_from_batch_list(batch_list):
@@ -92,7 +87,7 @@ def fetch_results(
9287

9388
return results, batch_index < len(batch_list)
9489

95-
mock_thrift_backend = Mock()
90+
mock_thrift_backend = FetchTests.mock_thrift_backend_with_retry_policy()
9691
mock_thrift_backend.fetch_results = fetch_results
9792
num_cols = len(batch_list[0][0]) if batch_list and batch_list[0] else 0
9893

0 commit comments

Comments
 (0)