Skip to content

Commit 00a3928

Browse files
author
Jesse
authored
Configure autospec for mocked Client objects (#188)
Resolves #187 Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent b894605 commit 00a3928

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.8.x (Unreleased)
44

55
- Other: Fix typo in README quick start example
6+
- Other: Add autospec to Client mocks and tidy up `make_request`
67

78
## 2.8.0 (2023-07-21)
89

src/databricks/sql/thrift_backend.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,9 @@ def attempt_request(attempt):
335335

336336
error, error_message, retry_delay = None, None, None
337337
try:
338-
# The MagicMocks in our unit tests have a `name` property instead of `__name__`.
339338
logger.debug(
340339
"Sending request: {}(<REDACTED>)".format(
341-
getattr(
342-
method, "__name__", getattr(method, "name", "UnknownMethod")
343-
)
340+
getattr(method, "__name__")
344341
)
345342
)
346343
unsafe_logger.debug("Sending request: {}".format(request))

tests/unit/test_thrift_backend.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_hive_schema_to_arrow_schema_preserves_column_names(self):
9696
self.assertEqual(arrow_schema.field(2).name, "column 2")
9797
self.assertEqual(arrow_schema.field(3).name, "")
9898

99-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
99+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
100100
def test_bad_protocol_versions_are_rejected(self, tcli_service_client_cass):
101101
t_http_client_instance = tcli_service_client_cass.return_value
102102
bad_protocol_versions = [
@@ -123,7 +123,7 @@ def test_bad_protocol_versions_are_rejected(self, tcli_service_client_cass):
123123

124124
self.assertIn("expected server to use a protocol version", str(cm.exception))
125125

126-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
126+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
127127
def test_okay_protocol_versions_succeed(self, tcli_service_client_cass):
128128
t_http_client_instance = tcli_service_client_cass.return_value
129129
good_protocol_versions = [
@@ -351,7 +351,7 @@ def test_handle_execute_response_sets_compression_in_direct_results(self, build_
351351
execute_response = thrift_backend._handle_execute_response(t_execute_resp, Mock())
352352
self.assertEqual(execute_response.lz4_compressed, lz4Compressed)
353353

354-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
354+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
355355
def test_handle_execute_response_checks_operation_state_in_polls(self, tcli_service_class):
356356
tcli_service_instance = tcli_service_class.return_value
357357

@@ -380,7 +380,7 @@ def test_handle_execute_response_checks_operation_state_in_polls(self, tcli_serv
380380
if op_state_resp.errorMessage:
381381
self.assertIn(op_state_resp.errorMessage, str(cm.exception))
382382

383-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
383+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
384384
def test_get_status_uses_display_message_if_available(self, tcli_service_class):
385385
tcli_service_instance = tcli_service_class.return_value
386386

@@ -405,7 +405,7 @@ def test_get_status_uses_display_message_if_available(self, tcli_service_class):
405405
self.assertEqual(display_message, str(cm.exception))
406406
self.assertIn(diagnostic_info, str(cm.exception.message_with_context()))
407407

408-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
408+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
409409
def test_direct_results_uses_display_message_if_available(self, tcli_service_class):
410410
tcli_service_instance = tcli_service_class.return_value
411411

@@ -477,7 +477,7 @@ def test_handle_execute_response_checks_direct_results_for_error_statuses(self):
477477
thrift_backend._handle_execute_response(error_resp, Mock())
478478
self.assertIn("this is a bad error", str(cm.exception))
479479

480-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
480+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
481481
def test_handle_execute_response_can_handle_without_direct_results(self, tcli_service_class):
482482
tcli_service_instance = tcli_service_class.return_value
483483

@@ -542,7 +542,7 @@ def test_handle_execute_response_can_handle_with_direct_results(self):
542542
ttypes.TOperationState.FINISHED_STATE,
543543
)
544544

545-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
545+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
546546
def test_use_arrow_schema_if_available(self, tcli_service_class):
547547
tcli_service_instance = tcli_service_class.return_value
548548
arrow_schema_mock = MagicMock(name="Arrow schema mock")
@@ -566,7 +566,7 @@ def test_use_arrow_schema_if_available(self, tcli_service_class):
566566

567567
self.assertEqual(execute_response.arrow_schema_bytes, arrow_schema_mock)
568568

569-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
569+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
570570
def test_fall_back_to_hive_schema_if_no_arrow_schema(self, tcli_service_class):
571571
tcli_service_instance = tcli_service_class.return_value
572572
hive_schema_mock = MagicMock(name="Hive schema mock")
@@ -591,7 +591,7 @@ def test_fall_back_to_hive_schema_if_no_arrow_schema(self, tcli_service_class):
591591
thrift_backend._hive_schema_to_arrow_schema.call_args[0][0])
592592

593593
@patch("databricks.sql.utils.ResultSetQueueFactory.build_queue", return_value=Mock())
594-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
594+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
595595
def test_handle_execute_response_reads_has_more_rows_in_direct_results(
596596
self, tcli_service_class, build_queue):
597597
for has_more_rows, resp_type in itertools.product([True, False],
@@ -625,7 +625,7 @@ def test_handle_execute_response_reads_has_more_rows_in_direct_results(
625625
self.assertEqual(has_more_rows, execute_response.has_more_rows)
626626

627627
@patch("databricks.sql.utils.ResultSetQueueFactory.build_queue", return_value=Mock())
628-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
628+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
629629
def test_handle_execute_response_reads_has_more_rows_in_result_response(
630630
self, tcli_service_class, build_queue):
631631
for has_more_rows, resp_type in itertools.product([True, False],
@@ -671,7 +671,7 @@ def test_handle_execute_response_reads_has_more_rows_in_result_response(
671671

672672
self.assertEqual(has_more_rows, has_more_rows_resp)
673673

674-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
674+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
675675
def test_arrow_batches_row_count_are_respected(self, tcli_service_class):
676676
# make some semi-real arrow batches and check the number of rows is correct in the queue
677677
tcli_service_instance = tcli_service_class.return_value
@@ -709,7 +709,7 @@ def test_arrow_batches_row_count_are_respected(self, tcli_service_class):
709709

710710
self.assertEqual(arrow_queue.n_valid_rows, 15 * 10)
711711

712-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
712+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
713713
def test_execute_statement_calls_client_and_handle_execute_response(self, tcli_service_class):
714714
tcli_service_instance = tcli_service_class.return_value
715715
response = Mock()
@@ -727,7 +727,7 @@ def test_execute_statement_calls_client_and_handle_execute_response(self, tcli_s
727727
# Check response handling
728728
thrift_backend._handle_execute_response.assert_called_with(response, cursor_mock)
729729

730-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
730+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
731731
def test_get_catalogs_calls_client_and_handle_execute_response(self, tcli_service_class):
732732
tcli_service_instance = tcli_service_class.return_value
733733
response = Mock()
@@ -744,7 +744,7 @@ def test_get_catalogs_calls_client_and_handle_execute_response(self, tcli_servic
744744
# Check response handling
745745
thrift_backend._handle_execute_response.assert_called_with(response, cursor_mock)
746746

747-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
747+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
748748
def test_get_schemas_calls_client_and_handle_execute_response(self, tcli_service_class):
749749
tcli_service_instance = tcli_service_class.return_value
750750
response = Mock()
@@ -769,7 +769,7 @@ def test_get_schemas_calls_client_and_handle_execute_response(self, tcli_service
769769
# Check response handling
770770
thrift_backend._handle_execute_response.assert_called_with(response, cursor_mock)
771771

772-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
772+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
773773
def test_get_tables_calls_client_and_handle_execute_response(self, tcli_service_class):
774774
tcli_service_instance = tcli_service_class.return_value
775775
response = Mock()
@@ -798,7 +798,7 @@ def test_get_tables_calls_client_and_handle_execute_response(self, tcli_service_
798798
# Check response handling
799799
thrift_backend._handle_execute_response.assert_called_with(response, cursor_mock)
800800

801-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
801+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
802802
def test_get_columns_calls_client_and_handle_execute_response(self, tcli_service_class):
803803
tcli_service_instance = tcli_service_class.return_value
804804
response = Mock()
@@ -827,7 +827,7 @@ def test_get_columns_calls_client_and_handle_execute_response(self, tcli_service
827827
# Check response handling
828828
thrift_backend._handle_execute_response.assert_called_with(response, cursor_mock)
829829

830-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
830+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
831831
def test_open_session_user_provided_session_id_optional(self, tcli_service_class):
832832
tcli_service_instance = tcli_service_class.return_value
833833
tcli_service_instance.OpenSession.return_value = self.open_session_resp
@@ -836,23 +836,23 @@ def test_open_session_user_provided_session_id_optional(self, tcli_service_class
836836
thrift_backend.open_session({}, None, None)
837837
self.assertEqual(len(tcli_service_instance.OpenSession.call_args_list), 1)
838838

839-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
839+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
840840
def test_op_handle_respected_in_close_command(self, tcli_service_class):
841841
tcli_service_instance = tcli_service_class.return_value
842842
thrift_backend = ThriftBackend("foobar", 443, "path", [], auth_provider=AuthProvider())
843843
thrift_backend.close_command(self.operation_handle)
844844
self.assertEqual(tcli_service_instance.CloseOperation.call_args[0][0].operationHandle,
845845
self.operation_handle)
846846

847-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
847+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
848848
def test_session_handle_respected_in_close_session(self, tcli_service_class):
849849
tcli_service_instance = tcli_service_class.return_value
850850
thrift_backend = ThriftBackend("foobar", 443, "path", [], auth_provider=AuthProvider())
851851
thrift_backend.close_session(self.session_handle)
852852
self.assertEqual(tcli_service_instance.CloseSession.call_args[0][0].sessionHandle,
853853
self.session_handle)
854854

855-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
855+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
856856
def test_non_arrow_non_column_based_set_triggers_exception(self, tcli_service_class):
857857
tcli_service_instance = tcli_service_class.return_value
858858
results_mock = Mock()
@@ -1021,7 +1021,7 @@ def test_convert_column_based_set_to_arrow_table_uses_types_from_col_set(self):
10211021
self.assertEqual(arrow_table.column(2).to_pylist(), [1.15, 2.2, 3.3])
10221022
self.assertEqual(arrow_table.column(3).to_pylist(), [b'\x11', b'\x22', b'\x33'])
10231023

1024-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1024+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
10251025
def test_cancel_command_uses_active_op_handle(self, tcli_service_class):
10261026
tcli_service_instance = tcli_service_class.return_value
10271027

@@ -1318,7 +1318,7 @@ def test_retry_args_bounding(self, mock_http_client):
13181318
for (arg, val) in retry_delay_expected_vals.items():
13191319
self.assertEqual(getattr(backend, arg), val)
13201320

1321-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1321+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
13221322
def test_configuration_passthrough(self, tcli_client_class):
13231323
tcli_service_instance = tcli_client_class.return_value
13241324
tcli_service_instance.OpenSession.return_value = self.open_session_resp
@@ -1336,7 +1336,7 @@ def test_configuration_passthrough(self, tcli_client_class):
13361336
open_session_req = tcli_client_class.return_value.OpenSession.call_args[0][0]
13371337
self.assertEqual(open_session_req.configuration, expected_config)
13381338

1339-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1339+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
13401340
def test_cant_set_timestamp_as_string_to_true(self, tcli_client_class):
13411341
tcli_service_instance = tcli_client_class.return_value
13421342
tcli_service_instance.OpenSession.return_value = self.open_session_resp
@@ -1355,7 +1355,7 @@ def _construct_open_session_with_namespace(self, can_use_multiple_cats, cat, sch
13551355
canUseMultipleCatalogs=can_use_multiple_cats,
13561356
initialNamespace=ttypes.TNamespace(catalogName=cat, schemaName=schem))
13571357

1358-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1358+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
13591359
def test_initial_namespace_passthrough_to_open_session(self, tcli_client_class):
13601360
tcli_service_instance = tcli_client_class.return_value
13611361

@@ -1373,7 +1373,7 @@ def test_initial_namespace_passthrough_to_open_session(self, tcli_client_class):
13731373
self.assertEqual(open_session_req.initialNamespace.catalogName, cat)
13741374
self.assertEqual(open_session_req.initialNamespace.schemaName, schem)
13751375

1376-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1376+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
13771377
def test_can_use_multiple_catalogs_is_set_in_open_session_req(self, tcli_client_class):
13781378
tcli_service_instance = tcli_client_class.return_value
13791379
tcli_service_instance.OpenSession.return_value = self.open_session_resp
@@ -1384,7 +1384,7 @@ def test_can_use_multiple_catalogs_is_set_in_open_session_req(self, tcli_client_
13841384
open_session_req = tcli_client_class.return_value.OpenSession.call_args[0][0]
13851385
self.assertTrue(open_session_req.canUseMultipleCatalogs)
13861386

1387-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1387+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
13881388
def test_can_use_multiple_catalogs_is_false_fails_with_initial_catalog(self, tcli_client_class):
13891389
tcli_service_instance = tcli_client_class.return_value
13901390

@@ -1410,7 +1410,7 @@ def test_can_use_multiple_catalogs_is_false_fails_with_initial_catalog(self, tcl
14101410
self._construct_open_session_with_namespace(False, cat, schem)
14111411
backend.open_session({}, cat, schem)
14121412

1413-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1413+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
14141414
def test_protocol_v3_fails_if_initial_namespace_set(self, tcli_client_class):
14151415
tcli_service_instance = tcli_client_class.return_value
14161416

@@ -1430,7 +1430,7 @@ def test_protocol_v3_fails_if_initial_namespace_set(self, tcli_client_class):
14301430
self.assertIn("Setting initial namespace not supported by the DBR version",
14311431
str(cm.exception))
14321432

1433-
@patch("databricks.sql.thrift_backend.TCLIService.Client")
1433+
@patch("databricks.sql.thrift_backend.TCLIService.Client", autospec=True)
14341434
@patch("databricks.sql.thrift_backend.ThriftBackend._handle_execute_response")
14351435
def test_execute_command_sets_complex_type_fields_correctly(self, mock_handle_execute_response,
14361436
tcli_service_class):

0 commit comments

Comments
 (0)