|
23 | 23 | import databricks.sql |
24 | 24 | import databricks.sql.client as client |
25 | 25 | from databricks.sql import InterfaceError, DatabaseError, Error, NotSupportedError |
26 | | -from databricks.sql.exc import RequestError, CursorAlreadyClosedError |
27 | 26 | from databricks.sql.types import Row |
28 | 27 | from databricks.sql.result_set import ResultSet, ThriftResultSet |
29 | 28 | from databricks.sql.backend.types import CommandId, CommandState |
@@ -261,15 +260,20 @@ def test_context_manager_closes_cursor(self): |
261 | 260 | cursor.close = mock_close |
262 | 261 | mock_close.assert_called_once_with() |
263 | 262 |
|
264 | | - cursor = client.Cursor(Mock(), Mock()) |
265 | | - cursor.close = Mock() |
| 263 | + @patch("%s.client.ThriftBackend" % PACKAGE_NAME) |
| 264 | + def test_context_manager_closes_connection(self, mock_client_class): |
| 265 | + instance = mock_client_class.return_value |
266 | 266 |
|
267 | | - try: |
268 | | - with self.assertRaises(KeyboardInterrupt): |
269 | | - with cursor: |
270 | | - raise KeyboardInterrupt("Simulated interrupt") |
271 | | - finally: |
272 | | - cursor.close.assert_called() |
| 267 | + mock_open_session_resp = MagicMock(spec=TOpenSessionResp)() |
| 268 | + mock_open_session_resp.sessionHandle.sessionId = b"\x22" |
| 269 | + instance.open_session.return_value = mock_open_session_resp |
| 270 | + |
| 271 | + with databricks.sql.connect(**self.DUMMY_CONNECTION_ARGS) as connection: |
| 272 | + pass |
| 273 | + |
| 274 | + # Check the close session request has an id of x22 |
| 275 | + close_session_id = instance.close_session.call_args[0][0].sessionId |
| 276 | + self.assertEqual(close_session_id, b"\x22") |
273 | 277 |
|
274 | 278 | def dict_product(self, dicts): |
275 | 279 | """ |
@@ -599,42 +603,6 @@ def test_access_current_query_id(self): |
599 | 603 | cursor.close() |
600 | 604 | self.assertIsNone(cursor.query_id) |
601 | 605 |
|
602 | | - def test_cursor_close_handles_exception(self): |
603 | | - """Test that Cursor.close() handles exceptions from close_command properly.""" |
604 | | - mock_backend = Mock() |
605 | | - mock_connection = Mock() |
606 | | - mock_command_id = Mock() |
607 | | - |
608 | | - mock_backend.close_command.side_effect = Exception("Test error") |
609 | | - |
610 | | - cursor = client.Cursor(mock_connection, mock_backend) |
611 | | - cursor.active_command_id = mock_command_id |
612 | | - |
613 | | - cursor.close() |
614 | | - |
615 | | - mock_backend.close_command.assert_called_once_with(mock_command_id) |
616 | | - |
617 | | - self.assertIsNone(cursor.active_command_id) |
618 | | - |
619 | | - self.assertFalse(cursor.open) |
620 | | - |
621 | | - def test_cursor_context_manager_handles_exit_exception(self): |
622 | | - """Test that cursor's context manager handles exceptions during __exit__.""" |
623 | | - mock_backend = Mock() |
624 | | - mock_connection = Mock() |
625 | | - |
626 | | - cursor = client.Cursor(mock_connection, mock_backend) |
627 | | - original_close = cursor.close |
628 | | - cursor.close = Mock(side_effect=Exception("Test error during close")) |
629 | | - |
630 | | - try: |
631 | | - with cursor: |
632 | | - raise ValueError("Test error inside context") |
633 | | - except ValueError: |
634 | | - pass |
635 | | - |
636 | | - cursor.close.assert_called_once() |
637 | | - |
638 | 606 | def test_connection_close_handles_cursor_close_exception(self): |
639 | 607 | """Test that _close handles exceptions from cursor.close() properly.""" |
640 | 608 | cursors_closed = [] |
@@ -670,49 +638,6 @@ def mock_close_normal(): |
670 | 638 | cursors_closed, [1, 2], "Both cursors should have close called" |
671 | 639 | ) |
672 | 640 |
|
673 | | - def test_resultset_close_handles_cursor_already_closed_error(self): |
674 | | - """Test that ResultSet.close() handles CursorAlreadyClosedError properly.""" |
675 | | - result_set = client.ThriftResultSet.__new__(client.ThriftResultSet) |
676 | | - result_set.backend = Mock() |
677 | | - result_set.backend.CLOSED_OP_STATE = "CLOSED" |
678 | | - result_set.connection = Mock() |
679 | | - result_set.connection.open = True |
680 | | - result_set.op_state = "RUNNING" |
681 | | - result_set.has_been_closed_server_side = False |
682 | | - result_set.command_id = Mock() |
683 | | - |
684 | | - class MockRequestError(Exception): |
685 | | - def __init__(self): |
686 | | - self.args = ["Error message", CursorAlreadyClosedError()] |
687 | | - |
688 | | - result_set.backend.close_command.side_effect = MockRequestError() |
689 | | - |
690 | | - original_close = client.ResultSet.close |
691 | | - try: |
692 | | - try: |
693 | | - if ( |
694 | | - result_set.op_state != result_set.backend.CLOSED_OP_STATE |
695 | | - and not result_set.has_been_closed_server_side |
696 | | - and result_set.connection.open |
697 | | - ): |
698 | | - result_set.backend.close_command(result_set.command_id) |
699 | | - except MockRequestError as e: |
700 | | - if isinstance(e.args[1], CursorAlreadyClosedError): |
701 | | - pass |
702 | | - finally: |
703 | | - result_set.has_been_closed_server_side = True |
704 | | - result_set.op_state = result_set.backend.CLOSED_OP_STATE |
705 | | - |
706 | | - result_set.backend.close_command.assert_called_once_with( |
707 | | - result_set.command_id |
708 | | - ) |
709 | | - |
710 | | - assert result_set.has_been_closed_server_side is True |
711 | | - |
712 | | - assert result_set.op_state == result_set.backend.CLOSED_OP_STATE |
713 | | - finally: |
714 | | - pass |
715 | | - |
716 | 641 |
|
717 | 642 | if __name__ == "__main__": |
718 | 643 | suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) |
|
0 commit comments