|
5 | 5 | import logging |
6 | 6 | import unittest |
7 | 7 | import pytest |
8 | | -from unittest.mock import patch, MagicMock, Mock |
| 8 | +from unittest.mock import patch, MagicMock, Mock, sentinel |
9 | 9 | from ssl import CERT_NONE, CERT_REQUIRED |
10 | 10 | from urllib3 import HTTPSConnectionPool |
11 | 11 |
|
@@ -222,6 +222,64 @@ def _make_fake_thrift_backend(self): |
222 | 222 | thrift_backend._create_arrow_table.return_value = (MagicMock(), Mock()) |
223 | 223 | return thrift_backend |
224 | 224 |
|
| 225 | + @patch("databricks.sql.backend.thrift_backend.ThriftResultSet") |
| 226 | + @patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True) |
| 227 | + def test_async_result_rowcount(self, tcli_service_class, result_set): |
| 228 | + for state, count, expected_count, polled in [ |
| 229 | + (ttypes.TOperationState.FINISHED_STATE, 7, 7, True), |
| 230 | + (ttypes.TOperationState.FINISHED_STATE, 0, 0, True), |
| 231 | + (ttypes.TOperationState.FINISHED_STATE, None, None, True), |
| 232 | + (ttypes.TOperationState.FINISHED_STATE, 7, 7, False), |
| 233 | + (ttypes.TOperationState.RUNNING_STATE, 7, None, False), |
| 234 | + ]: |
| 235 | + with self.subTest(state=state, count=count, polled=polled): |
| 236 | + backend = self._make_fake_thrift_backend() |
| 237 | + backend._poll_for_status = Mock( |
| 238 | + return_value=ttypes.TGetOperationStatusResp( |
| 239 | + status=self.okay_status, |
| 240 | + operationState=state, |
| 241 | + numModifiedRows=count, |
| 242 | + ) |
| 243 | + ) |
| 244 | + backend.make_request = Mock( |
| 245 | + return_value=Mock( |
| 246 | + resultSetMetadata=self.metadata_resp, |
| 247 | + results=sentinel.counter_rows, |
| 248 | + hasMoreRows=False, |
| 249 | + ) |
| 250 | + ) |
| 251 | + cid = CommandId.from_thrift_handle(self.operation_handle) |
| 252 | + if polled: |
| 253 | + self.assertEqual( |
| 254 | + backend.get_query_state(cid), CommandState.SUCCEEDED |
| 255 | + ) |
| 256 | + backend.get_execution_result(cid, Mock()) |
| 257 | + |
| 258 | + backend._poll_for_status.assert_called_once_with(cid.to_thrift_handle()) |
| 259 | + response = result_set.call_args.kwargs["execute_response"] |
| 260 | + self.assertEqual(response.num_modified_rows, expected_count) |
| 261 | + self.assertIs( |
| 262 | + result_set.call_args.kwargs["t_row_set"], sentinel.counter_rows |
| 263 | + ) |
| 264 | + |
| 265 | + @patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True) |
| 266 | + def test_async_result_checks_status_before_fetch(self, tcli_service_class): |
| 267 | + backend = self._make_fake_thrift_backend() |
| 268 | + backend._poll_for_status = Mock( |
| 269 | + return_value=ttypes.TGetOperationStatusResp( |
| 270 | + status=self.okay_status, |
| 271 | + operationState=ttypes.TOperationState.ERROR_STATE, |
| 272 | + errorMessage="async operation failed", |
| 273 | + ) |
| 274 | + ) |
| 275 | + backend.make_request = Mock() |
| 276 | + cid = CommandId.from_thrift_handle(self.operation_handle) |
| 277 | + |
| 278 | + with self.assertRaises(ServerOperationError): |
| 279 | + backend.get_execution_result(cid, Mock()) |
| 280 | + |
| 281 | + backend.make_request.assert_not_called() |
| 282 | + |
225 | 283 | def test_hive_schema_to_arrow_schema_preserves_column_names(self): |
226 | 284 | columns = [ |
227 | 285 | ttypes.TColumnDesc( |
|
0 commit comments