2222from databricks .sql import InterfaceError , DatabaseError , Error , NotSupportedError
2323from databricks .sql .exc import RequestError , CursorAlreadyClosedError
2424from databricks .sql .types import Row
25+ from databricks .sql .client import CommandId
2526
2627from tests .unit .test_fetches import FetchTests
2728from tests .unit .test_thrift_backend import ThriftBackendTestSuite
@@ -81,7 +82,10 @@ class ClientTestSuite(unittest.TestCase):
8182 "access_token" : "tok" ,
8283 }
8384
84- @patch ("%s.session.ThriftDatabricksClient" % PACKAGE_NAME , ThriftDatabricksClientMockFactory .new ())
85+ @patch (
86+ "%s.session.ThriftDatabricksClient" % PACKAGE_NAME ,
87+ ThriftDatabricksClientMockFactory .new (),
88+ )
8589 @patch ("%s.client.ResultSet" % PACKAGE_NAME )
8690 def test_closing_connection_closes_commands (self , mock_result_set_class ):
8791 # Test once with has_been_closed_server side, once without
@@ -286,10 +290,10 @@ def test_get_columns_parameters_passed_to_thrift_backend(self, mock_thrift_backe
286290 def test_cancel_command_calls_the_backend (self ):
287291 mock_thrift_backend = Mock ()
288292 cursor = client .Cursor (Mock (), mock_thrift_backend )
289- mock_op_handle = Mock ()
290- cursor .active_op_handle = mock_op_handle
293+ mock_command_id = Mock ()
294+ cursor .active_command_id = mock_command_id
291295 cursor .cancel ()
292- mock_thrift_backend .cancel_command .assert_called_with (mock_op_handle )
296+ mock_thrift_backend .cancel_command .assert_called_with (mock_command_id )
293297
294298 @patch ("databricks.sql.client.logger" )
295299 def test_cancel_command_will_issue_warning_for_cancel_with_no_executing_command (
@@ -505,7 +509,10 @@ def test_staging_operation_response_is_handled(
505509
506510 mock_handle_staging_operation .call_count == 1
507511
508- @patch ("%s.session.ThriftDatabricksClient" % PACKAGE_NAME , ThriftDatabricksClientMockFactory .new ())
512+ @patch (
513+ "%s.session.ThriftDatabricksClient" % PACKAGE_NAME ,
514+ ThriftDatabricksClientMockFactory .new (),
515+ )
509516 def test_access_current_query_id (self ):
510517 operation_id = "EE6A8778-21FC-438B-92D8-96AC51EE3821"
511518
@@ -514,9 +521,13 @@ def test_access_current_query_id(self):
514521
515522 self .assertIsNone (cursor .query_id )
516523
517- cursor .active_op_handle = TOperationHandle (
518- operationId = THandleIdentifier (guid = UUID (operation_id ).bytes , secret = 0x00 ),
519- operationType = TOperationType .EXECUTE_STATEMENT ,
524+ cursor .active_command_id = CommandId .from_thrift_handle (
525+ TOperationHandle (
526+ operationId = THandleIdentifier (
527+ guid = UUID (operation_id ).bytes , secret = 0x00
528+ ),
529+ operationType = TOperationType .EXECUTE_STATEMENT ,
530+ )
520531 )
521532 self .assertEqual (cursor .query_id .upper (), operation_id .upper ())
522533
@@ -527,88 +538,90 @@ def test_cursor_close_handles_exception(self):
527538 """Test that Cursor.close() handles exceptions from close_command properly."""
528539 mock_backend = Mock ()
529540 mock_connection = Mock ()
530- mock_op_handle = Mock ()
531-
541+ mock_command_id = Mock ()
542+
532543 mock_backend .close_command .side_effect = Exception ("Test error" )
533544
534545 cursor = client .Cursor (mock_connection , mock_backend )
535- cursor .active_op_handle = mock_op_handle
546+ cursor .active_command_id = mock_command_id
536547
537548 cursor .close ()
538549
539- mock_backend .close_command .assert_called_once_with (mock_op_handle )
540-
541- self .assertIsNone (cursor .active_op_handle )
542-
550+ mock_backend .close_command .assert_called_once_with (mock_command_id )
551+
552+ self .assertIsNone (cursor .active_command_id )
553+
543554 self .assertFalse (cursor .open )
544555
545556 def test_cursor_context_manager_handles_exit_exception (self ):
546557 """Test that cursor's context manager handles exceptions during __exit__."""
547558 mock_backend = Mock ()
548559 mock_connection = Mock ()
549-
560+
550561 cursor = client .Cursor (mock_connection , mock_backend )
551562 original_close = cursor .close
552563 cursor .close = Mock (side_effect = Exception ("Test error during close" ))
553-
564+
554565 try :
555566 with cursor :
556567 raise ValueError ("Test error inside context" )
557568 except ValueError :
558569 pass
559-
570+
560571 cursor .close .assert_called_once ()
561572
562573 def test_connection_close_handles_cursor_close_exception (self ):
563574 """Test that _close handles exceptions from cursor.close() properly."""
564575 cursors_closed = []
565-
576+
566577 def mock_close_with_exception ():
567578 cursors_closed .append (1 )
568579 raise Exception ("Test error during close" )
569-
580+
570581 cursor1 = Mock ()
571582 cursor1 .close = mock_close_with_exception
572-
583+
573584 def mock_close_normal ():
574585 cursors_closed .append (2 )
575-
586+
576587 cursor2 = Mock ()
577588 cursor2 .close = mock_close_normal
578-
589+
579590 mock_backend = Mock ()
580591 mock_session_handle = Mock ()
581-
592+
582593 try :
583594 for cursor in [cursor1 , cursor2 ]:
584595 try :
585596 cursor .close ()
586597 except Exception :
587598 pass
588-
599+
589600 mock_backend .close_session (mock_session_handle )
590601 except Exception as e :
591602 self .fail (f"Connection close should handle exceptions: { e } " )
592-
593- self .assertEqual (cursors_closed , [1 , 2 ], "Both cursors should have close called" )
603+
604+ self .assertEqual (
605+ cursors_closed , [1 , 2 ], "Both cursors should have close called"
606+ )
594607
595608 def test_resultset_close_handles_cursor_already_closed_error (self ):
596609 """Test that ResultSet.close() handles CursorAlreadyClosedError properly."""
597610 result_set = client .ResultSet .__new__ (client .ResultSet )
598611 result_set .thrift_backend = Mock ()
599- result_set .thrift_backend .CLOSED_OP_STATE = ' CLOSED'
612+ result_set .thrift_backend .CLOSED_OP_STATE = " CLOSED"
600613 result_set .connection = Mock ()
601614 result_set .connection .open = True
602- result_set .op_state = ' RUNNING'
615+ result_set .op_state = " RUNNING"
603616 result_set .has_been_closed_server_side = False
604617 result_set .command_id = Mock ()
605618
606619 class MockRequestError (Exception ):
607620 def __init__ (self ):
608621 self .args = ["Error message" , CursorAlreadyClosedError ()]
609-
622+
610623 result_set .thrift_backend .close_command .side_effect = MockRequestError ()
611-
624+
612625 original_close = client .ResultSet .close
613626 try :
614627 try :
@@ -624,11 +637,13 @@ def __init__(self):
624637 finally :
625638 result_set .has_been_closed_server_side = True
626639 result_set .op_state = result_set .thrift_backend .CLOSED_OP_STATE
627-
628- result_set .thrift_backend .close_command .assert_called_once_with (result_set .command_id )
629-
640+
641+ result_set .thrift_backend .close_command .assert_called_once_with (
642+ result_set .command_id
643+ )
644+
630645 assert result_set .has_been_closed_server_side is True
631-
646+
632647 assert result_set .op_state == result_set .thrift_backend .CLOSED_OP_STATE
633648 finally :
634649 pass
0 commit comments