| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from typing import Awaitable, Callable |
| 5 |
|
| 6 |
import pytest |
| 7 |
from textual.message import Message |
| 8 |
|
| 9 |
from harlequin import Harlequin |
| 10 |
from harlequin.adapter import HarlequinAdapter |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.asyncio |
| 14 |
async def test_run_query_bar( |
| 15 |
app_all_adapters_small_db: Harlequin, |
| 16 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 17 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 18 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 19 |
) -> None: |
| 20 |
app = app_all_adapters_small_db |
| 21 |
snap_results: list[bool] = [] |
| 22 |
messages: list[Message] = [] |
| 23 |
async with app.run_test(size=(120, 36), message_hook=messages.append) as pilot: |
| 24 |
await wait_for_workers(app) |
| 25 |
while app.editor is None: |
| 26 |
await pilot.pause() |
| 27 |
# initialization |
| 28 |
bar = app.run_query_bar |
| 29 |
assert bar.limit_checkbox.value is False |
| 30 |
assert bar.limit_input.value == "500" |
| 31 |
assert bar.limit_value is None |
| 32 |
|
| 33 |
# query without any limit by clicking the button; |
| 34 |
# dataset has 857 records |
| 35 |
assert app.editor is not None |
| 36 |
app.editor.text = "select * from drivers" |
| 37 |
await pilot.click("#run_query") |
| 38 |
await wait_for_workers(app) |
| 39 |
await pilot.pause() |
| 40 |
await wait_for_workers(app) |
| 41 |
await pilot.pause() |
| 42 |
await wait_for_workers(app) |
| 43 |
await pilot.pause() |
| 44 |
await pilot.wait_for_scheduled_animations() |
| 45 |
table = app.results_viewer.get_visible_table() |
| 46 |
assert table |
| 47 |
assert table.row_count == table.source_row_count == 857 |
| 48 |
assert table.fetch_truncated is False |
| 49 |
# nothing was cut short, so the count is exact and says so |
| 50 |
assert app.results_viewer.border_title == "Query Results (857 Records)" |
| 51 |
snap_results.append(await app_snapshot(app, "No limit")) |
| 52 |
|
| 53 |
# apply a limit by clicking the limit checkbox |
| 54 |
await pilot.click(bar.limit_checkbox.__class__) |
| 55 |
assert bar.limit_checkbox.value is True |
| 56 |
assert bar.limit_value == 500 |
| 57 |
await pilot.click("#run_query") |
| 58 |
await wait_for_workers(app) |
| 59 |
await pilot.pause() |
| 60 |
await wait_for_workers(app) |
| 61 |
await pilot.pause() |
| 62 |
await wait_for_workers(app) |
| 63 |
await pilot.pause() |
| 64 |
await pilot.wait_for_scheduled_animations() |
| 65 |
table = app.results_viewer.get_visible_table() |
| 66 |
assert table |
| 67 |
# 501 fetched: one row more than the limit is what proves there are |
| 68 |
# more, and it is not kept. |
| 69 |
assert table.row_count == 500 |
| 70 |
assert table.source_row_count == 501 |
| 71 |
assert table.fetch_truncated is True |
| 72 |
assert app.results_viewer.border_title == ( |
| 73 |
"Query Results (Showing 500 of >500 Records)" |
| 74 |
) |
| 75 |
snap_results.append(await app_snapshot(app, "Limit 500")) |
| 76 |
|
| 77 |
# type an invalid limit, checkbox should be unchecked |
| 78 |
# and a tooltip should appear on hover |
| 79 |
await pilot.click(bar.limit_input.__class__) |
| 80 |
await pilot.press("a") |
| 81 |
assert bar.limit_input.value == "a500" |
| 82 |
assert bar.limit_value is None |
| 83 |
assert bar.limit_checkbox.value is False |
| 84 |
assert bar.limit_input.tooltip is not None |
| 85 |
await pilot.pause() |
| 86 |
await pilot.wait_for_scheduled_animations() |
| 87 |
snap_results.append(await app_snapshot(app, "Invalid limit")) |
| 88 |
|
| 89 |
# type a valid limit |
| 90 |
await pilot.press("backspace") |
| 91 |
await pilot.press("delete") |
| 92 |
await pilot.press("1") |
| 93 |
assert bar.limit_input.value == "100" |
| 94 |
assert bar.limit_value == 100 |
| 95 |
assert bar.limit_checkbox.value is True |
| 96 |
assert bar.limit_input.tooltip is None |
| 97 |
|
| 98 |
# run the query with a smaller limit |
| 99 |
await pilot.click("#run_query") |
| 100 |
await wait_for_workers(app) |
| 101 |
await pilot.pause() |
| 102 |
await wait_for_workers(app) |
| 103 |
await pilot.pause() |
| 104 |
await wait_for_workers(app) |
| 105 |
await pilot.pause() |
| 106 |
await pilot.wait_for_scheduled_animations() |
| 107 |
table = app.results_viewer.get_visible_table() |
| 108 |
assert table |
| 109 |
assert table.row_count == 100 |
| 110 |
assert table.source_row_count == 101 |
| 111 |
snap_results.append(await app_snapshot(app, "Limit 100")) |
| 112 |
|
| 113 |
if not transaction_button_visible(app): |
| 114 |
assert all(snap_results) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.py12 |
| 118 |
@pytest.mark.skipif( |
| 119 |
sys.version_info < (3, 12), reason="SQLite in Python < 3.12 won't show txn button" |
| 120 |
) |
| 121 |
@pytest.mark.asyncio |
| 122 |
async def test_transaction_button( |
| 123 |
app_small_sqlite: Harlequin, |
| 124 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 125 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 126 |
) -> None: |
| 127 |
app = app_small_sqlite |
| 128 |
snap_results: list[bool] = [] |
| 129 |
async with app.run_test(size=(120, 36)) as pilot: |
| 130 |
await wait_for_workers(app) |
| 131 |
while app.editor is None or app.connection is None: |
| 132 |
await pilot.pause() |
| 133 |
|
| 134 |
assert app.connection.transaction_mode |
| 135 |
assert app.connection.transaction_mode.label == "Auto" |
| 136 |
snap_results.append(await app_snapshot(app, "Initialize with Tx: Auto")) |
| 137 |
await pilot.click("#transaction_button") |
| 138 |
await pilot.pause(0.3) |
| 139 |
await pilot.wait_for_animation() |
| 140 |
assert app.connection.transaction_mode |
| 141 |
assert app.connection.transaction_mode.label == "Manual" |
| 142 |
assert app.connection.transaction_mode.commit is not None |
| 143 |
assert app.connection.transaction_mode.rollback is not None |
| 144 |
snap_results.append(await app_snapshot(app, "After click with Tx: Manual")) |
| 145 |
|
| 146 |
assert all(snap_results) |
| 147 |
|
| 148 |
|
| 149 |
@pytest.mark.asyncio |
| 150 |
async def test_a_configured_limit_is_in_force_from_the_first_query( |
| 151 |
duckdb_adapter: type[HarlequinAdapter], |
| 152 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 153 |
) -> None: |
| 154 |
"""`--limit` sets the input and checks the box, so it actually limits. |
| 155 |
|
| 156 |
Which is the whole of what makes it the same option as `hsql --limit`: the |
| 157 |
number in the bar is what `cursor.set_limit()` receives. |
| 158 |
""" |
| 159 |
app = Harlequin( |
| 160 |
duckdb_adapter([":memory:"], no_init=True), |
| 161 |
connection_hash="limited", |
| 162 |
query_limit=10, |
| 163 |
) |
| 164 |
async with app.run_test() as pilot: |
| 165 |
await wait_for_workers(app) |
| 166 |
while app.editor is None: |
| 167 |
await pilot.pause() |
| 168 |
bar = app.run_query_bar |
| 169 |
assert bar.limit_checkbox.value is True |
| 170 |
assert bar.limit_input.value == "10" |
| 171 |
assert bar.limit_value == 10 |
| 172 |
|
| 173 |
app.editor.text = "select * from range(100)" |
| 174 |
await pilot.press("ctrl+j") |
| 175 |
for _ in range(3): |
| 176 |
await wait_for_workers(app) |
| 177 |
await pilot.pause() |
| 178 |
|
| 179 |
table = app.results_viewer.get_visible_table() |
| 180 |
assert table is not None |
| 181 |
assert table.row_count == 10 |
| 182 |
assert table.fetch_truncated is True |
| 183 |
assert app.results_viewer.border_title == ( |
| 184 |
"Query Results (Showing 10 of >10 Records)" |
| 185 |
) |
| 186 |
|
| 187 |
|
| 188 |
@pytest.mark.asyncio |
| 189 |
async def test_no_configured_limit_leaves_the_box_unchecked( |
| 190 |
app_small_duck: Harlequin, |
| 191 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 192 |
) -> None: |
| 193 |
"""A human watching a viewport can afford a full fetch, so nothing configured |
| 194 |
means nothing limited -- with 500 in the input to start from.""" |
| 195 |
app = app_small_duck |
| 196 |
async with app.run_test() as pilot: |
| 197 |
await wait_for_workers(app) |
| 198 |
while app.editor is None: |
| 199 |
await pilot.pause() |
| 200 |
assert app.run_query_bar.limit_checkbox.value is False |
| 201 |
assert app.run_query_bar.limit_input.value == "500" |
| 202 |
assert app.run_query_bar.limit_value is None |