21
21
import databricks .sql
22
22
import databricks .sql .client as client
23
23
from databricks .sql import InterfaceError , DatabaseError , Error , NotSupportedError
24
- from databricks .sql .exc import RequestError , CursorAlreadyClosedError
25
24
from databricks .sql .types import Row
26
25
27
26
from databricks .sql .utils import ExecuteResponse
@@ -342,15 +341,6 @@ def test_context_manager_closes_cursor(self):
342
341
cursor .close = mock_close
343
342
mock_close .assert_called_once_with ()
344
343
345
- cursor = client .Cursor (Mock (), Mock ())
346
- cursor .close = Mock ()
347
- try :
348
- with self .assertRaises (KeyboardInterrupt ):
349
- with cursor :
350
- raise KeyboardInterrupt ("Simulated interrupt" )
351
- finally :
352
- cursor .close .assert_called ()
353
-
354
344
@patch ("%s.client.ThriftBackend" % PACKAGE_NAME )
355
345
def test_context_manager_closes_connection (self , mock_client_class ):
356
346
instance = mock_client_class .return_value
@@ -366,15 +356,6 @@ def test_context_manager_closes_connection(self, mock_client_class):
366
356
close_session_id = instance .close_session .call_args [0 ][0 ].sessionId
367
357
self .assertEqual (close_session_id , b"\x22 " )
368
358
369
- connection = databricks .sql .connect (** self .DUMMY_CONNECTION_ARGS )
370
- connection .close = Mock ()
371
- try :
372
- with self .assertRaises (KeyboardInterrupt ):
373
- with connection :
374
- raise KeyboardInterrupt ("Simulated interrupt" )
375
- finally :
376
- connection .close .assert_called ()
377
-
378
359
def dict_product (self , dicts ):
379
360
"""
380
361
Generate cartesion product of values in input dictionary, outputting a dictionary
@@ -753,42 +734,6 @@ def test_access_current_query_id(self):
753
734
cursor .close ()
754
735
self .assertIsNone (cursor .query_id )
755
736
756
- def test_cursor_close_handles_exception (self ):
757
- """Test that Cursor.close() handles exceptions from close_command properly."""
758
- mock_backend = Mock ()
759
- mock_connection = Mock ()
760
- mock_op_handle = Mock ()
761
-
762
- mock_backend .close_command .side_effect = Exception ("Test error" )
763
-
764
- cursor = client .Cursor (mock_connection , mock_backend )
765
- cursor .active_op_handle = mock_op_handle
766
-
767
- cursor .close ()
768
-
769
- mock_backend .close_command .assert_called_once_with (mock_op_handle )
770
-
771
- self .assertIsNone (cursor .active_op_handle )
772
-
773
- self .assertFalse (cursor .open )
774
-
775
- def test_cursor_context_manager_handles_exit_exception (self ):
776
- """Test that cursor's context manager handles exceptions during __exit__."""
777
- mock_backend = Mock ()
778
- mock_connection = Mock ()
779
-
780
- cursor = client .Cursor (mock_connection , mock_backend )
781
- original_close = cursor .close
782
- cursor .close = Mock (side_effect = Exception ("Test error during close" ))
783
-
784
- try :
785
- with cursor :
786
- raise ValueError ("Test error inside context" )
787
- except ValueError :
788
- pass
789
-
790
- cursor .close .assert_called_once ()
791
-
792
737
def test_connection_close_handles_cursor_close_exception (self ):
793
738
"""Test that _close handles exceptions from cursor.close() properly."""
794
739
cursors_closed = []
@@ -824,49 +769,6 @@ def mock_close_normal():
824
769
cursors_closed , [1 , 2 ], "Both cursors should have close called"
825
770
)
826
771
827
- def test_resultset_close_handles_cursor_already_closed_error (self ):
828
- """Test that ResultSet.close() handles CursorAlreadyClosedError properly."""
829
- result_set = client .ResultSet .__new__ (client .ResultSet )
830
- result_set .thrift_backend = Mock ()
831
- result_set .thrift_backend .CLOSED_OP_STATE = "CLOSED"
832
- result_set .connection = Mock ()
833
- result_set .connection .open = True
834
- result_set .op_state = "RUNNING"
835
- result_set .has_been_closed_server_side = False
836
- result_set .command_id = Mock ()
837
-
838
- class MockRequestError (Exception ):
839
- def __init__ (self ):
840
- self .args = ["Error message" , CursorAlreadyClosedError ()]
841
-
842
- result_set .thrift_backend .close_command .side_effect = MockRequestError ()
843
-
844
- original_close = client .ResultSet .close
845
- try :
846
- try :
847
- if (
848
- result_set .op_state != result_set .thrift_backend .CLOSED_OP_STATE
849
- and not result_set .has_been_closed_server_side
850
- and result_set .connection .open
851
- ):
852
- result_set .thrift_backend .close_command (result_set .command_id )
853
- except MockRequestError as e :
854
- if isinstance (e .args [1 ], CursorAlreadyClosedError ):
855
- pass
856
- finally :
857
- result_set .has_been_closed_server_side = True
858
- result_set .op_state = result_set .thrift_backend .CLOSED_OP_STATE
859
-
860
- result_set .thrift_backend .close_command .assert_called_once_with (
861
- result_set .command_id
862
- )
863
-
864
- assert result_set .has_been_closed_server_side is True
865
-
866
- assert result_set .op_state == result_set .thrift_backend .CLOSED_OP_STATE
867
- finally :
868
- pass
869
-
870
772
871
773
if __name__ == "__main__" :
872
774
suite = unittest .TestLoader ().loadTestsFromModule (sys .modules [__name__ ])
0 commit comments