Skip to content

Commit c1d3be2

Browse files
introduce replacement of original has_more_rows read test
Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent 81280e7 commit c1d3be2

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

tests/unit/test_thrift_backend.py

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_make_request_checks_thrift_status_code(self):
8282
mock_method = Mock()
8383
mock_method.__name__ = "method name"
8484
mock_method.return_value = mock_response
85-
thrift_backend = self._create_fake_thrift_client()
85+
thrift_backend = self._create_thrift_client()
8686
with self.assertRaises(DatabaseError):
8787
thrift_backend.make_request(mock_method, Mock())
8888

@@ -106,7 +106,7 @@ def _create_mock_execute_response(self):
106106
mock_execute_response.result_format = Mock()
107107
return mock_execute_response
108108

109-
def _create_fake_thrift_client(self):
109+
def _create_thrift_client(self):
110110
"""Create a fake ThriftDatabricksClient without mocking any methods."""
111111
return ThriftDatabricksClient(
112112
"foobar",
@@ -119,7 +119,7 @@ def _create_fake_thrift_client(self):
119119

120120
def _create_mocked_thrift_client(self):
121121
"""Create a fake ThriftDatabricksClient with mocked methods."""
122-
thrift_backend = self._create_fake_thrift_client()
122+
thrift_backend = self._create_thrift_client()
123123
thrift_backend._hive_schema_to_arrow_schema = Mock()
124124
thrift_backend._hive_schema_to_description = Mock()
125125
thrift_backend._create_arrow_table = MagicMock()
@@ -575,7 +575,7 @@ def test_make_request_checks_status_code(self):
575575
ttypes.TStatusCode.ERROR_STATUS,
576576
ttypes.TStatusCode.INVALID_HANDLE_STATUS,
577577
]
578-
thrift_backend = self._create_fake_thrift_client()
578+
thrift_backend = self._create_thrift_client()
579579

580580
for code in error_codes:
581581
mock_error_response = Mock()
@@ -612,7 +612,7 @@ def test_handle_execute_response_checks_operation_state_in_direct_results(self):
612612
closeOperation=None,
613613
),
614614
)
615-
thrift_backend = self._create_fake_thrift_client()
615+
thrift_backend = self._create_thrift_client()
616616

617617
with self.assertRaises(DatabaseError) as cm:
618618
thrift_backend._handle_execute_response(t_execute_resp, Mock())
@@ -835,7 +835,7 @@ def test_handle_execute_response_checks_direct_results_for_error_statuses(self):
835835

836836
for error_resp in [resp_1, resp_2, resp_3, resp_4]:
837837
with self.subTest(error_resp=error_resp):
838-
thrift_backend = self._create_fake_thrift_client()
838+
thrift_backend = self._create_thrift_client()
839839

840840
with self.assertRaises(DatabaseError) as cm:
841841
thrift_backend._handle_execute_response(error_resp, Mock())
@@ -963,7 +963,7 @@ def test_use_arrow_schema_if_available(self, tcli_service_class):
963963
t_get_result_set_metadata_resp
964964
)
965965

966-
thrift_backend = self._create_fake_thrift_client()
966+
thrift_backend = self._create_thrift_client()
967967

