| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from types import SimpleNamespace |
| 4 |
from typing import Awaitable, Callable, cast |
| 5 |
|
| 6 |
import pytest |
| 7 |
from textual.message import Message |
| 8 |
from textual.worker import Worker, WorkerState |
| 9 |
|
| 10 |
from harlequin import Harlequin |
| 11 |
from harlequin.app import QueriesExecuted, QuerySubmitted, ResultsFetched |
| 12 |
from harlequin.components import ErrorModal |
| 13 |
from tests.functional_tests.helpers import ( |
| 14 |
wait_for_any_table, |
| 15 |
wait_for_editor, |
| 16 |
wait_for_error_modal, |
| 17 |
) |
| 18 |
from tests.waiting import wait_for, wait_for_messages |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.asyncio |
| 22 |
async def test_select_1( |
| 23 |
app_all_adapters: Harlequin, |
| 24 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 25 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 26 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 27 |
) -> None: |
| 28 |
app = app_all_adapters |
| 29 |
messages: list[Message] = [] |
| 30 |
async with app.run_test(message_hook=messages.append) as pilot: |
| 31 |
await wait_for_workers(app) |
| 32 |
await wait_for_editor(pilot, app) |
| 33 |
assert app.title == "Harlequin" |
| 34 |
assert app.focused.__class__.__name__ == "TextAreaPlus" |
| 35 |
|
| 36 |
q = "select 1 as foo" |
| 37 |
for key in q: |
| 38 |
await pilot.press(key) |
| 39 |
await pilot.press("ctrl+j") # alias for ctrl+enter |
| 40 |
|
| 41 |
[query_submitted_message] = await wait_for_messages( |
| 42 |
pilot, messages, QuerySubmitted, exactly=True |
| 43 |
) |
| 44 |
assert query_submitted_message.queries == [q] |
| 45 |
[query_executed_message] = await wait_for_messages( |
| 46 |
pilot, messages, QueriesExecuted, exactly=True |
| 47 |
) |
| 48 |
assert query_executed_message.query_count == 1 |
| 49 |
assert query_executed_message.cursors |
| 50 |
[results_fetched_message] = await wait_for_messages( |
| 51 |
pilot, messages, ResultsFetched, exactly=True |
| 52 |
) |
| 53 |
assert results_fetched_message.errors == [] |
| 54 |
table = await wait_for_any_table(pilot, app) |
| 55 |
assert table.source_row_count == table.row_count == 1 |
| 56 |
# sqlite on py3.12 will show the Tx: Auto button, and snap |
| 57 |
# will fail |
| 58 |
if not transaction_button_visible(app): |
| 59 |
assert await app_snapshot(app, "select 1 as foo") |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.asyncio |
| 63 |
@pytest.mark.parametrize( |
| 64 |
"query", |
| 65 |
[ |
| 66 |
"select 1+1", |
| 67 |
"select 'a' as foo", |
| 68 |
"select null", |
| 69 |
"select null as foo", |
| 70 |
"", |
| 71 |
"select 1 where false", |
| 72 |
"select 1 union all select 'hi'", |
| 73 |
"select 'hi' union all select 1", |
| 74 |
"""select '[/foo]' as "[/bar]" """.strip(), |
| 75 |
], |
| 76 |
) |
| 77 |
async def test_queries_do_not_crash_all_adapters( |
| 78 |
app_all_adapters: Harlequin, |
| 79 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 80 |
query: str, |
| 81 |
) -> None: |
| 82 |
app = app_all_adapters |
| 83 |
messages: list[Message] = [] |
| 84 |
async with app.run_test(message_hook=messages.append) as pilot: |
| 85 |
await wait_for_workers(app) |
| 86 |
editor = await wait_for_editor(pilot, app) |
| 87 |
editor.text = query |
| 88 |
await pilot.press("ctrl+j") |
| 89 |
|
| 90 |
if query: |
| 91 |
[query_submitted_message] = await wait_for_messages( |
| 92 |
pilot, messages, QuerySubmitted, exactly=True |
| 93 |
) |
| 94 |
assert query_submitted_message.queries == [query] |
| 95 |
[query_executed_message] = await wait_for_messages( |
| 96 |
pilot, messages, QueriesExecuted, exactly=True |
| 97 |
) |
| 98 |
assert query_executed_message.cursors |
| 99 |
# an empty result is still a table, with a header and no rows |
| 100 |
table = await wait_for_any_table(pilot, app) |
| 101 |
if query != "select 1 where false": |
| 102 |
assert table.row_count >= 1 |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.asyncio |
| 106 |
@pytest.mark.parametrize( |
| 107 |
"query", |
| 108 |
[ |
| 109 |
"SELECT {'x': 1, 'y': 2, 'z': 3}", # struct |
| 110 |
# also a struct: |
| 111 |
"SELECT {'yes': 'duck', 'maybe': 'goose', 'huh': NULL, 'no': 'heron'}", |
| 112 |
"SELECT {'key1': 'string', 'key2': 1, 'key3': 12.345}", # struct |
| 113 |
"""SELECT {'birds': |
| 114 |
{'yes': 'duck', 'maybe': 'goose', 'huh': NULL, 'no': 'heron'}, |
| 115 |
'aliens': |
| 116 |
NULL} as bar""", # struct |
| 117 |
"select {'a': 5} union all select {'a': 6}", # struct |
| 118 |
"select map {'a': 5}", # map |
| 119 |
"select map {'a': 5} union all select map {'b': 6}", # map |
| 120 |
"SELECT map { 1: 42.001, 5: -32.1 }", # map |
| 121 |
"SELECT map { ['a', 'b']: [1.1, 2.2], ['c', 'd']: [3.3, 4.4] }", # map |
| 122 |
"SELECT [1, 2, 3]", # list |
| 123 |
"SELECT ['duck', 'goose', NULL, 'heron'];", # list |
| 124 |
"SELECT [['duck', 'goose', 'heron'], NULL, ['frog', 'toad'], []];", # list |
| 125 |
"set timezone = 'America/New_York'; select '2024-01-01'::timestamptz;", |
| 126 |
"set timezone = 'America/New_York'; select '1-1-1T00:00:00Z'::timestamptz", |
| 127 |
], |
| 128 |
) |
| 129 |
async def test_queries_do_not_crash( |
| 130 |
app: Harlequin, |
| 131 |
query: str, |
| 132 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 133 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 134 |
) -> None: |
| 135 |
async with app.run_test() as pilot: |
| 136 |
await wait_for_workers(app) |
| 137 |
editor = await wait_for_editor(pilot, app) |
| 138 |
editor.text = query |
| 139 |
await pilot.press("ctrl+a") |
| 140 |
await pilot.press("ctrl+j") |
| 141 |
table = await wait_for_any_table(pilot, app) |
| 142 |
assert table.row_count >= 1 |
| 143 |
|
| 144 |
|
| 145 |
@pytest.mark.asyncio |
| 146 |
async def test_multiple_queries( |
| 147 |
app_all_adapters: Harlequin, |
| 148 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 149 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 150 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 151 |
) -> None: |
| 152 |
app = app_all_adapters |
| 153 |
snap_results: list[bool] = [] |
| 154 |
messages: list[Message] = [] |
| 155 |
async with app.run_test(message_hook=messages.append) as pilot: |
| 156 |
await wait_for_workers(app) |
| 157 |
editor = await wait_for_editor(pilot, app) |
| 158 |
q = "select 1; select 2" |
| 159 |
editor.text = q |
| 160 |
await pilot.press("ctrl+j") |
| 161 |
|
| 162 |
# should only run one query |
| 163 |
[query_submitted_message] = await wait_for_messages( |
| 164 |
pilot, messages, QuerySubmitted, exactly=True |
| 165 |
) |
| 166 |
assert query_submitted_message.queries == ["select 1;"] |
| 167 |
table = await wait_for_any_table(pilot, app) |
| 168 |
assert table.row_count == table.source_row_count == 1 |
| 169 |
assert "hide-tabs" in app.results_viewer.classes |
| 170 |
await pilot.wait_for_scheduled_animations() |
| 171 |
snap_results.append(await app_snapshot(app, "One query")) |
| 172 |
|
| 173 |
editor.focus() |
| 174 |
await pilot.press("ctrl+a") |
| 175 |
await pilot.press("ctrl+j") |
| 176 |
# should run both queries |
| 177 |
[_, query_submitted_message] = await wait_for_messages( |
| 178 |
pilot, messages, QuerySubmitted, count=2, exactly=True |
| 179 |
) |
| 180 |
assert query_submitted_message.queries == ["select 1;", "select 2"] |
| 181 |
await wait_for( |
| 182 |
pilot, |
| 183 |
lambda: app.results_viewer.tab_count == 2, |
| 184 |
description="the Results Viewer to show a tab per query", |
| 185 |
) |
| 186 |
assert "hide-tabs" not in app.results_viewer.classes |
| 187 |
await wait_for_messages(pilot, messages, ResultsFetched, count=2) |
| 188 |
await pilot.wait_for_scheduled_animations() |
| 189 |
snap_results.append(await app_snapshot(app, "Both queries")) |
| 190 |
assert app.results_viewer.active == "result-1" |
| 191 |
await pilot.press("k") |
| 192 |
await pilot.wait_for_scheduled_animations() |
| 193 |
assert app.results_viewer.active == "result-2" |
| 194 |
snap_results.append(await app_snapshot(app, "Both queries, tab 2")) |
| 195 |
await pilot.press("k") |
| 196 |
await pilot.wait_for_scheduled_animations() |
| 197 |
assert app.results_viewer.active == "result-1" |
| 198 |
snap_results.append(await app_snapshot(app, "Both queries, tab 1")) |
| 199 |
await pilot.press("j") |
| 200 |
assert app.results_viewer.active == "result-2" |
| 201 |
await pilot.press("j") |
| 202 |
assert app.results_viewer.active == "result-1" |
| 203 |
|
| 204 |
if not transaction_button_visible(app): |
| 205 |
assert all(snap_results) |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.asyncio |
| 209 |
async def test_single_query_terminated_with_semicolon( |
| 210 |
app_all_adapters: Harlequin, |
| 211 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 212 |
) -> None: |
| 213 |
app = app_all_adapters |
| 214 |
messages: list[Message] = [] |
| 215 |
async with app.run_test(message_hook=messages.append) as pilot: |
| 216 |
await wait_for_workers(app) |
| 217 |
editor = await wait_for_editor(pilot, app) |
| 218 |
q = "select 1; \n\t\n" |
| 219 |
editor.text = q |
| 220 |
await pilot.press("ctrl+j") |
| 221 |
|
| 222 |
# should only run current query |
| 223 |
[query_submitted_message] = await wait_for_messages( |
| 224 |
pilot, messages, QuerySubmitted, exactly=True |
| 225 |
) |
| 226 |
assert query_submitted_message.queries == ["select 1;"] |
| 227 |
await wait_for_any_table(pilot, app) |
| 228 |
assert app.results_viewer.tab_count == 1 |
| 229 |
|
| 230 |
editor.focus() |
| 231 |
await pilot.press("ctrl+a") |
| 232 |
await pilot.press("ctrl+j") |
| 233 |
|
| 234 |
# should not run whitespace query, even though included |
| 235 |
# in selection. |
| 236 |
[_, query_submitted_message] = await wait_for_messages( |
| 237 |
pilot, messages, QuerySubmitted, count=2, exactly=True |
| 238 |
) |
| 239 |
assert query_submitted_message.queries == ["select 1;"] |
| 240 |
await wait_for_workers(app) |
| 241 |
assert app.results_viewer.tab_count == 1 |
| 242 |
|
| 243 |
editor.focus() |
| 244 |
await pilot.press("ctrl+end") |
| 245 |
await pilot.press("ctrl+j") |
| 246 |
# should run previous query |
| 247 |
[*_, query_submitted_message] = await wait_for_messages( |
| 248 |
pilot, messages, QuerySubmitted, count=3, exactly=True |
| 249 |
) |
| 250 |
assert query_submitted_message.queries == ["select 1;"] |
| 251 |
await wait_for_workers(app) |
| 252 |
assert app.results_viewer.tab_count == 1 |
| 253 |
|
| 254 |
|
| 255 |
@pytest.mark.asyncio |
| 256 |
@pytest.mark.parametrize( |
| 257 |
"bad_query", |
| 258 |
[ |
| 259 |
"select", # errors when building cursor |
| 260 |
"select 0::struct(id int)", # errors when fetching data |
| 261 |
"select; select 0::struct(id int)", # multiple errors |
| 262 |
"select 1; select 0::struct(id int)", # one error, mult queries |
| 263 |
"select 0::struct(id int); select 1", # one error, mult queries, err first |
| 264 |
], |
| 265 |
) |
| 266 |
async def test_query_errors( |
| 267 |
app_all_adapters: Harlequin, |
| 268 |
bad_query: str, |
| 269 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 270 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 271 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 272 |
) -> None: |
| 273 |
app = app_all_adapters |
| 274 |
snap_results: list[bool] = [] |
| 275 |
async with app.run_test(size=(120, 36)) as pilot: |
| 276 |
await wait_for_workers(app) |
| 277 |
editor = await wait_for_editor(pilot, app) |
| 278 |
editor.text = bad_query |
| 279 |
|
| 280 |
await pilot.press("ctrl+a") |
| 281 |
await pilot.press("ctrl+j") |
| 282 |
await wait_for_error_modal(pilot, app) |
| 283 |
assert len(app.screen_stack) == 2 |
| 284 |
snap_results.append(await app_snapshot(app, "Error visible")) |
| 285 |
|
| 286 |
await pilot.press("space") |
| 287 |
assert len(app.screen_stack) == 1 |
| 288 |
|
| 289 |
# data table and query bar should be responsive |
| 290 |
assert "non-responsive" not in app.run_query_bar.classes |
| 291 |
assert "non-responsive" not in app.results_viewer.classes |
| 292 |
snap_results.append(await app_snapshot(app, "After dismissing error")) |
| 293 |
|
| 294 |
if not transaction_button_visible(app): |
| 295 |
assert all(snap_results) |
| 296 |
|
| 297 |
|
| 298 |
@pytest.mark.asyncio |
| 299 |
async def test_rich_markup( |
| 300 |
app: Harlequin, |
| 301 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 302 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 303 |
) -> None: |
| 304 |
async with app.run_test() as pilot: |
| 305 |
await wait_for_workers(app) |
| 306 |
editor = await wait_for_editor(pilot, app) |
| 307 |
|
| 308 |
q = "select '[some text]', '[red]some text[/]'" |
| 309 |
editor.text = q |
| 310 |
await pilot.press("ctrl+j") # alias for ctrl+enter |
| 311 |
|
| 312 |
await wait_for_any_table(pilot, app) |
| 313 |
assert await app_snapshot(app, "select markup") |
| 314 |
|
| 315 |
|
| 316 |
@pytest.mark.asyncio |
| 317 |
async def test_adapter_raises_unexpected_error( |
| 318 |
app: Harlequin, |
| 319 |
monkeypatch: pytest.MonkeyPatch, |
| 320 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 321 |
) -> None: |
| 322 |
""" |
| 323 |
An adapter that raises a raw driver exception instead of a |
| 324 |
HarlequinQueryError should not crash the app. |
| 325 |
""" |
| 326 |
async with app.run_test() as pilot: |
| 327 |
await wait_for_workers(app) |
| 328 |
editor = await wait_for_editor(pilot, app) |
| 329 |
assert app.connection is not None |
| 330 |
|
| 331 |
def raise_driver_error(query: str) -> None: |
| 332 |
raise RuntimeError("1049 (42000): Unknown database 'ht;'") |
| 333 |
|
| 334 |
monkeypatch.setattr(app.connection, "execute", raise_driver_error) |
| 335 |
|
| 336 |
editor.text = "use ht;" |
| 337 |
await pilot.press("ctrl+a") |
| 338 |
await pilot.press("ctrl+j") |
| 339 |
modal = await wait_for_error_modal(pilot, app) |
| 340 |
|
| 341 |
assert app.is_running |
| 342 |
assert "Unknown database" in str(modal.error) |
| 343 |
|
| 344 |
await pilot.press("space") |
| 345 |
assert len(app.screen_stack) == 1 |
| 346 |
assert "non-responsive" not in app.run_query_bar.classes |
| 347 |
assert "non-responsive" not in app.results_viewer.classes |
| 348 |
|
| 349 |
|
| 350 |
def worker_error_message( |
| 351 |
worker_name: str, error: BaseException | None |
| 352 |
) -> Worker.StateChanged: |
| 353 |
"""A StateChanged ERROR the handler sees from a real failed worker. |
| 354 |
|
| 355 |
The message only carries the worker object; the handler reads its name and |
| 356 |
error, so a namespace stands in for the Worker. |
| 357 |
""" |
| 358 |
return Worker.StateChanged( |
| 359 |
cast( |
| 360 |
"Worker", |
| 361 |
SimpleNamespace(name=worker_name, error=error), |
| 362 |
), |
| 363 |
WorkerState.ERROR, |
| 364 |
) |
| 365 |
|
| 366 |
|
| 367 |
def _modal_errors_on_screen(app: Harlequin) -> list[ErrorModal]: |
| 368 |
return [screen for screen in app.screen_stack if isinstance(screen, ErrorModal)] |
| 369 |
|
| 370 |
|
| 371 |
@pytest.mark.asyncio |
| 372 |
async def test_update_schema_data_worker_error_shows_catalog_modal( |
| 373 |
app: Harlequin, |
| 374 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 375 |
) -> None: |
| 376 |
async with app.run_test() as pilot: |
| 377 |
await wait_for_workers(app) |
| 378 |
await wait_for_editor(pilot, app) |
| 379 |
|
| 380 |
app.data_catalog.database_tree.loading = True |
| 381 |
await app.handle_worker_error( |
| 382 |
worker_error_message("update_schema_data", RuntimeError("boom")) |
| 383 |
) |
| 384 |
await pilot.pause() |
| 385 |
assert isinstance(app.screen, ErrorModal) |
| 386 |
assert app.screen.header == "Could not update data catalog" |
| 387 |
assert "boom" in str(app.screen.error) |
| 388 |
assert app.data_catalog.database_tree.loading is False |
| 389 |
|
| 390 |
|
| 391 |
@pytest.mark.asyncio |
| 392 |
async def test_connect_worker_error_exits_with_code_two( |
| 393 |
app: Harlequin, |
| 394 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 395 |
) -> None: |
| 396 |
async with app.run_test() as pilot: |
| 397 |
await wait_for_workers(app) |
| 398 |
await wait_for_editor(pilot, app) |
| 399 |
|
| 400 |
await app.handle_worker_error( |
| 401 |
worker_error_message("_connect", RuntimeError("connection refused")) |
| 402 |
) |
| 403 |
await pilot.pause() |
| 404 |
assert app.return_code == 2 |
| 405 |
|
| 406 |
|
| 407 |
@pytest.mark.asyncio |
| 408 |
async def test_worker_error_from_unrecognized_worker_shows_modal( |
| 409 |
app: Harlequin, |
| 410 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 411 |
) -> None: |
| 412 |
"""A worker the handler doesn't name is loud by default, not silent. |
| 413 |
|
| 414 |
Regression test for #1117: errors from every unrecognized worker used to |
| 415 |
vanish without a trace. |
| 416 |
""" |
| 417 |
async with app.run_test() as pilot: |
| 418 |
await wait_for_workers(app) |
| 419 |
await wait_for_editor(pilot, app) |
| 420 |
|
| 421 |
await app.handle_worker_error( |
| 422 |
worker_error_message("some_future_worker", RuntimeError("boom")) |
| 423 |
) |
| 424 |
await pilot.pause() |
| 425 |
assert app.is_running |
| 426 |
assert isinstance(app.screen, ErrorModal) |
| 427 |
assert "boom" in str(app.screen.error) |
| 428 |
|
| 429 |
|
| 430 |
@pytest.mark.asyncio |
| 431 |
@pytest.mark.parametrize("worker_name", ["_execute_query", "_fetch_data"]) |
| 432 |
async def test_query_worker_error_shows_modal_and_restores_ui( |
| 433 |
app: Harlequin, |
| 434 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 435 |
worker_name: str, |
| 436 |
) -> None: |
| 437 |
"""A query worker that dies without posting its result message must not |
| 438 |
leave the run bar stuck in the non-responsive state.""" |
| 439 |
async with app.run_test() as pilot: |
| 440 |
await wait_for_workers(app) |
| 441 |
await wait_for_editor(pilot, app) |
| 442 |
|
| 443 |
app.run_query_bar.set_not_responsive() |
| 444 |
app.results_viewer.show_loading() |
| 445 |
await app.handle_worker_error( |
| 446 |
worker_error_message(worker_name, RuntimeError("boom")) |
| 447 |
) |
| 448 |
await pilot.pause() |
| 449 |
assert app.is_running |
| 450 |
assert isinstance(app.screen, ErrorModal) |
| 451 |
assert "boom" in str(app.screen.error) |
| 452 |
assert "non-responsive" not in app.run_query_bar.classes |
| 453 |
assert "non-responsive" not in app.results_viewer.classes |
| 454 |
|
| 455 |
|
| 456 |
@pytest.mark.asyncio |
| 457 |
async def test_execute_query_worker_error_is_not_silent_and_app_survives( |
| 458 |
app: Harlequin, |
| 459 |
monkeypatch: pytest.MonkeyPatch, |
| 460 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 461 |
) -> None: |
| 462 |
"""An error in the code around _execute_query's per-statement handling |
| 463 |
shows a modal and restores the UI, instead of silently sticking it. |
| 464 |
|
| 465 |
Regression test for #1117, against the real worker machinery. |
| 466 |
""" |
| 467 |
async with app.run_test() as pilot: |
| 468 |
await wait_for_workers(app) |
| 469 |
editor = await wait_for_editor(pilot, app) |
| 470 |
assert app.connection is not None |
| 471 |
|
| 472 |
def broken_execute( |
| 473 |
connection: object, |
| 474 |
statements: object, |
| 475 |
limit: object = None, |
| 476 |
on_error: object = "stop", |
| 477 |
) -> object: |
| 478 |
raise RuntimeError("boom inside execute core") |
| 479 |
|
| 480 |
monkeypatch.setattr("harlequin.app.execute", broken_execute) |
| 481 |
|
| 482 |
editor.text = "select 1" |
| 483 |
await pilot.press("ctrl+a") |
| 484 |
await pilot.press("ctrl+j") |
| 485 |
modal = await wait_for_error_modal(pilot, app) |
| 486 |
assert app.is_running |
| 487 |
assert "boom inside execute core" in str(modal.error) |
| 488 |
assert "non-responsive" not in app.run_query_bar.classes |
| 489 |
|
| 490 |
await pilot.press("space") |
| 491 |
assert len(app.screen_stack) == 1 |
| 492 |
|
| 493 |
|
| 494 |
@pytest.mark.asyncio |
| 495 |
async def test_toggle_transaction_mode_worker_error_shows_modal( |
| 496 |
app: Harlequin, |
| 497 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 498 |
) -> None: |
| 499 |
async with app.run_test() as pilot: |
| 500 |
await wait_for_workers(app) |
| 501 |
await wait_for_editor(pilot, app) |
| 502 |
|
| 503 |
await app.handle_worker_error( |
| 504 |
worker_error_message("toggle_transaction_mode", RuntimeError("boom")) |
| 505 |
) |
| 506 |
await pilot.pause() |
| 507 |
assert app.is_running |
| 508 |
assert isinstance(app.screen, ErrorModal) |
| 509 |
assert app.screen.title == "Transaction Error" |
| 510 |
assert "boom" in str(app.screen.error) |
| 511 |
|
| 512 |
|
| 513 |
@pytest.mark.asyncio |
| 514 |
@pytest.mark.parametrize( |
| 515 |
("worker_name", "expected_message"), |
| 516 |
[ |
| 517 |
("_load_catalog_cache", "Harlequin could not load its cache."), |
| 518 |
("_load_query_history", "Harlequin could not read your query history."), |
| 519 |
("_extend_and_merge_completers", "Harlequin could not update completions."), |
| 520 |
("_build_completers", "Harlequin could not build completions."), |
| 521 |
], |
| 522 |
) |
| 523 |
async def test_partial_failure_workers_notify_without_modal( |
| 524 |
app: Harlequin, |
| 525 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 526 |
worker_name: str, |
| 527 |
expected_message: str, |
| 528 |
) -> None: |
| 529 |
"""A worker whose failure leaves the app usable warns instead of modaling.""" |
| 530 |
async with app.run_test() as pilot: |
| 531 |
await wait_for_workers(app) |
| 532 |
await wait_for_editor(pilot, app) |
| 533 |
|
| 534 |
await app.handle_worker_error( |
| 535 |
worker_error_message(worker_name, RuntimeError("boom")) |
| 536 |
) |
| 537 |
await pilot.pause() |
| 538 |
assert app.is_running |
| 539 |
assert not _modal_errors_on_screen(app) |
| 540 |
warnings = [ |
| 541 |
notification |
| 542 |
for notification in list(app._notifications) |
| 543 |
if notification.severity == "warning" |
| 544 |
] |
| 545 |
assert [n.message for n in warnings] == [expected_message] |
| 546 |
|
| 547 |
|
| 548 |
@pytest.mark.asyncio |
| 549 |
async def test_worker_errors_are_ignored_while_app_is_exiting( |
| 550 |
app: Harlequin, |
| 551 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 552 |
) -> None: |
| 553 |
"""An error that lands during the exit drain is noise, not a modal.""" |
| 554 |
async with app.run_test() as pilot: |
| 555 |
await wait_for_workers(app) |
| 556 |
await wait_for_editor(pilot, app) |
| 557 |
|
| 558 |
app._exit = True |
| 559 |
try: |
| 560 |
await app.handle_worker_error( |
| 561 |
worker_error_message("some_future_worker", RuntimeError("boom")) |
| 562 |
) |
| 563 |
finally: |
| 564 |
app._exit = False |
| 565 |
await pilot.pause() |
| 566 |
assert len(app.screen_stack) == 1 |
| 567 |
assert app.is_running |
| 568 |
|
| 569 |
|
| 570 |
@pytest.mark.asyncio |
| 571 |
async def test_worker_state_change_without_error_is_ignored( |
| 572 |
app: Harlequin, |
| 573 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 574 |
) -> None: |
| 575 |
async with app.run_test() as pilot: |
| 576 |
await wait_for_workers(app) |
| 577 |
await wait_for_editor(pilot, app) |
| 578 |
|
| 579 |
await app.handle_worker_error(worker_error_message("some_future_worker", None)) |
| 580 |
await pilot.pause() |
| 581 |
assert len(app.screen_stack) == 1 |
| 582 |
assert app.is_running |