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