Skip to content

Commit f7a2276

Browse files
fixed (most) tests by accounting for normalised Session interface
have not considered how to fix the protocol version check yet
1 parent 02505e8 commit f7a2276

File tree

5 files changed

+149
-92
lines changed

5 files changed

+149
-92
lines changed

tests/e2e/test_driver.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,7 @@ def test_close_connection_closes_cursors(self):
824824
status_request = ttypes.TGetOperationStatusReq(
825825
operationHandle=ars.command_id, getProgressUpdate=False
826826
)
827-
op_status_at_server = ars.backend._client.GetOperationStatus(
828-
status_request
829-
)
827+
op_status_at_server = ars.backend._client.GetOperationStatus(status_request)
830828
assert (
831829
op_status_at_server.operationState
832830
!= ttypes.TOperationState.CLOSED_STATE
@@ -856,17 +854,19 @@ def test_closing_a_closed_connection_doesnt_fail(self, caplog):
856854
raise KeyboardInterrupt("Simulated interrupt")
857855
finally:
858856
if conn is not None:
859-
assert not conn.open, "Connection should be closed after KeyboardInterrupt"
857+
assert (
858+
not conn.open
859+
), "Connection should be closed after KeyboardInterrupt"
860860

861861
def test_cursor_close_properly_closes_operation(self):
862862
"""Test that Cursor.close() properly closes the active operation handle on the server."""
863863
with self.connection() as conn:
864864
cursor = conn.cursor()
865865
try:
866866
cursor.execute("SELECT 1 AS test")
867-
assert cursor.active_op_handle is not None
867+
assert cursor.active_command_id is not None
868868
cursor.close()
869-
assert cursor.active_op_handle is None
869+
assert cursor.active_command_id is None
870870
assert not cursor.open
871871
finally:
872872
if cursor.open:
@@ -883,26 +883,28 @@ def test_cursor_close_properly_closes_operation(self):
883883
raise KeyboardInterrupt("Simulated interrupt")
884884
finally:
885885
if cursor is not None:
886-
assert not cursor.open, "Cursor should be closed after KeyboardInterrupt"
886+
assert (
887+
not cursor.open
888+
), "Cursor should be closed after KeyboardInterrupt"
887889

888890
def test_nested_cursor_context_managers(self):
889891
"""Test that nested cursor context managers properly close operations on the server."""
890892
with self.connection() as conn:
891893
with conn.cursor() as cursor1:
892894
cursor1.execute("SELECT 1 AS test1")
893-
assert cursor1.active_op_handle is not None
895+
assert cursor1.active_command_id is not None
894896

895897
with conn.cursor() as cursor2:
896898
cursor2.execute("SELECT 2 AS test2")
897-
assert cursor2.active_op_handle is not None
899+
assert cursor2.active_command_id is not None
898900

899901
# After inner context manager exit, cursor2 should be not open
900902
assert not cursor2.open
901-
assert cursor2.active_op_handle is None
903+
assert cursor2.active_command_id is None
902904

903905
# After outer context manager exit, cursor1 should be not open
904906
assert not cursor1.open
905-
assert cursor1.active_op_handle is None
907+
assert cursor1.active_command_id is None
906908

907909
def test_cursor_error_handling(self):
908910
"""Test that cursor close handles errors properly to prevent orphaned operations."""
@@ -911,7 +913,7 @@ def test_cursor_error_handling(self):
911913

912914
cursor.execute("SELECT 1 AS test")
913915

914-
op_handle = cursor.active_op_handle
916+
op_handle = cursor.active_command_id
915917

916918
assert op_handle is not None
917919

tests/unit/test_client.py

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from databricks.sql import InterfaceError, DatabaseError, Error, NotSupportedError
2323
from databricks.sql.exc import RequestError, CursorAlreadyClosedError
2424
from databricks.sql.types import Row
25+
from databricks.sql.client import CommandId
2526

2627
from tests.unit.test_fetches import FetchTests
2728
from 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

tests/unit/test_fetches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def make_dummy_result_set_from_batch_list(batch_list):
6565
batch_index = 0
6666

6767
def fetch_results(
68-
op_handle,
68+
command_id,
6969
max_rows,
7070
max_bytes,
7171
expected_row_start_offset,

0 commit comments

Comments
 (0)