Debian Codegraph @

tests/functional_tests/test_run_query_bar.py

raw
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 from tests.functional_tests.helpers import wait_for_any_table, wait_for_editor
12 from tests.waiting import wait_for, wait_for_value
13
14
15 @pytest.mark.asyncio
16 async def test_run_query_bar(
17 app_all_adapters_small_db: Harlequin,
18 app_snapshot: Callable[..., Awaitable[bool]],
19 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
20 transaction_button_visible: Callable[[Harlequin], bool],
21 ) -> None:
22 app = app_all_adapters_small_db
23 snap_results: list[bool] = []
24 messages: list[Message] = []
25 async with app.run_test(size=(120, 36), message_hook=messages.append) as pilot:
26 await wait_for_workers(app)
27 editor = await wait_for_editor(pilot, app)
28 # initialization
29 bar = app.run_query_bar
30 assert bar.limit_checkbox.value is False
31 assert bar.limit_input.value == "500"
32 assert bar.limit_value is None
33
34 # query without any limit by clicking the button;
35 # dataset has 857 records
36 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 await wait_for_editor(pilot, app)
132 connection = await wait_for_value(
133 pilot, lambda: app.connection, description="the app to connect"
134 )
135
136 assert connection.transaction_mode
137 assert connection.transaction_mode.label == "Auto"
138 snap_results.append(await app_snapshot(app, "Initialize with Tx: Auto"))
139 await pilot.click("#transaction_button")
140 await wait_for(
141 pilot,
142 lambda: (
143 connection.transaction_mode is not None
144 and connection.transaction_mode.label == "Manual"
145 ),
146 description="the transaction mode to switch to Manual",
147 )
148 await pilot.wait_for_animation()
149 assert connection.transaction_mode
150 assert connection.transaction_mode.commit is not None
151 assert connection.transaction_mode.rollback is not None
152 snap_results.append(await app_snapshot(app, "After click with Tx: Manual"))
153
154 assert all(snap_results)
155
156
157 @pytest.mark.asyncio
158 async def test_a_configured_limit_is_in_force_from_the_first_query(
159 duckdb_adapter: type[HarlequinAdapter],
160 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
161 ) -> None:
162 """`--limit` sets the input and checks the box, so it actually limits.
163
164 Which is the whole of what makes it the same option as `hsql --limit`: the
165 number in the bar is what `cursor.set_limit()` receives.
166 """
167 app = Harlequin(
168 duckdb_adapter([":memory:"], no_init=True),
169 connection_hash="limited",
170 query_limit=10,
171 )
172 async with app.run_test() as pilot:
173 await wait_for_workers(app)
174 editor = await wait_for_editor(pilot, app)
175 bar = app.run_query_bar
176 assert bar.limit_checkbox.value is True
177 assert bar.limit_input.value == "10"
178 assert bar.limit_value == 10
179
180 editor.text = "select * from range(100)"
181 await pilot.press("ctrl+j")
182 table = await wait_for_any_table(pilot, app)
183 assert table.row_count == 10
184 assert table.fetch_truncated is True
185 assert app.results_viewer.border_title == (
186 "Query Results (Showing 10 of >10 Records)"
187 )
188
189
190 @pytest.mark.asyncio
191 async def test_no_configured_limit_leaves_the_box_unchecked(
192 app_small_duck: Harlequin,
193 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
194 ) -> None:
195 """A human watching a viewport can afford a full fetch, so nothing configured
196 means nothing limited -- with 500 in the input to start from."""
197 app = app_small_duck
198 async with app.run_test() as pilot:
199 await wait_for_workers(app)
200 await wait_for_editor(pilot, app)
201 assert app.run_query_bar.limit_checkbox.value is False
202 assert app.run_query_bar.limit_input.value == "500"
203 assert app.run_query_bar.limit_value is None