Debian Codegraph @

tests/functional_tests/test_export.py

raw
1 import os
2 import sys
3 from pathlib import Path
4 from typing import Awaitable, Callable, List
5
6 import pytest
7 from textual.pilot import Pilot
8
9 from harlequin import Harlequin
10 from harlequin.adapter import HarlequinAdapter
11 from harlequin.components import ExportScreen
12 from tests.functional_tests.helpers import wait_for_any_table, wait_for_editor
13 from tests.waiting import wait_for, wait_for_value
14
15
16 async def open_export_screen(pilot: Pilot, app: Harlequin) -> ExportScreen:
17 """Press the key that opens the Data Exporter, and return the screen."""
18 await pilot.press("ctrl+e")
19 return await wait_for_value(
20 pilot,
21 lambda: app.screen if isinstance(app.screen, ExportScreen) else None,
22 description="the Export screen",
23 )
24
25
26 @pytest.mark.asyncio
27 @pytest.mark.parametrize(
28 "filename",
29 [
30 "one.csv",
31 "one.parquet",
32 "one.json",
33 pytest.param(
34 "one.orc",
35 marks=pytest.mark.skipif(
36 sys.platform == "win32", reason="ORC not supported on Windows"
37 ),
38 ),
39 "one.feather",
40 ],
41 )
42 async def test_export(
43 app_all_adapters: Harlequin,
44 tmp_path: Path,
45 filename: str,
46 app_snapshot: Callable[..., Awaitable[bool]],
47 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
48 transaction_button_visible: Callable[[Harlequin], bool],
49 ) -> None:
50 app = app_all_adapters
51 snap_results: List[bool] = []
52 async with app.run_test(size=(120, 36)) as pilot:
53 await wait_for_workers(app)
54 editor = await wait_for_editor(pilot, app)
55 editor.text = "select 1 as a, 2 as b"
56 await pilot.press("ctrl+j") # run query
57 await wait_for_any_table(pilot, app)
58 assert len(app.screen_stack) == 1
59
60 export_screen = await open_export_screen(pilot, app)
61 assert len(app.screen_stack) == 2
62 assert export_screen.id == "export_screen"
63 snap_results.append(await app_snapshot(app, "Export Screen"))
64
65 export_screen.file_input.value = f"/tmp/foo-bar-static/{filename}"
66 await pilot.pause()
67 snap_results.append(await app_snapshot(app, "Export with Path"))
68 export_path = tmp_path / filename
69 export_screen.file_input.value = str(export_path)
70 await pilot.pause()
71 await pilot.press("enter")
72 # the export runs on a worker, and the screen it was on comes back
73 await wait_for(
74 pilot,
75 lambda: len(app.screen_stack) == 1 and export_path.is_file(),
76 description="the export to be written and the Export screen to close",
77 )
78 if export_path.suffix == ".json":
79 with export_path.open("r") as f:
80 line = f.readline()
81 assert line == '{"a":1,"b":2}\n'
82
83 snap_results.append(await app_snapshot(app, "After Export"))
84
85 if not transaction_button_visible(app):
86 assert all(snap_results)
87
88
89 @pytest.mark.asyncio
90 async def test_export_result_with_no_rows(
91 app_all_adapters: Harlequin,
92 tmp_path: Path,
93 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
94 ) -> None:
95 """A query that matched nothing exports a header and no rows.
96
97 Not an error: an empty file is a true account of what the query returned,
98 and it is what tells a reader "nothing matched" apart from "it failed".
99 SQLite is the case that matters -- its cursor returns no backend at all
100 for zero rows, so the columns come from the labels on screen.
101 """
102 app = app_all_adapters
103 async with app.run_test(size=(120, 36)) as pilot:
104 await wait_for_workers(app)
105 editor = await wait_for_editor(pilot, app)
106 editor.text = "select 1 as a, 2 as b where false"
107 await pilot.press("ctrl+j")
108 await wait_for_any_table(pilot, app)
109
110 export_screen = await open_export_screen(pilot, app)
111
112 export_path = tmp_path / "empty.csv"
113 export_screen.file_input.value = str(export_path)
114 await pilot.pause()
115 await pilot.press("enter")
116 await wait_for(
117 pilot, export_path.is_file, description="the export to be written"
118 )
119
120 assert export_path.read_text() == "a,b\n"
121 # back on the main screen, i.e. no error modal
122 assert len(app.screen_stack) == 1
123
124
125 @pytest.mark.asyncio
126 async def test_export_under_a_limit_stops_at_the_limit(
127 app: Harlequin,
128 tmp_path: Path,
129 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
130 ) -> None:
131 """The fetch asks for one row more than the limit, to learn there are more.
132
133 That row proves the truncation and is not part of what was asked for, so a
134 limit of 5 exports five rows.
135 """
136 async with app.run_test(size=(120, 36)) as pilot:
137 await wait_for_workers(app)
138 editor = await wait_for_editor(pilot, app)
139 app.run_query_bar.limit_input.value = "5"
140 editor.text = "select * from range(100)"
141 await pilot.press("ctrl+j")
142 table = await wait_for_any_table(pilot, app)
143 assert table.fetch_truncated is True
144
145 export_screen = await open_export_screen(pilot, app)
146 export_path = tmp_path / "limited.csv"
147 export_screen.file_input.value = str(export_path)
148 await pilot.pause()
149 await pilot.press("enter")
150 await wait_for(
151 pilot, export_path.is_file, description="the export to be written"
152 )
153
154 assert len(export_path.read_text().splitlines()) == 6 # header and 5 rows
155
156
157 @pytest.mark.asyncio
158 async def test_export_starts_at_the_export_path(
159 duckdb_adapter: type[HarlequinAdapter],
160 tmp_path: Path,
161 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
162 ) -> None:
163 """`-o` names the folder the Data Exporter opens in, so a user who exports
164 into the same place every time types a file name and nothing else."""
165 app = Harlequin(
166 duckdb_adapter([":memory:"], no_init=True),
167 connection_hash="foo",
168 export_path=str(tmp_path / "exports"),
169 )
170 async with app.run_test(size=(120, 36)) as pilot:
171 await wait_for_workers(app)
172 editor = await wait_for_editor(pilot, app)
173 editor.text = "select 1 as a"
174 await pilot.press("ctrl+j")
175 await wait_for_any_table(pilot, app)
176
177 export_screen = await open_export_screen(pilot, app)
178 assert export_screen.file_input.value == f"{tmp_path / 'exports'}{os.sep}"
179
180 # the folder does not exist yet, and exporting into it makes it
181 export_screen.file_input.value += "one.csv"
182 await pilot.pause()
183 await pilot.press("enter")
184 exported = tmp_path / "exports" / "one.csv"
185 await wait_for(pilot, exported.is_file, description="the export to be written")
186
187 assert exported.read_text() == "a\n1\n"
188 assert len(app.screen_stack) == 1
189
190
191 @pytest.mark.asyncio
192 async def test_an_export_path_with_a_file_name_picks_its_format(
193 duckdb_adapter: type[HarlequinAdapter],
194 tmp_path: Path,
195 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
196 ) -> None:
197 """A whole path prefills whole, and the extension chooses the format the
198 same way a typed one does."""
199 export_path = tmp_path / "out.parquet"
200 app = Harlequin(
201 duckdb_adapter([":memory:"], no_init=True),
202 connection_hash="foo",
203 export_path=str(export_path),
204 )
205 async with app.run_test(size=(120, 36)) as pilot:
206 await wait_for_workers(app)
207 editor = await wait_for_editor(pilot, app)
208 editor.text = "select 1 as a"
209 await pilot.press("ctrl+j")
210 await wait_for_any_table(pilot, app)
211
212 export_screen = await open_export_screen(pilot, app)
213 assert export_screen.file_input.value == str(export_path)
214 assert export_screen.format_select.value == "parquet"