22
22
from databricks .sql import InterfaceError , DatabaseError , Error , NotSupportedError
23
23
from databricks .sql .exc import RequestError , CursorAlreadyClosedError
24
24
from databricks .sql .types import Row
25
+ from databricks .sql .client import CommandId
25
26
26
27
from tests .unit .test_fetches import FetchTests
27
28
from tests .unit .test_thrift_backend import ThriftBackendTestSuite
@@ -81,7 +82,10 @@ class ClientTestSuite(unittest.TestCase):
81
82
"access_token" : "tok" ,
82
83
}
83
84
84
- @patch ("%s.session.ThriftDatabricksClient" % PACKAGE_NAME , ThriftDatabricksClientMockFactory .new ())
85
+ @patch (
86
+ "%s.session.ThriftDatabricksClient" % PACKAGE_NAME ,
87
+ ThriftDatabricksClientMockFactory .new (),
88
+ )
85
89
@patch ("%s.client.ResultSet" % PACKAGE_NAME )
86
90
def test_closing_connection_closes_commands (self , mock_result_set_class ):
87
91
# 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
286
290
def test_cancel_command_calls_the_backend (self ):
287
291
mock_thrift_backend = Mock ()
288
292
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
291
295
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 )
293
297
294
298
@patch ("databricks.sql.client.logger" )
295
299
def test_cancel_command_will_issue_warning_for_cancel_with_no_executing_command (
@@ -505,7 +509,10 @@ def test_staging_operation_response_is_handled(
505
509
506
510
mock_handle_staging_operation .call_count == 1
507
511
508
- @patch ("%s.session.ThriftDatabricksClient" % PACKAGE_NAME , ThriftDatabricksClientMockFactory .new ())
512
+ @patch (
513
+ "%s.session.ThriftDatabricksClient" % PACKAGE_NAME ,
514
+ ThriftDatabricksClientMockFactory .new (),
515
+ )
509
516
def test_access_current_query_id (self ):
510
517
operation_id = "EE6A8778-21FC-438B-92D8-96AC51EE3821"
511
518
@@ -514,9 +521,13 @@ def test_access_current_query_id(self):
514
521
515
522
self .assertIsNone (cursor .query_id )
516
523
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
+ )
520
531
)
521
532
self .assertEqual (cursor .query_id .upper (), operation_id .upper ())
522
533
@@ -527,88 +538,90 @@ def test_cursor_close_handles_exception(self):
527
538
"""Test that Cursor.close() handles exceptions from close_command properly."""
528
539
mock_backend = Mock ()
529
540
mock_connection = Mock ()
530
- mock_op_handle = Mock ()
531
-
541
+ mock_command_id = Mock ()
542
+
532
543
mock_backend .close_command .side_effect = Exception ("Test error" )
533
544
534
545
cursor = client .Cursor (mock_connection , mock_backend )
535
- cursor .active_op_handle = mock_op_handle
546
+ cursor .active_command_id = mock_command_id
536
547
537
548
cursor .close ()
538
549
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
+
543
554
self .assertFalse (cursor .open )
544
555
545
556
def test_cursor_context_manager_handles_exit_exception (self ):
546
557
"""Test that cursor's context manager handles exceptions during __exit__."""
547
558
mock_backend = Mock ()
548
559
mock_connection = Mock ()
549
-
560
+
550
561
cursor = client .Cursor (mock_connection , mock_backend )
551
562
original_close = cursor .close
552
563
cursor .close = Mock (side_effect = Exception ("Test error during close" ))
553
-
564
+
554
565
try :
555
566
with cursor :
556
567
raise ValueError ("Test error inside context" )
557
568
except ValueError :
558
569
pass
559
-
570
+
560
571
cursor .close .assert_called_once ()
561
572
562
573
def test_connection_close_handles_cursor_close_exception (self ):
563
574
"""Test that _close handles exceptions from cursor.close() properly."""
564
575
cursors_closed = []
565
-
576
+
566
577
def mock_close_with_exception ():
567
578
cursors_closed .append (1 )
568
579
raise Exception ("Test error during close" )
569
-
580
+
570
581
cursor1 = Mock ()
571
582
cursor1 .close = mock_close_with_exception
572
-
583
+
573
584
def mock_close_normal ():
574
585
cursors_closed .append (2 )
575
-
586
+
576
587
cursor2 = Mock ()
577
588
cursor2 .close = mock_close_normal
578
-
589
+
579
590
mock_backend = Mock ()
580
591
mock_session_handle = Mock ()
581
-
592
+
582
593
try :
583
594
for cursor in [cursor1 , cursor2 ]:
584
595
try :
585
596
cursor .close ()
586
597
except Exception :
587
598
pass
588
-
599
+
589
600
mock_backend .close_session (mock_session_handle )
590
601
except Exception as e :
591
602
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
+ )
594
607
595
608
def test_resultset_close_handles_cursor_already_closed_error (self ):
596
609
"""Test that ResultSet.close() handles CursorAlreadyClosedError properly."""
597
610
result_set = client .ResultSet .__new__ (client .ResultSet )
598
611
result_set .thrift_backend = Mock ()
599
- result_set .thrift_backend .CLOSED_OP_STATE = ' CLOSED'
612
+ result_set .thrift_backend .CLOSED_OP_STATE = " CLOSED"
600
613
result_set .connection = Mock ()
601
614
result_set .connection .open = True
602
- result_set .op_state = ' RUNNING'
615
+ result_set .op_state = " RUNNING"
603
616
result_set .has_been_closed_server_side = False
604
617
result_set .command_id = Mock ()
605
618
606
619
class MockRequestError (Exception ):
607
620
def __init__ (self ):
608
621
self .args = ["Error message" , CursorAlreadyClosedError ()]
609
-
622
+
610
623
result_set .thrift_backend .close_command .side_effect = MockRequestError ()
611
-
624
+
612
625
original_close = client .ResultSet .close
613
626
try :
614
627
try :
@@ -624,11 +637,13 @@ def __init__(self):
624
637
finally :
625
638
result_set .has_been_closed_server_side = True
626
639
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
+
630
645
assert result_set .has_been_closed_server_side is True
631
-
646
+
632
647
assert result_set .op_state == result_set .thrift_backend .CLOSED_OP_STATE
633
648
finally :
634
649
pass
0 commit comments