forked from wherobots/wherobots-python-dbapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_empty_store_results.py
More file actions
229 lines (188 loc) · 7.76 KB
/
test_empty_store_results.py
File metadata and controls
229 lines (188 loc) · 7.76 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""Tests for the empty store results fix.
These tests verify that:
1. A store-configured query with empty results (result_uri=null) completes
promptly instead of hanging.
2. An execution_result event with results=null unblocks the cursor instead
of hanging forever.
"""
import json
import queue
import threading
from unittest.mock import MagicMock, patch
import pytest
from wherobots.db.connection import Connection, Query
from wherobots.db.cursor import Cursor
from wherobots.db.models import ExecutionResult, Store, StoreResult
from wherobots.db.types import EventKind, ExecutionState, StorageFormat
class TestEmptyStoreResults:
"""Tests for the primary fix: store configured, result_uri is null."""
def _make_connection_and_cursor(self):
"""Create a Connection with a mocked WebSocket and return (connection, cursor)."""
mock_ws = MagicMock()
# Prevent the background thread from running the main loop
mock_ws.protocol.state = 4 # CLOSED state, so __main_loop exits immediately
conn = Connection(mock_ws)
cursor = conn.cursor()
return conn, cursor
def test_store_configured_empty_results_completes(self):
"""When a store-configured query succeeds with result_uri=null,
the cursor should receive an empty ExecutionResult (not hang)."""
result_queue = queue.Queue()
def handler(result):
result_queue.put(result)
store = Store.for_download(format=StorageFormat.GEOJSON)
query = Query(
sql="SELECT * FROM t WHERE 1=0",
execution_id="exec-1",
state=ExecutionState.RUNNING,
handler=handler,
store=store,
)
conn, _ = self._make_connection_and_cursor()
conn._Connection__queries["exec-1"] = query
# Simulate receiving a state_updated message with result_uri=null
message = {
"kind": "state_updated",
"execution_id": "exec-1",
"state": "succeeded",
"result_uri": None,
"size": None,
}
conn._Connection__ws.recv.return_value = json.dumps(message)
conn._Connection__listen()
# The handler should have been called
result = result_queue.get(timeout=1)
assert isinstance(result, ExecutionResult)
assert result.results is None
assert result.error is None
assert result.store_result is None
assert query.state == ExecutionState.COMPLETED
def test_store_configured_with_result_uri_produces_store_result(self):
"""When a store-configured query succeeds with result_uri set,
the cursor should receive an ExecutionResult with store_result."""
result_queue = queue.Queue()
def handler(result):
result_queue.put(result)
store = Store.for_download(format=StorageFormat.GEOJSON)
query = Query(
sql="SELECT * FROM t",
execution_id="exec-2",
state=ExecutionState.RUNNING,
handler=handler,
store=store,
)
conn, _ = self._make_connection_and_cursor()
conn._Connection__queries["exec-2"] = query
message = {
"kind": "state_updated",
"execution_id": "exec-2",
"state": "succeeded",
"result_uri": "https://presigned-url.example.com/results.geojson",
"size": 12345,
}
conn._Connection__ws.recv.return_value = json.dumps(message)
conn._Connection__listen()
result = result_queue.get(timeout=1)
assert isinstance(result, ExecutionResult)
assert result.results is None
assert result.error is None
assert result.store_result is not None
assert result.store_result.result_uri == "https://presigned-url.example.com/results.geojson"
assert result.store_result.size == 12345
assert query.state == ExecutionState.COMPLETED
def test_no_store_configured_calls_request_results(self):
"""When no store is configured and result_uri is null,
__request_results should be called (existing behavior)."""
result_queue = queue.Queue()
def handler(result):
result_queue.put(result)
query = Query(
sql="SELECT 1",
execution_id="exec-3",
state=ExecutionState.RUNNING,
handler=handler,
store=None, # No store configured
)
conn, _ = self._make_connection_and_cursor()
conn._Connection__queries["exec-3"] = query
message = {
"kind": "state_updated",
"execution_id": "exec-3",
"state": "succeeded",
"result_uri": None,
"size": None,
}
conn._Connection__ws.recv.return_value = json.dumps(message)
with patch.object(conn, "_Connection__request_results") as mock_request:
conn._Connection__listen()
mock_request.assert_called_once_with("exec-3")
# Handler should NOT have been called (waiting for execution_result)
assert result_queue.empty()
class TestDefensiveNullResults:
"""Tests for the defensive fix: execution_result with results=null."""
def _make_connection_and_cursor(self):
mock_ws = MagicMock()
mock_ws.protocol.state = 4
conn = Connection(mock_ws)
cursor = conn.cursor()
return conn, cursor
def test_null_results_in_execution_result_unblocks_cursor(self):
"""When execution_result arrives with results=null,
the cursor should be unblocked with an empty ExecutionResult."""
result_queue = queue.Queue()
def handler(result):
result_queue.put(result)
query = Query(
sql="SELECT 1",
execution_id="exec-4",
state=ExecutionState.RESULTS_REQUESTED,
handler=handler,
store=None,
)
conn, _ = self._make_connection_and_cursor()
conn._Connection__queries["exec-4"] = query
# Simulate execution_result with results=null
# (this is what the server sends for store-only executions
# when retrieve_results is erroneously called)
message = {
"kind": "execution_result",
"execution_id": "exec-4",
"state": "succeeded",
"results": None,
}
conn._Connection__ws.recv.return_value = json.dumps(message)
conn._Connection__listen()
result = result_queue.get(timeout=1)
assert isinstance(result, ExecutionResult)
assert result.results is None
assert result.error is None
assert result.store_result is None
assert query.state == ExecutionState.COMPLETED
def test_empty_dict_results_in_execution_result_unblocks_cursor(self):
"""When execution_result arrives with results={} (empty dict),
the cursor should be unblocked with an empty ExecutionResult."""
result_queue = queue.Queue()
def handler(result):
result_queue.put(result)
query = Query(
sql="SELECT 1",
execution_id="exec-5",
state=ExecutionState.RESULTS_REQUESTED,
handler=handler,
store=None,
)
conn, _ = self._make_connection_and_cursor()
conn._Connection__queries["exec-5"] = query
message = {
"kind": "execution_result",
"execution_id": "exec-5",
"state": "succeeded",
"results": {},
}
conn._Connection__ws.recv.return_value = json.dumps(message)
conn._Connection__listen()
result = result_queue.get(timeout=1)
assert isinstance(result, ExecutionResult)
assert result.results is None
assert result.error is None
assert query.state == ExecutionState.COMPLETED