| 1 |
import re |
| 2 |
import shlex |
| 3 |
import sqlite3 |
| 4 |
from pathlib import Path |
| 5 |
from typing import Any |
| 6 |
from unittest.mock import MagicMock |
| 7 |
|
| 8 |
import pytest |
| 9 |
from click.testing import CliRunner, Result |
| 10 |
|
| 11 |
from harlequin import Harlequin |
| 12 |
from harlequin.cli import ( |
| 13 |
DEFAULT_KEYMAP_NAMES, |
| 14 |
DEFAULT_THEME, |
| 15 |
DEFAULT_VIEWER_MAX_ROWS, |
| 16 |
HEADLESS_DOCS_URL, |
| 17 |
build_cli, |
| 18 |
hsql_profile_keys, |
| 19 |
hsql_spellings, |
| 20 |
) |
| 21 |
from harlequin.config import Config, Provenance, _merge |
| 22 |
from harlequin_duckdb import DUCKDB_OPTIONS, DuckDbAdapter |
| 23 |
from harlequin_sqlite import SQLITE_OPTIONS, HarlequinSqliteAdapter |
| 24 |
|
| 25 |
|
| 26 |
def invoke(runner: CliRunner, args: str | list[str] = "", **kwargs: Any) -> Result: |
| 27 |
"""Build the command for these arguments, then run it against them. |
| 28 |
|
| 29 |
The two are one step for a caller -- `harlequin()` does exactly this with |
| 30 |
`sys.argv` -- because the command is built from the arguments: which |
| 31 |
adapter's connection options it carries is what the first pass over them |
| 32 |
settles. |
| 33 |
""" |
| 34 |
argv = shlex.split(args) if isinstance(args, str) else args |
| 35 |
return runner.invoke(build_cli(argv), args=argv, **kwargs) |
| 36 |
|
| 37 |
|
| 38 |
@pytest.fixture() |
| 39 |
def mock_adapter(monkeypatch: pytest.MonkeyPatch) -> MagicMock: |
| 40 |
mock_adapter = MagicMock(name="mock_duckdb_adapter", spec=DuckDbAdapter) |
| 41 |
mock_adapter.ADAPTER_OPTIONS = DUCKDB_OPTIONS |
| 42 |
mock_adapter.profile_name = None |
| 43 |
mock_entrypoint = MagicMock(name="mock_entrypoint") |
| 44 |
mock_entrypoint.name = "duckdb" |
| 45 |
mock_entrypoint.load.return_value = mock_adapter |
| 46 |
mock_entry_points = MagicMock() |
| 47 |
mock_entry_points.return_value = [mock_entrypoint] |
| 48 |
monkeypatch.setattr("harlequin.plugins.entry_points", mock_entry_points) |
| 49 |
return mock_adapter |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture() |
| 53 |
def mock_sqlite_adapter(monkeypatch: pytest.MonkeyPatch) -> MagicMock: |
| 54 |
mock_adapter = MagicMock(name="mock_sqlite_adapter", spec=HarlequinSqliteAdapter) |
| 55 |
mock_adapter.ADAPTER_OPTIONS = SQLITE_OPTIONS |
| 56 |
mock_entrypoint = MagicMock(name="mock_entrypoint") |
| 57 |
mock_entrypoint.name = "sqlite" |
| 58 |
mock_entrypoint.load.return_value = mock_adapter |
| 59 |
mock_entry_points = MagicMock() |
| 60 |
mock_entry_points.return_value = [mock_entrypoint] |
| 61 |
monkeypatch.setattr("harlequin.plugins.entry_points", mock_entry_points) |
| 62 |
return mock_adapter |
| 63 |
|
| 64 |
|
| 65 |
@pytest.fixture() |
| 66 |
def mock_harlequin(monkeypatch: pytest.MonkeyPatch) -> MagicMock: |
| 67 |
mock = MagicMock(spec=Harlequin) |
| 68 |
# the command exits with it, and a MagicMock is not an exit code |
| 69 |
mock.return_value.return_code = 0 |
| 70 |
monkeypatch.setattr("harlequin.cli.Harlequin", mock) |
| 71 |
return mock |
| 72 |
|
| 73 |
|
| 74 |
@pytest.fixture() |
| 75 |
def mock_empty_config(no_discovered_config: None) -> None: |
| 76 |
"""No config file anywhere. See tests/conftest.py.""" |
| 77 |
|
| 78 |
|
| 79 |
@pytest.fixture() |
| 80 |
def mock_load_config(monkeypatch: pytest.MonkeyPatch) -> Config: |
| 81 |
"""A merged config, in place of whatever is on the machine running this. |
| 82 |
|
| 83 |
Merged by the real `_merge()` rather than handed over as a literal, because |
| 84 |
a caller that asks for a `Provenance` gets one filled in -- and a double |
| 85 |
that filled it in itself would be a second copy of the merge's bookkeeping. |
| 86 |
""" |
| 87 |
config = Config(profiles={"test-profile": {"theme": "fruity"}}) |
| 88 |
|
| 89 |
def load_config( |
| 90 |
config_path: Path | None = None, provenance: Provenance | None = None |
| 91 |
) -> Config: |
| 92 |
merged = Config() |
| 93 |
_merge(config, into=merged, source=Path("test.toml"), provenance=provenance) |
| 94 |
return merged |
| 95 |
|
| 96 |
monkeypatch.setattr("harlequin.config.load_config", load_config) |
| 97 |
return config |
| 98 |
|
| 99 |
|
| 100 |
def test_help(mock_adapter: MagicMock, mock_empty_config: None) -> None: |
| 101 |
"""--help has to actually render. |
| 102 |
|
| 103 |
It is the one command that exercises rich-click's help rendering, so a |
| 104 |
rich-click that is out of step with click's API breaks it and nothing else. |
| 105 |
""" |
| 106 |
runner = CliRunner() |
| 107 |
# the option groups are keyed on the program name, so this has to match the |
| 108 |
# name the console script is installed under |
| 109 |
res = invoke(runner, "--help", prog_name="harlequin") |
| 110 |
assert res.exception is None, f"--help raised {res.exception!r}" |
| 111 |
assert res.exit_code == 0 |
| 112 |
# the option groups configured at the top of cli.py, including the ones |
| 113 |
# built dynamically from the installed adapters |
| 114 |
assert "Harlequin Options" in res.output |
| 115 |
assert "duckdb Adapter Options" in res.output |
| 116 |
# an option's help text, and a metavar appended to it |
| 117 |
assert "--theme" in res.output |
| 118 |
assert "(TEXT)" in res.output |
| 119 |
|
| 120 |
|
| 121 |
def test_no_write_history_typed( |
| 122 |
mock_harlequin: MagicMock, mock_adapter: MagicMock, mock_empty_config: None |
| 123 |
) -> None: |
| 124 |
runner = CliRunner() |
| 125 |
res = invoke(runner, "--no-write-history") |
| 126 |
assert res.exit_code == 0 |
| 127 |
assert mock_harlequin.call_args.kwargs["record_history"] is False |
| 128 |
|
| 129 |
|
| 130 |
def test_no_write_history_from_a_profile( |
| 131 |
mock_harlequin: MagicMock, |
| 132 |
mock_adapter: MagicMock, |
| 133 |
tmp_path: Path, |
| 134 |
) -> None: |
| 135 |
config = tmp_path / ".harlequin.toml" |
| 136 |
config.write_text("[profiles.quiet]\nadapter = 'duckdb'\nno_write_history = true\n") |
| 137 |
runner = CliRunner() |
| 138 |
res = invoke(runner, ["--config-path", str(config), "-P", "quiet"]) |
| 139 |
assert res.exit_code == 0 |
| 140 |
assert mock_harlequin.call_args.kwargs["record_history"] is False |
| 141 |
|
| 142 |
|
| 143 |
def test_no_write_history_that_is_not_a_boolean_is_refused( |
| 144 |
mock_harlequin: MagicMock, |
| 145 |
mock_adapter: MagicMock, |
| 146 |
tmp_path: Path, |
| 147 |
) -> None: |
| 148 |
config = tmp_path / ".harlequin.toml" |
| 149 |
config.write_text( |
| 150 |
"[profiles.odd]\nadapter = 'duckdb'\nno_write_history = 'sometimes'\n" |
| 151 |
) |
| 152 |
runner = CliRunner() |
| 153 |
res = invoke(runner, ["--config-path", str(config), "-P", "odd"]) |
| 154 |
assert res.exit_code == 2 |
| 155 |
assert not mock_harlequin.called |
| 156 |
|
| 157 |
|
| 158 |
def test_no_download_tzdata_reaches_the_app( |
| 159 |
mock_harlequin: MagicMock, mock_adapter: MagicMock, mock_empty_config: None |
| 160 |
) -> None: |
| 161 |
"""The app decides what to do with it; the command no longer downloads.""" |
| 162 |
runner = CliRunner() |
| 163 |
res = invoke(runner, "--no-download-tzdata") |
| 164 |
assert res.exit_code == 0 |
| 165 |
assert mock_harlequin.call_args.kwargs["no_download_tzdata"] is True |
| 166 |
|
| 167 |
|
| 168 |
def test_no_download_tzdata_from_a_profile( |
| 169 |
mock_harlequin: MagicMock, |
| 170 |
mock_adapter: MagicMock, |
| 171 |
tmp_path: Path, |
| 172 |
) -> None: |
| 173 |
config = tmp_path / ".harlequin.toml" |
| 174 |
config.write_text( |
| 175 |
"[profiles.offline]\nadapter = 'duckdb'\nno_download_tzdata = true\n" |
| 176 |
) |
| 177 |
runner = CliRunner() |
| 178 |
res = invoke(runner, ["--config-path", str(config), "-P", "offline"]) |
| 179 |
assert res.exit_code == 0 |
| 180 |
assert mock_harlequin.call_args.kwargs["no_download_tzdata"] is True |
| 181 |
|
| 182 |
|
| 183 |
@pytest.mark.parametrize("harlequin_args", ["", ":memory:"]) |
| 184 |
def test_default( |
| 185 |
mock_harlequin: MagicMock, |
| 186 |
mock_adapter: MagicMock, |
| 187 |
harlequin_args: str, |
| 188 |
mock_empty_config: None, |
| 189 |
) -> None: |
| 190 |
runner = CliRunner() |
| 191 |
res = invoke(runner, harlequin_args) |
| 192 |
assert res.exit_code == 0 |
| 193 |
expected_conn_str = (harlequin_args,) if harlequin_args else tuple() |
| 194 |
mock_adapter.assert_called_once_with(conn_str=expected_conn_str, read_only=False) |
| 195 |
mock_harlequin.assert_called_once_with( |
| 196 |
adapter=mock_adapter.return_value, |
| 197 |
profile_name=None, |
| 198 |
adapter_name="duckdb", |
| 199 |
connection_hash=mock_adapter.return_value.connection_id, |
| 200 |
record_history=True, |
| 201 |
viewer_max_rows=DEFAULT_VIEWER_MAX_ROWS, |
| 202 |
query_limit=None, |
| 203 |
keymap_names=DEFAULT_KEYMAP_NAMES, |
| 204 |
user_defined_keymaps=[], |
| 205 |
theme=DEFAULT_THEME, |
| 206 |
show_files=None, |
| 207 |
show_s3=None, |
| 208 |
export_path=None, |
| 209 |
ssh_tunnel=None, |
| 210 |
no_download_tzdata=False, |
| 211 |
) |
| 212 |
|
| 213 |
|
| 214 |
@pytest.mark.parametrize( |
| 215 |
"harlequin_args", ["--init-path foo", ":memory: -i foo", "-init foo"] |
| 216 |
) |
| 217 |
def test_custom_init_script( |
| 218 |
mock_harlequin: MagicMock, |
| 219 |
mock_adapter: MagicMock, |
| 220 |
harlequin_args: str, |
| 221 |
mock_empty_config: None, |
| 222 |
) -> None: |
| 223 |
runner = CliRunner() |
| 224 |
res = invoke(runner, harlequin_args) |
| 225 |
assert res.exit_code == 0 |
| 226 |
mock_adapter.assert_called_once() |
| 227 |
assert mock_adapter.call_args |
| 228 |
assert mock_adapter.call_args.kwargs["init_path"] == Path("foo").resolve() |
| 229 |
|
| 230 |
|
| 231 |
@pytest.mark.parametrize("harlequin_args", ["--no-init", ":memory: --no-init"]) |
| 232 |
def test_no_init_script( |
| 233 |
mock_harlequin: MagicMock, |
| 234 |
mock_adapter: MagicMock, |
| 235 |
harlequin_args: str, |
| 236 |
mock_empty_config: None, |
| 237 |
) -> None: |
| 238 |
runner = CliRunner() |
| 239 |
res = invoke(runner, harlequin_args) |
| 240 |
assert res.exit_code == 0 |
| 241 |
mock_adapter.assert_called_once() |
| 242 |
assert mock_adapter.call_args |
| 243 |
assert mock_adapter.call_args.kwargs["no_init"] is True |
| 244 |
|
| 245 |
|
| 246 |
@pytest.mark.parametrize( |
| 247 |
"harlequin_args", ["--theme one-dark", ":memory: -t one-dark", "foo.db -t one-dark"] |
| 248 |
) |
| 249 |
def test_theme( |
| 250 |
mock_harlequin: MagicMock, |
| 251 |
mock_adapter: MagicMock, |
| 252 |
harlequin_args: str, |
| 253 |
mock_empty_config: None, |
| 254 |
) -> None: |
| 255 |
runner = CliRunner() |
| 256 |
res = invoke(runner, harlequin_args) |
| 257 |
assert res.exit_code == 0 |
| 258 |
mock_harlequin.assert_called_once() |
| 259 |
assert mock_harlequin.call_args |
| 260 |
assert mock_harlequin.call_args.kwargs["theme"] == "one-dark" |
| 261 |
|
| 262 |
|
| 263 |
@pytest.mark.parametrize( |
| 264 |
("harlequin_args", "expected"), |
| 265 |
[ |
| 266 |
("--viewer-max-rows 10", 10), |
| 267 |
(":memory: --viewer-max-rows 1000000", 1_000_000), |
| 268 |
("foo.db --viewer-max-rows 5000000000", 5_000_000_000), |
| 269 |
# both spellings of "hold everything" |
| 270 |
("--viewer-max-rows 0", None), |
| 271 |
("--viewer-max-rows -1", None), |
| 272 |
], |
| 273 |
) |
| 274 |
def test_viewer_max_rows( |
| 275 |
mock_harlequin: MagicMock, |
| 276 |
mock_adapter: MagicMock, |
| 277 |
harlequin_args: str, |
| 278 |
expected: int | None, |
| 279 |
mock_empty_config: None, |
| 280 |
) -> None: |
| 281 |
runner = CliRunner() |
| 282 |
res = invoke(runner, harlequin_args) |
| 283 |
assert res.exit_code == 0 |
| 284 |
mock_harlequin.assert_called_once() |
| 285 |
assert mock_harlequin.call_args |
| 286 |
assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == expected |
| 287 |
# the display cap says nothing about what is fetched |
| 288 |
assert mock_harlequin.call_args.kwargs["query_limit"] is None |
| 289 |
|
| 290 |
|
| 291 |
@pytest.mark.parametrize( |
| 292 |
("harlequin_args", "expected"), |
| 293 |
[ |
| 294 |
("--limit 10", 10), |
| 295 |
(":memory: --limit 10", 10), |
| 296 |
("foo.db --limit 10", 10), |
| 297 |
# a header and no rows, which is how you ask what a query returns |
| 298 |
("--limit 0", 0), |
| 299 |
# every row, which is what an unset limit does too |
| 300 |
("--limit -1", None), |
| 301 |
], |
| 302 |
) |
| 303 |
def test_limit_is_the_hard_fetch_limit( |
| 304 |
mock_harlequin: MagicMock, |
| 305 |
mock_adapter: MagicMock, |
| 306 |
harlequin_args: str, |
| 307 |
expected: int | None, |
| 308 |
mock_empty_config: None, |
| 309 |
) -> None: |
| 310 |
"""The same limit hsql applies, and the Run Query Bar shows it.""" |
| 311 |
runner = CliRunner() |
| 312 |
res = invoke(runner, harlequin_args) |
| 313 |
assert res.exit_code == 0 |
| 314 |
assert mock_harlequin.call_args |
| 315 |
assert mock_harlequin.call_args.kwargs["query_limit"] == expected |
| 316 |
# the viewer's cap is untouched by it |
| 317 |
assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == DEFAULT_VIEWER_MAX_ROWS |
| 318 |
|
| 319 |
|
| 320 |
def test_unset_limit_fetches_everything( |
| 321 |
mock_harlequin: MagicMock, |
| 322 |
mock_adapter: MagicMock, |
| 323 |
mock_empty_config: None, |
| 324 |
) -> None: |
| 325 |
"""Naming nothing is the full fetch the IDE has always done.""" |
| 326 |
runner = CliRunner() |
| 327 |
res = invoke(runner, "") |
| 328 |
assert res.exit_code == 0 |
| 329 |
assert mock_harlequin.call_args |
| 330 |
assert mock_harlequin.call_args.kwargs["query_limit"] is None |
| 331 |
|
| 332 |
|
| 333 |
def test_the_two_limits_are_independent( |
| 334 |
mock_harlequin: MagicMock, |
| 335 |
mock_adapter: MagicMock, |
| 336 |
mock_empty_config: None, |
| 337 |
) -> None: |
| 338 |
runner = CliRunner() |
| 339 |
res = invoke(runner, "--limit 10 --viewer-max-rows 20") |
| 340 |
assert res.exit_code == 0 |
| 341 |
assert mock_harlequin.call_args |
| 342 |
assert mock_harlequin.call_args.kwargs["query_limit"] == 10 |
| 343 |
assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == 20 |
| 344 |
|
| 345 |
|
| 346 |
def test_a_profile_limit_is_a_fetch_limit( |
| 347 |
mock_harlequin: MagicMock, |
| 348 |
mock_adapter: MagicMock, |
| 349 |
data_dir: Path, |
| 350 |
) -> None: |
| 351 |
"""The profile that motivated the split, read the new way. |
| 352 |
|
| 353 |
`limit = 200_000` in a config file both commands share now means the same |
| 354 |
thing in both: 200,000 rows leave the database, and nothing has an opinion |
| 355 |
about what the Results Viewer does with them. |
| 356 |
""" |
| 357 |
runner = CliRunner() |
| 358 |
config_path = data_dir / "unit_tests" / "config" / "good_config.toml" |
| 359 |
res = invoke(runner, f"--config-path {config_path.as_posix()}") |
| 360 |
assert res.exit_code == 0 |
| 361 |
assert mock_harlequin.call_args |
| 362 |
assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000 |
| 363 |
assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == DEFAULT_VIEWER_MAX_ROWS |
| 364 |
|
| 365 |
|
| 366 |
@pytest.mark.parametrize("harlequin_args", ["--show-files .", "-f .", "foo.db -f ."]) |
| 367 |
def test_show_files( |
| 368 |
mock_harlequin: MagicMock, |
| 369 |
mock_adapter: MagicMock, |
| 370 |
harlequin_args: str, |
| 371 |
mock_empty_config: None, |
| 372 |
) -> None: |
| 373 |
runner = CliRunner() |
| 374 |
res = invoke(runner, harlequin_args) |
| 375 |
assert res.exit_code == 0 |
| 376 |
mock_harlequin.assert_called_once() |
| 377 |
assert mock_harlequin.call_args |
| 378 |
assert mock_harlequin.call_args.kwargs["show_files"] == Path(".") |
| 379 |
|
| 380 |
|
| 381 |
@pytest.mark.parametrize( |
| 382 |
"harlequin_args,export_path", |
| 383 |
[ |
| 384 |
("--output .harlequin", Path(".harlequin")), |
| 385 |
("-o .harlequin/", Path(".harlequin")), |
| 386 |
("-o exports/out.csv", Path("exports/out.csv")), |
| 387 |
("", None), |
| 388 |
], |
| 389 |
) |
| 390 |
def test_output_sets_the_export_path( |
| 391 |
mock_harlequin: MagicMock, |
| 392 |
mock_adapter: MagicMock, |
| 393 |
harlequin_args: str, |
| 394 |
export_path: Path | None, |
| 395 |
mock_empty_config: None, |
| 396 |
) -> None: |
| 397 |
"""`-o` is where the Data Exporter starts, whether it names a folder or a |
| 398 |
file.""" |
| 399 |
runner = CliRunner() |
| 400 |
res = invoke(runner, harlequin_args) |
| 401 |
assert res.exit_code == 0 |
| 402 |
assert mock_harlequin.call_args |
| 403 |
assert mock_harlequin.call_args.kwargs["export_path"] == export_path |
| 404 |
|
| 405 |
|
| 406 |
@pytest.mark.parametrize( |
| 407 |
"harlequin_args", |
| 408 |
[ |
| 409 |
"--adapter duckdb", |
| 410 |
"-a duckdb", |
| 411 |
"-a DUCKDB", |
| 412 |
], |
| 413 |
) |
| 414 |
def test_adapter_opt( |
| 415 |
mock_harlequin: MagicMock, |
| 416 |
mock_adapter: MagicMock, |
| 417 |
harlequin_args: str, |
| 418 |
mock_empty_config: None, |
| 419 |
) -> None: |
| 420 |
runner = CliRunner() |
| 421 |
res = invoke(runner, harlequin_args) |
| 422 |
assert res.exit_code == 0 |
| 423 |
mock_harlequin.assert_called_once() |
| 424 |
assert mock_harlequin.call_args |
| 425 |
assert mock_harlequin.call_args.kwargs["adapter"] == mock_adapter.return_value |
| 426 |
|
| 427 |
|
| 428 |
@pytest.mark.parametrize( |
| 429 |
"harlequin_args", |
| 430 |
[ |
| 431 |
"--adapter foo", |
| 432 |
"-a bar", |
| 433 |
], |
| 434 |
) |
| 435 |
def test_bad_adapter_opt( |
| 436 |
mock_harlequin: MagicMock, |
| 437 |
mock_adapter: MagicMock, |
| 438 |
harlequin_args: str, |
| 439 |
mock_empty_config: None, |
| 440 |
) -> None: |
| 441 |
runner = CliRunner() |
| 442 |
res = invoke(runner, harlequin_args) |
| 443 |
assert res.exit_code == 2 |
| 444 |
key_words = ["Error", "Invalid", "-a", "-adapter", "duckdb"] |
| 445 |
assert all([w in res.stderr for w in key_words]) |
| 446 |
|
| 447 |
|
| 448 |
@pytest.mark.parametrize( |
| 449 |
"harlequin_args", |
| 450 |
[ |
| 451 |
"--profile test-profile", |
| 452 |
"-P test-profile", |
| 453 |
], |
| 454 |
) |
| 455 |
def test_profile_opt( |
| 456 |
mock_harlequin: MagicMock, |
| 457 |
mock_adapter: MagicMock, |
| 458 |
harlequin_args: str, |
| 459 |
mock_load_config: Config, |
| 460 |
) -> None: |
| 461 |
runner = CliRunner() |
| 462 |
res = invoke(runner, harlequin_args) |
| 463 |
assert res.exit_code == 0 |
| 464 |
mock_harlequin.assert_called_once() |
| 465 |
assert mock_harlequin.call_args |
| 466 |
assert mock_harlequin.call_args.kwargs["theme"] == "fruity" |
| 467 |
|
| 468 |
|
| 469 |
@pytest.mark.parametrize( |
| 470 |
"harlequin_args", |
| 471 |
[ |
| 472 |
"--profile test-profile -t zenburn", |
| 473 |
"-P test-profile --theme zenburn", |
| 474 |
], |
| 475 |
) |
| 476 |
def test_profile_override( |
| 477 |
mock_harlequin: MagicMock, |
| 478 |
mock_adapter: MagicMock, |
| 479 |
harlequin_args: str, |
| 480 |
mock_load_config: Config, |
| 481 |
) -> None: |
| 482 |
runner = CliRunner() |
| 483 |
res = invoke(runner, harlequin_args) |
| 484 |
assert res.exit_code == 0 |
| 485 |
mock_harlequin.assert_called_once() |
| 486 |
assert mock_harlequin.call_args |
| 487 |
assert mock_harlequin.call_args.kwargs["theme"] == "zenburn" |
| 488 |
|
| 489 |
|
| 490 |
@pytest.mark.parametrize( |
| 491 |
"harlequin_args", |
| 492 |
[ |
| 493 |
"--profile foo", |
| 494 |
"-P bar", |
| 495 |
], |
| 496 |
) |
| 497 |
def test_bad_profile_opt( |
| 498 |
mock_harlequin: MagicMock, |
| 499 |
mock_adapter: MagicMock, |
| 500 |
harlequin_args: str, |
| 501 |
mock_load_config: Config, |
| 502 |
) -> None: |
| 503 |
runner = CliRunner() |
| 504 |
res = invoke(runner, harlequin_args) |
| 505 |
assert res.exit_code == 2 |
| 506 |
key_words = ["profile", "config"] |
| 507 |
assert all([w in res.stderr for w in key_words]) |
| 508 |
|
| 509 |
|
| 510 |
@pytest.mark.parametrize("filename", ["good_config.toml", "pyproject.toml"]) |
| 511 |
def test_config_path( |
| 512 |
mock_harlequin: MagicMock, |
| 513 |
mock_adapter: MagicMock, |
| 514 |
data_dir: Path, |
| 515 |
filename: str, |
| 516 |
) -> None: |
| 517 |
runner = CliRunner() |
| 518 |
config_path = data_dir / "unit_tests" / "config" / filename |
| 519 |
res = invoke(runner, f"--config-path {config_path.as_posix()}") |
| 520 |
assert res.exit_code == 0 |
| 521 |
mock_harlequin.assert_called_once() |
| 522 |
assert mock_harlequin.call_args |
| 523 |
# should use default profile of my-duckdb-profile |
| 524 |
assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000 |
| 525 |
mock_adapter.assert_called_once() |
| 526 |
assert mock_adapter.call_args.kwargs["conn_str"] == ["my-database.db"] |
| 527 |
assert mock_adapter.call_args.kwargs["read_only"] is False |
| 528 |
assert mock_adapter.call_args.kwargs["extension"] == ["httpfs", "spatial"] |
| 529 |
|
| 530 |
|
| 531 |
@pytest.mark.parametrize("filename", ["good_config.toml", "pyproject.toml"]) |
| 532 |
def test_config_path_fron_env( |
| 533 |
mock_harlequin: MagicMock, |
| 534 |
mock_adapter: MagicMock, |
| 535 |
data_dir: Path, |
| 536 |
filename: str, |
| 537 |
) -> None: |
| 538 |
runner = CliRunner() |
| 539 |
config_path = data_dir / "unit_tests" / "config" / filename |
| 540 |
res = invoke(runner, env={"HARLEQUIN_CONFIG_PATH": config_path.as_posix()}) |
| 541 |
assert res.exit_code == 0 |
| 542 |
mock_harlequin.assert_called_once() |
| 543 |
assert mock_harlequin.call_args |
| 544 |
# should use default profile of my-duckdb-profile |
| 545 |
assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000 |
| 546 |
mock_adapter.assert_called_once() |
| 547 |
assert mock_adapter.call_args.kwargs["conn_str"] == ["my-database.db"] |
| 548 |
assert mock_adapter.call_args.kwargs["read_only"] is False |
| 549 |
assert mock_adapter.call_args.kwargs["extension"] == ["httpfs", "spatial"] |
| 550 |
|
| 551 |
|
| 552 |
@pytest.mark.parametrize("filename", ["good_config.toml", "pyproject.toml"]) |
| 553 |
def test_conn_str_overrides_the_profile( |
| 554 |
mock_harlequin: MagicMock, |
| 555 |
mock_adapter: MagicMock, |
| 556 |
data_dir: Path, |
| 557 |
filename: str, |
| 558 |
) -> None: |
| 559 |
"""A conn_str on the command line beats the profile's. |
| 560 |
|
| 561 |
test_config_path is the other half: with none passed, the profile's survives. |
| 562 |
""" |
| 563 |
runner = CliRunner() |
| 564 |
config_path = data_dir / "unit_tests" / "config" / filename |
| 565 |
res = invoke(runner, f"--config-path {config_path.as_posix()} other.db") |
| 566 |
assert res.exit_code == 0 |
| 567 |
mock_adapter.assert_called_once() |
| 568 |
assert mock_adapter.call_args.kwargs["conn_str"] == ("other.db",) |
| 569 |
# unrelated profile values are untouched |
| 570 |
assert mock_adapter.call_args.kwargs["extension"] == ["httpfs", "spatial"] |
| 571 |
assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000 |
| 572 |
|
| 573 |
|
| 574 |
def test_bad_config_exits( |
| 575 |
mock_harlequin: MagicMock, |
| 576 |
mock_adapter: MagicMock, |
| 577 |
data_dir: Path, |
| 578 |
) -> None: |
| 579 |
runner = CliRunner() |
| 580 |
config_path = data_dir / "unit_tests" / "config" / "default_no_exist.toml" |
| 581 |
res = invoke(runner, f"--config-path {config_path.as_posix()}") |
| 582 |
assert res.exit_code == 2 |
| 583 |
key_words = ["default_profile", "foo"] |
| 584 |
assert all([w in res.stderr for w in key_words]) |
| 585 |
|
| 586 |
|
| 587 |
def test_a_profile_naming_an_uninstalled_adapter_says_so( |
| 588 |
mock_harlequin: MagicMock, |
| 589 |
mock_adapter: MagicMock, |
| 590 |
tmp_path: Path, |
| 591 |
) -> None: |
| 592 |
"""The one name the command's `--adapter` Choice never sees. |
| 593 |
|
| 594 |
click vets what is typed, so a name nothing installed provides can only |
| 595 |
arrive from a config file -- and that is the profile the pass over the |
| 596 |
arguments could not turn into an adapter. |
| 597 |
""" |
| 598 |
config_path = tmp_path / ".harlequin.toml" |
| 599 |
config_path.write_text('[profiles.gone]\nadapter = "notreal"\n') |
| 600 |
runner = CliRunner() |
| 601 |
res = invoke(runner, f"--config-path {config_path.as_posix()} -P gone") |
| 602 |
assert res.exit_code == 2 |
| 603 |
said = _said(res.stderr) |
| 604 |
assert "notreal" in said |
| 605 |
# and what it could have been |
| 606 |
assert "duckdb" in said |
| 607 |
|
| 608 |
|
| 609 |
@pytest.mark.skipif( |
| 610 |
not hasattr(sqlite3.Connection, "enable_load_extension"), |
| 611 |
reason="Extension option not supported on many pythons.", |
| 612 |
) |
| 613 |
def test_sqlite_extensions( |
| 614 |
mock_harlequin: MagicMock, |
| 615 |
mock_sqlite_adapter: MagicMock, |
| 616 |
mock_empty_config: None, |
| 617 |
data_dir: Path, |
| 618 |
) -> None: |
| 619 |
extension_path = data_dir / "unit_tests" / "sqlite_extension" / "hello0" |
| 620 |
runner = CliRunner() |
| 621 |
res = invoke(runner, f"-a sqlite --extension {extension_path.as_posix()}") |
| 622 |
assert res.exit_code == 0 |
| 623 |
|
| 624 |
|
| 625 |
@pytest.mark.skipif( |
| 626 |
hasattr(sqlite3.Connection, "enable_load_extension"), |
| 627 |
reason="Extension option not supported on many pythons.", |
| 628 |
) |
| 629 |
def test_sqlite_extension_not_supported( |
| 630 |
mock_harlequin: MagicMock, |
| 631 |
mock_sqlite_adapter: MagicMock, |
| 632 |
mock_empty_config: None, |
| 633 |
data_dir: Path, |
| 634 |
) -> None: |
| 635 |
extension_path = data_dir / "unit_tests" / "sqlite_extension" / "hello0" |
| 636 |
runner = CliRunner() |
| 637 |
res = invoke(runner, f"-a sqlite --extension {extension_path.as_posix()}") |
| 638 |
assert res.exit_code == 2 |
| 639 |
assert "No such option" in res.stderr |
| 640 |
|
| 641 |
|
| 642 |
# --- read-only --------------------------------------------------------------- |
| 643 |
|
| 644 |
|
| 645 |
@pytest.mark.parametrize("harlequin_args", ["--read-only", "-r"]) |
| 646 |
def test_read_only_reaches_the_adapter_however_it_is_spelled( |
| 647 |
mock_harlequin: MagicMock, |
| 648 |
mock_adapter: MagicMock, |
| 649 |
harlequin_args: str, |
| 650 |
mock_empty_config: None, |
| 651 |
) -> None: |
| 652 |
"""It is an argument of the adapter's constructor rather than an option any |
| 653 |
adapter declares, so every adapter takes it under the same name.""" |
| 654 |
runner = CliRunner() |
| 655 |
res = invoke(runner, harlequin_args) |
| 656 |
assert res.exit_code == 0 |
| 657 |
assert mock_adapter.call_args |
| 658 |
assert mock_adapter.call_args.kwargs["read_only"] is True |
| 659 |
|
| 660 |
|
| 661 |
def test_read_only_from_a_profile_reaches_the_adapter( |
| 662 |
mock_harlequin: MagicMock, mock_adapter: MagicMock, data_dir: Path |
| 663 |
) -> None: |
| 664 |
"""`read_only` is the command's key in a profile, whatever adapter it names.""" |
| 665 |
config_path = data_dir / "unit_tests" / "config" / "good_config.toml" |
| 666 |
runner = CliRunner() |
| 667 |
res = invoke(runner, f"--config-path {config_path.as_posix()}") |
| 668 |
assert res.exit_code == 0 |
| 669 |
assert mock_adapter.call_args |
| 670 |
assert mock_adapter.call_args.kwargs["read_only"] is False |
| 671 |
|
| 672 |
|
| 673 |
def test_read_only_refuses_an_adapter_that_does_not_declare_it( |
| 674 |
mock_harlequin: MagicMock, mock_adapter: MagicMock, mock_empty_config: None |
| 675 |
) -> None: |
| 676 |
"""Before the app starts, and before the adapter is constructed: an adapter |
| 677 |
that cannot enforce read-only is free to ignore the argument, and a session |
| 678 |
that believed it was read-only would write.""" |
| 679 |
mock_adapter.IMPLEMENTS_READ_ONLY = False |
| 680 |
runner = CliRunner() |
| 681 |
res = invoke(runner, "--read-only") |
| 682 |
assert res.exit_code == 2 |
| 683 |
mock_adapter.assert_not_called() |
| 684 |
mock_harlequin.assert_not_called() |
| 685 |
|
| 686 |
|
| 687 |
# --- pointing at the other command ------------------------------------------- |
| 688 |
|
| 689 |
_ANSI = re.compile(r"\x1b(?:\[[0-9;]*[a-zA-Z]|\][^\x1b\x07]*(?:\x1b\\|\x07))") |
| 690 |
|
| 691 |
|
| 692 |
def _said(stderr: str) -> str: |
| 693 |
"""What a reader sees, out of what rich-click wrote. |
| 694 |
|
| 695 |
rich-click renders errors into a panel at truecolor, so the message arrives |
| 696 |
styled, wrapped at whatever width the terminal happens to be, and with the |
| 697 |
option name colored -- which puts escape sequences in the middle of the |
| 698 |
sentence, not only around it. |
| 699 |
""" |
| 700 |
return " ".join(_ANSI.sub("", stderr).split()) |
| 701 |
|
| 702 |
|
| 703 |
@pytest.mark.parametrize("option", ["-c", "--command", "--csv", "-A", "--null-string"]) |
| 704 |
def test_an_hsql_option_names_hsql( |
| 705 |
mock_adapter: MagicMock, mock_empty_config: None, option: str |
| 706 |
) -> None: |
| 707 |
"""The likeliest first mistake now that there are two commands.""" |
| 708 |
runner = CliRunner() |
| 709 |
res = invoke(runner, [option, "select 1"]) |
| 710 |
assert res.exit_code == 2 |
| 711 |
# rich-click wraps the panel it renders errors in, so the message arrives |
| 712 |
# broken across lines at whatever width the terminal happens to be |
| 713 |
said = _said(res.stderr) |
| 714 |
assert f"{option} is not a harlequin option" in said |
| 715 |
assert f"Did you mean 'hsql {option}'?" in said |
| 716 |
assert HEADLESS_DOCS_URL in said |
| 717 |
|
| 718 |
|
| 719 |
def test_an_unknown_option_that_is_not_hsqls_is_unchanged( |
| 720 |
mock_adapter: MagicMock, mock_empty_config: None |
| 721 |
) -> None: |
| 722 |
"""click's own message, and its own suggestion, for everything else.""" |
| 723 |
runner = CliRunner() |
| 724 |
res = invoke(runner, ["--thmee", "nord"]) |
| 725 |
assert res.exit_code == 2 |
| 726 |
said = _said(res.stderr) |
| 727 |
# click has reworded this message across releases (8.4 quotes the option |
| 728 |
# and lists every near match), so assert on its parts rather than on one |
| 729 |
# release's sentence |
| 730 |
assert "No such option" in said |
| 731 |
assert "--thmee" in said |
| 732 |
# click's own suggestion, drawn from this command's options |
| 733 |
assert "--theme" in said |
| 734 |
assert "hsql" not in said |
| 735 |
|
| 736 |
|
| 737 |
def test_what_this_command_reads_off_the_other_one(mock_empty_config: None) -> None: |
| 738 |
"""Both are read from hsql's real command rather than copied from it. |
| 739 |
|
| 740 |
Which means asserting they are hsql's own surface and not an adapter's: |
| 741 |
building it must not import one, or the sets would grow with whatever |
| 742 |
happens to be installed. |
| 743 |
""" |
| 744 |
assert "--csv" in hsql_spellings() |
| 745 |
assert "--theme" not in hsql_spellings() |
| 746 |
assert "--no-init" not in hsql_spellings(), "an adapter's option, not hsql's" |
| 747 |
|
| 748 |
assert {"format", "stats", "on_error"} <= hsql_profile_keys() |
| 749 |
assert "no_init" not in hsql_profile_keys() |