| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from datetime import date, datetime |
| 4 |
from typing import Awaitable, Callable |
| 5 |
from unittest.mock import MagicMock |
| 6 |
|
| 7 |
import pytest |
| 8 |
from textual.message import Message |
| 9 |
from textual.widgets import Tooltip |
| 10 |
from textual_fastdatatable import DataTable |
| 11 |
|
| 12 |
from harlequin import Harlequin |
| 13 |
from harlequin.adapter import HarlequinAdapter |
| 14 |
from harlequin.components.results_viewer import ResultsViewer |
| 15 |
from harlequin.components.text_modal import CellViewModal |
| 16 |
from tests.functional_tests.helpers import wait_for_any_table, wait_for_editor |
| 17 |
from tests.waiting import POLL_INTERVAL, wait_for, wait_for_value |
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.asyncio |
| 21 |
async def test_dupe_column_names( |
| 22 |
app_all_adapters: Harlequin, |
| 23 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 24 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 25 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 26 |
) -> None: |
| 27 |
app = app_all_adapters |
| 28 |
query = "select 1 as a, 1 as a, 2 as a, 2 as a" |
| 29 |
async with app.run_test() as pilot: |
| 30 |
await wait_for_workers(app) |
| 31 |
editor = await wait_for_editor(pilot, app) |
| 32 |
editor.text = query |
| 33 |
await pilot.press("ctrl+j") |
| 34 |
await wait_for_any_table(pilot, app) |
| 35 |
if not transaction_button_visible(app): |
| 36 |
assert await app_snapshot(app, "dupe columns") |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.asyncio |
| 40 |
async def test_copy_data( |
| 41 |
app_all_adapters: Harlequin, |
| 42 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 43 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 44 |
mock_pyperclip: MagicMock, |
| 45 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 46 |
) -> None: |
| 47 |
app = app_all_adapters |
| 48 |
query = "select 3, 'rosberg', 6, 'ROS', 'Nico', 'Rosberg', '1985-06-27', 'German', 'http://en.wikipedia.org/wiki/Nico_Rosberg'" |
| 49 |
expected = "3 rosberg 6 ROS Nico Rosberg 1985-06-27 German http://en.wikipedia.org/wiki/Nico_Rosberg" |
| 50 |
messages: list[Message] = [] |
| 51 |
async with app.run_test(message_hook=messages.append) as pilot: |
| 52 |
await wait_for_workers(app) |
| 53 |
editor = await wait_for_editor(pilot, app) |
| 54 |
editor.text = query |
| 55 |
await pilot.press("ctrl+j") |
| 56 |
await wait_for_any_table(pilot, app) |
| 57 |
# the table is pushed before the Results Viewer is shown and focused |
| 58 |
await wait_for( |
| 59 |
pilot, |
| 60 |
lambda: app.results_viewer._has_focus_within, |
| 61 |
description="the Results Viewer to take focus", |
| 62 |
) |
| 63 |
keys = ["shift+right"] * 8 |
| 64 |
await pilot.press(*keys) |
| 65 |
await pilot.wait_for_scheduled_animations() |
| 66 |
await pilot.press("ctrl+c") |
| 67 |
await pilot.pause() |
| 68 |
|
| 69 |
copied_message = list( |
| 70 |
filter(lambda m: isinstance(m, DataTable.SelectionCopied), messages) |
| 71 |
)[0] |
| 72 |
assert isinstance(copied_message, DataTable.SelectionCopied) |
| 73 |
assert isinstance(copied_message.values, list) |
| 74 |
|
| 75 |
editor.text = "" |
| 76 |
editor.focus() |
| 77 |
await pilot.press("ctrl+v") # paste |
| 78 |
assert editor.text == expected |
| 79 |
if not transaction_button_visible(app): |
| 80 |
assert await app_snapshot(app, "paste values from table") |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.asyncio |
| 84 |
async def test_view_cell_modal( |
| 85 |
app: Harlequin, |
| 86 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 87 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 88 |
) -> None: |
| 89 |
long_value = "the quick brown fox " * 40 |
| 90 |
query = f"select '{long_value}' as story" |
| 91 |
async with app.run_test() as pilot: |
| 92 |
await wait_for_workers(app) |
| 93 |
editor = await wait_for_editor(pilot, app) |
| 94 |
editor.text = query |
| 95 |
await pilot.press("ctrl+j") |
| 96 |
await wait_for_any_table(pilot, app) |
| 97 |
# the table is pushed before the Results Viewer is shown and focused |
| 98 |
await wait_for( |
| 99 |
pilot, |
| 100 |
lambda: app.results_viewer._has_focus_within, |
| 101 |
description="the Results Viewer to take focus", |
| 102 |
) |
| 103 |
await pilot.press("space") |
| 104 |
modal = await wait_for_value( |
| 105 |
pilot, |
| 106 |
lambda: app.screen if isinstance(app.screen, CellViewModal) else None, |
| 107 |
description="the cell view modal", |
| 108 |
) |
| 109 |
assert modal.text == long_value |
| 110 |
assert modal.title == "story" |
| 111 |
assert await app_snapshot(app, "view cell modal") |
| 112 |
|
| 113 |
# clicking the text copies it and leaves the modal up, as does c |
| 114 |
await pilot.click("#modal_info") |
| 115 |
await pilot.pause() |
| 116 |
assert isinstance(app.screen, CellViewModal) |
| 117 |
assert app.clipboard == long_value |
| 118 |
|
| 119 |
await pilot.press("c") |
| 120 |
await pilot.pause() |
| 121 |
assert isinstance(app.screen, CellViewModal) |
| 122 |
assert app.clipboard == long_value |
| 123 |
|
| 124 |
# scroll keys scroll instead of dismissing |
| 125 |
body = app.screen.body |
| 126 |
await pilot.press("pagedown") |
| 127 |
await pilot.wait_for_scheduled_animations() |
| 128 |
await pilot.pause() |
| 129 |
assert isinstance(app.screen, CellViewModal) |
| 130 |
assert body.scroll_offset.y > 0 |
| 131 |
|
| 132 |
# any other key dismisses it |
| 133 |
await pilot.press("x") |
| 134 |
await pilot.pause() |
| 135 |
assert not isinstance(app.screen, CellViewModal) |
| 136 |
|
| 137 |
# a click outside the modal also closes it, like the help/error modals |
| 138 |
await pilot.press("space") |
| 139 |
await pilot.pause() |
| 140 |
assert isinstance(app.screen, CellViewModal) |
| 141 |
await pilot.click() |
| 142 |
await pilot.pause() |
| 143 |
assert not isinstance(app.screen, CellViewModal) |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.asyncio |
| 147 |
async def test_data_truncated_with_tooltip( |
| 148 |
app_all_adapters: Harlequin, |
| 149 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 150 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 151 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 152 |
) -> None: |
| 153 |
app = app_all_adapters |
| 154 |
query = "select 'supercalifragilisticexpialidocious'" |
| 155 |
async with app.run_test(tooltips=True) as pilot: |
| 156 |
await wait_for_workers(app) |
| 157 |
editor = await wait_for_editor(pilot, app) |
| 158 |
editor.text = query |
| 159 |
await pilot.press("ctrl+j") |
| 160 |
await wait_for_any_table(pilot, app) |
| 161 |
|
| 162 |
await pilot.hover(ResultsViewer, (2, 2)) |
| 163 |
await wait_for( |
| 164 |
pilot, |
| 165 |
lambda: app.screen.get_child_by_type(Tooltip).display, |
| 166 |
description="the hover to outlast the tooltip delay", |
| 167 |
interval=POLL_INTERVAL, |
| 168 |
) |
| 169 |
if not transaction_button_visible(app): |
| 170 |
assert await app_snapshot(app, "hover over truncated value") |
| 171 |
|
| 172 |
|
| 173 |
@pytest.mark.asyncio |
| 174 |
async def test_infinity_timestamp( |
| 175 |
app: Harlequin, |
| 176 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 177 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 178 |
) -> None: |
| 179 |
query = """ |
| 180 |
select |
| 181 |
'infinity'::date, |
| 182 |
'infinity'::timestamp, |
| 183 |
'-infinity'::date, |
| 184 |
'-infinity'::timestamp |
| 185 |
""" |
| 186 |
async with app.run_test(size=(120, 36)) as pilot: |
| 187 |
await wait_for_workers(app) |
| 188 |
editor = await wait_for_editor(pilot, app) |
| 189 |
editor.text = query |
| 190 |
await pilot.press("ctrl+j") |
| 191 |
results_table = await wait_for_any_table(pilot, app) |
| 192 |
assert results_table.get_row_at(0) == [ |
| 193 |
date.max, |
| 194 |
datetime.max, |
| 195 |
date.min, |
| 196 |
datetime.min, |
| 197 |
] |
| 198 |
|
| 199 |
assert await app_snapshot(app, "hover over truncated value") |
| 200 |
|
| 201 |
|
| 202 |
@pytest.mark.asyncio |
| 203 |
async def test_the_viewer_cap_is_a_soft_one( |
| 204 |
duckdb_adapter: type[HarlequinAdapter], |
| 205 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 206 |
) -> None: |
| 207 |
"""Everything is fetched and the viewer holds the first N. |
| 208 |
|
| 209 |
Which is why it can report the exact total it is showing a part of -- |
| 210 |
unlike the Run Query Bar's limit, where the rest was never fetched. |
| 211 |
""" |
| 212 |
app = Harlequin( |
| 213 |
duckdb_adapter([":memory:"], no_init=True), |
| 214 |
connection_hash="capped", |
| 215 |
viewer_max_rows=10, |
| 216 |
) |
| 217 |
async with app.run_test() as pilot: |
| 218 |
await wait_for_workers(app) |
| 219 |
editor = await wait_for_editor(pilot, app) |
| 220 |
editor.text = "select * from range(100)" |
| 221 |
await pilot.press("ctrl+j") |
| 222 |
table = await wait_for_any_table(pilot, app) |
| 223 |
assert table.row_count == 10 |
| 224 |
assert table.fetched_row_count == 100 |
| 225 |
assert table.fetch_truncated is False |
| 226 |
assert app.results_viewer.border_title == ( |
| 227 |
"Query Results (Showing 10 of 100 Records)" |
| 228 |
) |