|
1 | 1 | """ |
2 | 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. |
3 | 6 | """ |
4 | 7 |
|
5 | 8 | import pytest |
6 | | -import unittest |
7 | 9 | from unittest.mock import patch, MagicMock, Mock |
8 | 10 |
|
9 | 11 | from databricks.sql.result_set import SeaResultSet |
10 | 12 | from databricks.sql.backend.types import CommandId, CommandState, BackendType |
11 | 13 |
|
12 | 14 |
|
13 | | -class TestSeaResultSet(unittest.TestCase): |
14 | | - """Tests for the SeaResultSet class.""" |
| 15 | +class TestSeaResultSet: |
| 16 | + """Test suite for the SeaResultSet class.""" |
15 | 17 |
|
16 | 18 | @pytest.fixture |
17 | 19 | def mock_connection(self): |
@@ -40,130 +42,81 @@ def execute_response(self): |
40 | 42 | mock_response.is_staging_operation = False |
41 | 43 | return mock_response |
42 | 44 |
|
43 | | - def test_init_with_inline_data(self): |
44 | | - """Test initialization with inline data.""" |
45 | | - # Create mock result data and manifest |
46 | | - from databricks.sql.backend.sea.models.base import ResultData, ResultManifest |
47 | | - |
48 | | - result_data = ResultData( |
49 | | - data=[[1, "Alice"], [2, "Bob"], [3, "Charlie"]], external_links=None |
50 | | - ) |
51 | | - manifest = ResultManifest( |
52 | | - format="JSON_ARRAY", |
53 | | - schema={}, |
54 | | - total_row_count=3, |
55 | | - total_byte_count=0, |
56 | | - total_chunk_count=1, |
57 | | - truncated=False, |
58 | | - chunks=None, |
59 | | - result_compression=None, |
60 | | - ) |
61 | | - |
| 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.""" |
62 | 49 | result_set = SeaResultSet( |
63 | | - connection=self.mock_connection, |
64 | | - execute_response=self.mock_execute_response_inline, |
65 | | - sea_client=self.mock_backend, |
| 50 | + connection=mock_connection, |
| 51 | + execute_response=execute_response, |
| 52 | + sea_client=mock_sea_client, |
66 | 53 | buffer_size_bytes=1000, |
67 | 54 | arraysize=100, |
68 | | - result_data=result_data, |
69 | | - manifest=manifest, |
70 | 55 | ) |
71 | 56 |
|
72 | | - # Check properties |
73 | | - self.assertEqual(result_set.backend, self.mock_backend) |
74 | | - self.assertEqual(result_set.buffer_size_bytes, 1000) |
75 | | - self.assertEqual(result_set.arraysize, 100) |
76 | | - |
77 | | - # Check statement ID |
78 | | - self.assertEqual(result_set.statement_id, "test-statement-id") |
79 | | - |
80 | | - # Check status |
81 | | - self.assertEqual(result_set.status, CommandState.SUCCEEDED) |
82 | | - |
83 | | - # Check description |
84 | | - self.assertEqual(result_set.description, self.sample_description) |
85 | | - |
86 | | - # Check results queue |
87 | | - self.assertTrue(isinstance(result_set.results, JsonQueue)) |
88 | | - |
89 | | - def test_init_without_result_data(self): |
90 | | - """Test initialization without result data.""" |
91 | | - # Create a result set without providing result_data |
| 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.""" |
92 | 68 | result_set = SeaResultSet( |
93 | | - connection=self.mock_connection, |
94 | | - execute_response=self.mock_execute_response_inline, |
95 | | - sea_client=self.mock_backend, |
| 69 | + connection=mock_connection, |
| 70 | + execute_response=execute_response, |
| 71 | + sea_client=mock_sea_client, |
96 | 72 | buffer_size_bytes=1000, |
97 | 73 | arraysize=100, |
98 | 74 | ) |
99 | 75 |
|
100 | | - # Check properties |
101 | | - self.assertEqual(result_set.backend, self.mock_backend) |
102 | | - self.assertEqual(result_set.statement_id, "test-statement-id") |
103 | | - self.assertEqual(result_set.status, CommandState.SUCCEEDED) |
104 | | - self.assertEqual(result_set.description, self.sample_description) |
105 | | - self.assertTrue(isinstance(result_set.results, JsonQueue)) |
106 | | - |
107 | | - # Verify that the results queue is empty |
108 | | - self.assertEqual(result_set.results.data_array, []) |
109 | | - |
110 | | - def test_init_with_error(self): |
111 | | - """Test initialization with error response.""" |
112 | | - result_set = SeaResultSet( |
113 | | - connection=self.mock_connection, |
114 | | - execute_response=self.mock_execute_response_error, |
115 | | - sea_client=self.mock_backend, |
116 | | - ) |
| 76 | + # Close the result set |
| 77 | + result_set.close() |
117 | 78 |
|
118 | | - # Check status |
119 | | - self.assertEqual(result_set.status, CommandState.FAILED) |
120 | | - |
121 | | - # Check that description is None |
122 | | - self.assertIsNone(result_set.description) |
123 | | - |
124 | | - def test_close(self): |
125 | | - """Test closing the result set.""" |
126 | | - # Setup |
127 | | - from databricks.sql.backend.sea.models.base import ResultData, ResultManifest |
128 | | - |
129 | | - result_data = ResultData(data=[[1, "Alice"]], external_links=None) |
130 | | - manifest = ResultManifest( |
131 | | - format="JSON_ARRAY", |
132 | | - schema={}, |
133 | | - total_row_count=1, |
134 | | - total_byte_count=0, |
135 | | - total_chunk_count=1, |
136 | | - truncated=False, |
137 | | - chunks=None, |
138 | | - result_compression=None, |
139 | | - ) |
| 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 |
140 | 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.""" |
141 | 88 | result_set = SeaResultSet( |
142 | | - connection=self.mock_connection, |
143 | | - execute_response=self.mock_execute_response_inline, |
144 | | - sea_client=self.mock_backend, |
145 | | - result_data=result_data, |
146 | | - manifest=manifest, |
| 89 | + connection=mock_connection, |
| 90 | + execute_response=execute_response, |
| 91 | + sea_client=mock_sea_client, |
| 92 | + buffer_size_bytes=1000, |
| 93 | + arraysize=100, |
147 | 94 | ) |
| 95 | + result_set.has_been_closed_server_side = True |
148 | 96 |
|
149 | | - # Mock the backend's close_command method |
150 | | - self.mock_backend.close_command = MagicMock() |
151 | | - |
152 | | - # Execute |
| 97 | + # Close the result set |
153 | 98 | result_set.close() |
154 | 99 |
|
155 | | - # Verify |
156 | | - self.mock_backend.close_command.assert_called_once_with(self.mock_command_id) |
| 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 |
157 | 104 |
|
158 | | - def test_is_staging_operation(self): |
159 | | - """Test is_staging_operation property.""" |
| 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 |
160 | 110 | result_set = SeaResultSet( |
161 | | - connection=self.mock_connection, |
162 | | - execute_response=self.mock_execute_response_inline, |
163 | | - sea_client=self.mock_backend, |
| 111 | + connection=mock_connection, |
| 112 | + execute_response=execute_response, |
| 113 | + sea_client=mock_sea_client, |
| 114 | + buffer_size_bytes=1000, |
| 115 | + arraysize=100, |
164 | 116 | ) |
165 | 117 |
|
166 | | - self.assertFalse(result_set.is_staging_operation) |
| 118 | + # Close the result set |
| 119 | + result_set.close() |
167 | 120 |
|
168 | 121 | # Verify the backend's close_command was NOT called |
169 | 122 | mock_sea_client.close_command.assert_not_called() |
|
0 commit comments