|
6 | 6 | 2. Pyformat parameter substitution (%(name)s) works correctly with |
7 | 7 | type-aware SQL quoting. |
8 | 8 | 3. Unknown parameter keys raise ProgrammingError. |
| 9 | +4. Fetches only ever observe the most recent execution's result set, and |
| 10 | + re-executing never cancels an already-completed statement (WBC-922). |
9 | 11 | """ |
10 | 12 |
|
11 | 13 | from datetime import date |
12 | 14 |
|
| 15 | +import pandas |
13 | 16 | import pytest |
14 | 17 | from unittest.mock import MagicMock |
15 | 18 |
|
16 | 19 | from wherobots.db.cursor import Cursor, _substitute_parameters, _quote_value |
17 | 20 | from wherobots.db.errors import ProgrammingError |
| 21 | +from wherobots.db.models import ExecutionResult |
18 | 22 |
|
19 | 23 |
|
20 | 24 | def _make_cursor(): |
@@ -211,6 +215,114 @@ def test_unknown_parameter_raises(self): |
211 | 215 | cursor.execute(sql, parameters={"id": 42}) |
212 | 216 |
|
213 | 217 |
|
| 218 | +# --------------------------------------------------------------------------- |
| 219 | +# Result isolation and cancellation tests (WBC-922) |
| 220 | +# --------------------------------------------------------------------------- |
| 221 | + |
| 222 | + |
| 223 | +def _make_async_cursor(): |
| 224 | + """Create a Cursor whose exec_fn records each execution's handler. |
| 225 | +
|
| 226 | + Tests deliver results by invoking a recorded handler, mimicking the |
| 227 | + connection's asynchronous result callbacks. |
| 228 | + """ |
| 229 | + handlers = [] |
| 230 | + |
| 231 | + def exec_fn(sql, handler, store): |
| 232 | + handlers.append(handler) |
| 233 | + return f"exec-{len(handlers)}" |
| 234 | + |
| 235 | + cancel_fn = MagicMock() |
| 236 | + return Cursor(exec_fn, cancel_fn), handlers, cancel_fn |
| 237 | + |
| 238 | + |
| 239 | +class TestCursorResultIsolation: |
| 240 | + """Fetches must only observe the most recent execution's result set.""" |
| 241 | + |
| 242 | + def test_unfetched_result_does_not_leak_into_next_execute(self): |
| 243 | + cursor, handlers, _ = _make_async_cursor() |
| 244 | + |
| 245 | + cursor.execute("SELECT 1") |
| 246 | + handlers[0](ExecutionResult(results=pandas.DataFrame({"x": [1]}))) |
| 247 | + |
| 248 | + # Re-execute without fetching the first result. |
| 249 | + cursor.execute("SELECT 2") |
| 250 | + handlers[1](ExecutionResult(results=pandas.DataFrame({"x": [2]}))) |
| 251 | + |
| 252 | + assert cursor.fetchall()["x"].tolist() == [2] |
| 253 | + |
| 254 | + def test_late_result_from_superseded_execution_is_ignored(self): |
| 255 | + cursor, handlers, _ = _make_async_cursor() |
| 256 | + |
| 257 | + cursor.execute("SELECT 1") |
| 258 | + # First query still in flight when the second is executed. |
| 259 | + cursor.execute("SELECT 2") |
| 260 | + |
| 261 | + # The first query's result arrives late (e.g. the empty result the |
| 262 | + # connection delivers for a cancelled query), then the second's. |
| 263 | + handlers[0](ExecutionResult(results=pandas.DataFrame())) |
| 264 | + handlers[1](ExecutionResult(results=pandas.DataFrame({"x": [2]}))) |
| 265 | + |
| 266 | + assert cursor.fetchall()["x"].tolist() == [2] |
| 267 | + |
| 268 | + def test_fetch_after_fetch_returns_same_results(self): |
| 269 | + cursor, handlers, _ = _make_async_cursor() |
| 270 | + |
| 271 | + cursor.execute("SELECT 1") |
| 272 | + handlers[0](ExecutionResult(results=pandas.DataFrame({"x": [1]}))) |
| 273 | + |
| 274 | + assert cursor.fetchall()["x"].tolist() == [1] |
| 275 | + assert cursor.fetchall()["x"].tolist() == [1] |
| 276 | + |
| 277 | + |
| 278 | +class TestCursorCancellation: |
| 279 | + """Only genuinely in-flight executions may be cancelled.""" |
| 280 | + |
| 281 | + def test_execute_cancels_in_flight_previous_query(self): |
| 282 | + cursor, _, cancel_fn = _make_async_cursor() |
| 283 | + |
| 284 | + cursor.execute("SELECT 1") |
| 285 | + cursor.execute("SELECT 2") |
| 286 | + |
| 287 | + cancel_fn.assert_called_once_with("exec-1") |
| 288 | + |
| 289 | + def test_execute_does_not_cancel_completed_previous_query(self): |
| 290 | + cursor, handlers, cancel_fn = _make_async_cursor() |
| 291 | + |
| 292 | + cursor.execute("MERGE INTO t USING s ON t.id = s.id ...") |
| 293 | + handlers[0](ExecutionResult(results=pandas.DataFrame())) |
| 294 | + |
| 295 | + # The DML completed (result queued, not fetched); executing another |
| 296 | + # statement must not attempt to cancel it. |
| 297 | + cursor.execute("SELECT 1") |
| 298 | + |
| 299 | + cancel_fn.assert_not_called() |
| 300 | + |
| 301 | + def test_close_cancels_in_flight_query(self): |
| 302 | + cursor, _, cancel_fn = _make_async_cursor() |
| 303 | + |
| 304 | + cursor.execute("SELECT 1") |
| 305 | + cursor.close() |
| 306 | + |
| 307 | + cancel_fn.assert_called_once_with("exec-1") |
| 308 | + |
| 309 | + def test_close_does_not_cancel_completed_query(self): |
| 310 | + cursor, handlers, cancel_fn = _make_async_cursor() |
| 311 | + |
| 312 | + cursor.execute("SELECT 1") |
| 313 | + handlers[0](ExecutionResult(results=pandas.DataFrame({"x": [1]}))) |
| 314 | + cursor.close() |
| 315 | + |
| 316 | + cancel_fn.assert_not_called() |
| 317 | + |
| 318 | + def test_close_without_execute_does_not_cancel(self): |
| 319 | + cursor, _, cancel_fn = _make_async_cursor() |
| 320 | + |
| 321 | + cursor.close() |
| 322 | + |
| 323 | + cancel_fn.assert_not_called() |
| 324 | + |
| 325 | + |
214 | 326 | # --------------------------------------------------------------------------- |
215 | 327 | # _substitute_parameters unit tests |
216 | 328 | # --------------------------------------------------------------------------- |
|
0 commit comments