968968
# Call the real _results_message_to_execute_response method
969969
execute_response, _ = thrift_backend._results_message_to_execute_response(
@@ -997,7 +997,7 @@ def test_fall_back_to_hive_schema_if_no_arrow_schema(self, tcli_service_class):
997997
tcli_service_instance.GetOperationStatus.return_value = op_state
998998
tcli_service_instance.GetResultSetMetadata.return_value = hive_schema_req
999999

1000-
thrift_backend = self._create_fake_thrift_client()
1000+
thrift_backend = self._create_thrift_client()
10011001
thrift_backend._hive_schema_to_arrow_schema = Mock()
10021002

10031003
# Call the real _results_message_to_execute_response method
@@ -1014,56 +1014,42 @@ def test_fall_back_to_hive_schema_if_no_arrow_schema(self, tcli_service_class):
10141014
"databricks.sql.utils.ResultSetQueueFactory.build_queue", return_value=Mock()
10151015
)
10161016
@patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True)
1017-
def test_handle_execute_response_reads_has_more_rows_in_result_response(
1017+
def test_handle_execute_response_reads_has_more_rows_in_direct_results(
10181018
self, tcli_service_class, build_queue
10191019
):
10201020
for has_more_rows, resp_type in itertools.product(
10211021
[True, False], self.execute_response_types
10221022
):
10231023
with self.subTest(has_more_rows=has_more_rows, resp_type=resp_type):
10241024
tcli_service_instance = tcli_service_class.return_value
1025-
results_mock = MagicMock()
1025+
results_mock = Mock()
10261026
results_mock.startRowOffset = 0
1027-
1028-
execute_resp = resp_type(
1029-
status=self.okay_status,
1030-
directResults=None,
1031-
operationHandle=self.operation_handle,
1032-
)
1033-
1034-
fetch_results_resp = ttypes.TFetchResultsResp(
1035-
status=self.okay_status,
1036-
hasMoreRows=has_more_rows,
1037-
results=results_mock,
1038-
resultSetMetadata=ttypes.TGetResultSetMetadataResp(
1039-
resultFormat=ttypes.TSparkRowSetType.ARROW_BASED_SET
1027+
direct_results_message = ttypes.TSparkDirectResults(
1028+
operationStatus=ttypes.TGetOperationStatusResp(
1029+
status=self.okay_status,
1030+
operationState=ttypes.TOperationState.FINISHED_STATE,
10401031
),
1032+
resultSetMetadata=self.metadata_resp,
1033+
resultSet=ttypes.TFetchResultsResp(
1034+
status=self.okay_status,
1035+
hasMoreRows=has_more_rows,
1036+
results=results_mock,
1037+
),
1038+
closeOperation=Mock(),
10411039
)
1042-
1043-
operation_status_resp = ttypes.TGetOperationStatusResp(
1040+
execute_resp = resp_type(
10441041
status=self.okay_status,
1045-
operationState=ttypes.TOperationState.FINISHED_STATE,
1046-
errorMessage="some information about the error",
1042+
directResults=direct_results_message,
1043+
operationHandle=self.operation_handle,
10471044
)
10481045

1049-
tcli_service_instance.FetchResults.return_value = fetch_results_resp
1050-
tcli_service_instance.GetOperationStatus.return_value = (
1051-
operation_status_resp
1052-
)
10531046
tcli_service_instance.GetResultSetMetadata.return_value = (
10541047
self.metadata_resp
10551048
)
1056-
thrift_backend = self._create_mocked_thrift_client()
1049+
thrift_backend = self._create_thrift_client()
10571050

1058-
thrift_backend._handle_execute_response(execute_resp, Mock())
1059-
_, has_more_rows_resp = thrift_backend.fetch_results(
1060-
command_id=Mock(),
1061-
max_rows=1,
1062-
max_bytes=1,
1063-
expected_row_start_offset=0,
1064-
lz4_compressed=False,
1065-
arrow_schema_bytes=Mock(),
1066-
description=Mock(),
1051+
_, has_more_rows_resp = thrift_backend._handle_execute_response(
1052+
execute_resp, Mock()
10671053
)
10681054

10691055
self.assertEqual(has_more_rows, has_more_rows_resp)
@@ -1351,7 +1337,7 @@ def test_open_session_user_provided_session_id_optional(self, tcli_service_class
13511337
@patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True)
13521338
def test_op_handle_respected_in_close_command(self, tcli_service_class):
13531339
tcli_service_instance = tcli_service_class.return_value
1354-
thrift_backend = self._create_fake_thrift_client()
1340+
thrift_backend = self._create_thrift_client()
13551341
command_id = CommandId.from_thrift_handle(self.operation_handle)
13561342
thrift_backend.close_command(command_id)
13571343
self.assertEqual(
@@ -1362,7 +1348,7 @@ def test_op_handle_respected_in_close_command(self, tcli_service_class):
13621348
@patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True)
13631349
def test_session_handle_respected_in_close_session(self, tcli_service_class):
13641350
tcli_service_instance = tcli_service_class.return_value
1365-
thrift_backend = self._create_fake_thrift_client()
1351+
thrift_backend = self._create_thrift_client()
13661352
session_id = SessionId.from_thrift_handle(self.session_handle)
13671353
thrift_backend.close_session(session_id)
13681354
self.assertEqual(
@@ -1399,7 +1385,7 @@ def test_non_arrow_non_column_based_set_triggers_exception(
13991385
tcli_service_instance.GetResultSetMetadata.return_value = metadata_resp
14001386
tcli_service_instance.GetOperationStatus.return_value = operation_status_resp
14011387

1402-
thrift_backend = self._create_fake_thrift_client()
1388+
thrift_backend = self._create_thrift_client()
14031389

14041390
with self.assertRaises(OperationalError) as cm:
14051391
thrift_backend.execute_command("foo", Mock(), 100, 100, Mock(), Mock())
@@ -1409,7 +1395,7 @@ def test_non_arrow_non_column_based_set_triggers_exception(
14091395

14101396
def test_create_arrow_table_raises_error_for_unsupported_type(self):
14111397
t_row_set = ttypes.TRowSet()
1412-
thrift_backend = self._create_fake_thrift_client()
1398+
thrift_backend = self._create_thrift_client()
14131399
with self.assertRaises(OperationalError):
14141400
thrift_backend._create_arrow_table(t_row_set, Mock(), None, Mock())
14151401

@@ -1422,7 +1408,7 @@ def test_create_arrow_table_raises_error_for_unsupported_type(self):
14221408
def test_create_arrow_table_calls_correct_conversion_method(
14231409
self, convert_col_mock, convert_arrow_mock
14241410
):
1425-
thrift_backend = self._create_fake_thrift_client()
1411+
thrift_backend = self._create_thrift_client()
14261412
convert_arrow_mock.return_value = (MagicMock(), Mock())
14271413
convert_col_mock.return_value = (MagicMock(), Mock())
14281414

0 commit comments

Comments
 (0)