|
| 1 | +""" |
| 2 | +Tests for the SeaResultSet class. |
| 3 | +
|
| 4 | +This module contains tests for the SeaResultSet class, which implements |
| 5 | +the result set functionality for the SEA (Statement Execution API) backend. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | +from unittest.mock import patch, MagicMock, Mock |
| 10 | + |
| 11 | +from databricks.sql.result_set import SeaResultSet |
| 12 | +from databricks.sql.backend.types import CommandId, CommandState, BackendType |
| 13 | + |
| 14 | + |
| 15 | +class TestSeaResultSet: |
| 16 | + """Test suite for the SeaResultSet class.""" |
| 17 | + |
| 18 | + @pytest.fixture |
| 19 | + def mock_connection(self): |
| 20 | + """Create a mock connection.""" |
| 21 | + connection = Mock() |
| 22 | + connection.open = True |
| 23 | + return connection |
| 24 | + |
| 25 | + @pytest.fixture |
| 26 | + def mock_sea_client(self): |
| 27 | + """Create a mock SEA client.""" |
| 28 | + return Mock() |
| 29 | + |
| 30 | + @pytest.fixture |
| 31 | + def execute_response(self): |
| 32 | + """Create a sample execute response.""" |
| 33 | + mock_response = Mock() |
| 34 | + mock_response.command_id = CommandId.from_sea_statement_id("test-statement-123") |
| 35 | + mock_response.status = CommandState.SUCCEEDED |
| 36 | + mock_response.has_been_closed_server_side = False |
| 37 | + mock_response.is_direct_results = False |
| 38 | + mock_response.results_queue = None |
| 39 | + mock_response.description = [ |
| 40 | + ("test_value", "INT", None, None, None, None, None) |
| 41 | + ] |
| 42 | + mock_response.is_staging_operation = False |
| 43 | + return mock_response |
| 44 | + |
| 45 | + def test_init_with_execute_response( |
| 46 | + self, mock_connection, mock_sea_client, execute_response |
| 47 | + ): |
| 48 | + """Test initializing SeaResultSet with an execute response.""" |
| 49 | + result_set = SeaResultSet( |
| 50 | + connection=mock_connection, |
| 51 | + execute_response=execute_response, |
| 52 | + sea_client=mock_sea_client, |
| 53 | + buffer_size_bytes=1000, |
| 54 | + arraysize=100, |
| 55 | + ) |
| 56 | + |
| 57 | + # Verify basic properties |
| 58 | + assert result_set.command_id == execute_response.command_id |
| 59 | + assert result_set.status == CommandState.SUCCEEDED |
| 60 | + assert result_set.connection == mock_connection |
| 61 | + assert result_set.backend == mock_sea_client |
| 62 | + assert result_set.buffer_size_bytes == 1000 |
| 63 | + assert result_set.arraysize == 100 |
| 64 | + assert result_set.description == execute_response.description |
| 65 | + |
| 66 | + def test_close(self, mock_connection, mock_sea_client, execute_response): |
| 67 | + """Test closing a result set.""" |
| 68 | + result_set = SeaResultSet( |
| 69 | + connection=mock_connection, |
| 70 | + execute_response=execute_response, |
| 71 | + sea_client=mock_sea_client, |
| 72 | + buffer_size_bytes=1000, |
| 73 | + arraysize=100, |
| 74 | + ) |
| 75 | + |
| 76 | + # Close the result set |
| 77 | + result_set.close() |
| 78 | + |
| 79 | + # Verify the backend's close_command was called |
| 80 | + mock_sea_client.close_command.assert_called_once_with(result_set.command_id) |
| 81 | + assert result_set.has_been_closed_server_side is True |
| 82 | + assert result_set.status == CommandState.CLOSED |
| 83 | + |
| 84 | + def test_close_when_already_closed_server_side( |
| 85 | + self, mock_connection, mock_sea_client, execute_response |
| 86 | + ): |
| 87 | + """Test closing a result set that has already been closed server-side.""" |
| 88 | + result_set = SeaResultSet( |
| 89 | + connection=mock_connection, |
| 90 | + execute_response=execute_response, |
| 91 | + sea_client=mock_sea_client, |
| 92 | + buffer_size_bytes=1000, |
| 93 | + arraysize=100, |
| 94 | + ) |
| 95 | + result_set.has_been_closed_server_side = True |
| 96 | + |
| 97 | + # Close the result set |
| 98 | + result_set.close() |
| 99 | + |
| 100 | + # Verify the backend's close_command was NOT called |
| 101 | + mock_sea_client.close_command.assert_not_called() |
| 102 | + assert result_set.has_been_closed_server_side is True |
| 103 | + assert result_set.status == CommandState.CLOSED |
| 104 | + |
| 105 | + def test_close_when_connection_closed( |
| 106 | + self, mock_connection, mock_sea_client, execute_response |
| 107 | + ): |
| 108 | + """Test closing a result set when the connection is closed.""" |
| 109 | + mock_connection.open = False |
| 110 | + result_set = SeaResultSet( |
| 111 | + connection=mock_connection, |
| 112 | + execute_response=execute_response, |
| 113 | + sea_client=mock_sea_client, |
| 114 | + buffer_size_bytes=1000, |
| 115 | + arraysize=100, |
| 116 | + ) |
| 117 | + |
| 118 | + # Close the result set |
| 119 | + result_set.close() |
| 120 | + |
| 121 | + # Verify the backend's close_command was NOT called |
| 122 | + mock_sea_client.close_command.assert_not_called() |
| 123 | + assert result_set.has_been_closed_server_side is True |
| 124 | + assert result_set.status == CommandState.CLOSED |
| 125 | + |
| 126 | + def test_unimplemented_methods( |
| 127 | + self, mock_connection, mock_sea_client, execute_response |
| 128 | + ): |
| 129 | + """Test that unimplemented methods raise NotImplementedError.""" |
| 130 | + result_set = SeaResultSet( |
| 131 | + connection=mock_connection, |
| 132 | + execute_response=execute_response, |
| 133 | + sea_client=mock_sea_client, |
| 134 | + buffer_size_bytes=1000, |
| 135 | + arraysize=100, |
| 136 | + ) |
| 137 | + |
| 138 | + # Test each unimplemented method individually with specific error messages |
| 139 | + with pytest.raises( |
| 140 | + NotImplementedError, match="fetchone is not implemented for SEA backend" |
| 141 | + ): |
| 142 | + result_set.fetchone() |
| 143 | + |
| 144 | + with pytest.raises( |
| 145 | + NotImplementedError, match="fetchmany is not implemented for SEA backend" |
| 146 | + ): |
| 147 | + result_set.fetchmany(10) |
| 148 | + |
| 149 | + with pytest.raises( |
| 150 | + NotImplementedError, match="fetchmany is not implemented for SEA backend" |
| 151 | + ): |
| 152 | + # Test with default parameter value |
| 153 | + result_set.fetchmany() |
| 154 | + |
| 155 | + with pytest.raises( |
| 156 | + NotImplementedError, match="fetchall is not implemented for SEA backend" |
| 157 | + ): |
| 158 | + result_set.fetchall() |
| 159 | + |
| 160 | + with pytest.raises( |
| 161 | + NotImplementedError, |
| 162 | + match="fetchmany_arrow is not implemented for SEA backend", |
| 163 | + ): |
| 164 | + result_set.fetchmany_arrow(10) |
| 165 | + |
| 166 | + with pytest.raises( |
| 167 | + NotImplementedError, |
| 168 | + match="fetchall_arrow is not implemented for SEA backend", |
| 169 | + ): |
| 170 | + result_set.fetchall_arrow() |
| 171 | + |
| 172 | + with pytest.raises( |
| 173 | + NotImplementedError, match="fetchone is not implemented for SEA backend" |
| 174 | + ): |
| 175 | + # Test iteration protocol (calls fetchone internally) |
| 176 | + next(iter(result_set)) |
| 177 | + |
| 178 | + with pytest.raises( |
| 179 | + NotImplementedError, match="fetchone is not implemented for SEA backend" |
| 180 | + ): |
| 181 | + # Test using the result set in a for loop |
| 182 | + for row in result_set: |
| 183 | + pass |
| 184 | + |
| 185 | + def test_fill_results_buffer_not_implemented( |
| 186 | + self, mock_connection, mock_sea_client, execute_response |
| 187 | + ): |
| 188 | + """Test that _fill_results_buffer raises NotImplementedError.""" |
| 189 | + result_set = SeaResultSet( |
| 190 | + connection=mock_connection, |
| 191 | + execute_response=execute_response, |
| 192 | + sea_client=mock_sea_client, |
| 193 | + buffer_size_bytes=1000, |
| 194 | + arraysize=100, |
| 195 | + ) |
| 196 | + |
| 197 | + with pytest.raises( |
| 198 | + NotImplementedError, |
| 199 | + match="_fill_results_buffer is not implemented for SEA backend", |
| 200 | + ): |
| 201 | + result_set._fill_results_buffer() |
0 commit comments