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