| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import csv |
| 4 |
import sqlite3 |
| 5 |
import sys |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
import duckdb |
| 9 |
import pytest |
| 10 |
|
| 11 |
from harlequin import Harlequin |
| 12 |
from harlequin.adapter import HarlequinAdapter |
| 13 |
from harlequin.locale_manager import set_locale |
| 14 |
from harlequin.windows_timezone import check_and_install_tzdata |
| 15 |
|
| 16 |
if sys.version_info < (3, 10): |
| 17 |
from importlib_metadata import entry_points |
| 18 |
else: |
| 19 |
from importlib.metadata import entry_points |
| 20 |
|
| 21 |
|
| 22 |
# The committed snapshots are generated on the lowest supported Python. On 3.12+, |
| 23 |
# SQLite grows a transaction button, so the tests that show one render differently |
| 24 |
# and skip their snapshot assertions (see the transaction_button_visible fixture). |
| 25 |
SNAPSHOT_PYTHON = (3, 10) |
| 26 |
|
| 27 |
|
| 28 |
def pytest_configure(config: pytest.Config) -> None: |
| 29 |
"""Stop --snapshot-update on a newer Python from clobbering the baseline. |
| 30 |
|
| 31 |
A full update run on 3.12+ silently rewrites the committed snapshots with |
| 32 |
3.12 output, and deletes the ones those tests never take. The py12-only |
| 33 |
snapshots can't be generated on 3.10 (their tests are skipped there), so |
| 34 |
updating those on 3.12 is allowed -- but such a run covers a slice of the |
| 35 |
suite, so it must not prune everything it didn't take. |
| 36 |
""" |
| 37 |
if not config.option.update_snapshots: |
| 38 |
return |
| 39 |
if sys.version_info[:2] == SNAPSHOT_PYTHON: |
| 40 |
return |
| 41 |
|
| 42 |
baseline = ".".join(str(v) for v in SNAPSHOT_PYTHON) |
| 43 |
if "py12" not in (config.option.markexpr or ""): |
| 44 |
raise pytest.UsageError( |
| 45 |
f"--snapshot-update must run on Python {baseline}, which is what the " |
| 46 |
"committed snapshots were generated on:\n" |
| 47 |
" uv run pytest --snapshot-update\n" |
| 48 |
"To update the py12-only snapshots, which can only be generated on " |
| 49 |
"3.12+, select just those tests:\n" |
| 50 |
" uv run --python 3.12 --group test pytest -m 'py12 and not online' " |
| 51 |
"--snapshot-update" |
| 52 |
) |
| 53 |
config.option.no_cleanup = True |
| 54 |
|
| 55 |
|
| 56 |
@pytest.fixture(scope="session", autouse=True) |
| 57 |
def install_tzdata() -> None: |
| 58 |
if sys.platform == "win32": |
| 59 |
check_and_install_tzdata() |
| 60 |
|
| 61 |
|
| 62 |
@pytest.fixture(scope="session", autouse=True) |
| 63 |
def set_locale_to_enUS() -> None: |
| 64 |
set_locale("en_US.UTF-8") |
| 65 |
|
| 66 |
|
| 67 |
@pytest.fixture(autouse=True) |
| 68 |
def no_discovered_config(monkeypatch: pytest.MonkeyPatch) -> None: |
| 69 |
"""Keep the machine running the tests out of them. |
| 70 |
|
| 71 |
Config discovery walks the home directory, the user config dir and the cwd, |
| 72 |
so without this a developer's own `.harlequin.toml` decides what a test |
| 73 |
asserts. |
| 74 |
|
| 75 |
The search is the seam, rather than `load_config()` or a command's own |
| 76 |
`load_profile()`, because it is the one both commands share *and* that |
| 77 |
leaves `--config-path` working -- a test that passes an explicit config |
| 78 |
file still gets it. A test about discovery itself patches the seam back |
| 79 |
(see `test_discover_config_files`) or replaces it with directories of its |
| 80 |
own (see the `config_dirs` fixture). |
| 81 |
""" |
| 82 |
monkeypatch.setattr("harlequin.config._search_directories", list) |
| 83 |
|
| 84 |
|
| 85 |
@pytest.fixture |
| 86 |
def data_dir() -> Path: |
| 87 |
here = Path(__file__) |
| 88 |
return here.parent / "data" |
| 89 |
|
| 90 |
|
| 91 |
@pytest.fixture |
| 92 |
def fake_ssh_client(data_dir: Path) -> Path: |
| 93 |
"""The stand-in for the `ssh` binary, which needs no server to bind a forward.""" |
| 94 |
return data_dir / "unit_tests" / "ssh" / "ssh" |
| 95 |
|
| 96 |
|
| 97 |
@pytest.fixture |
| 98 |
def drop_trigger(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: |
| 99 |
"""Touch the path this returns, and the fake client drops its forwards.""" |
| 100 |
path = tmp_path / "drop" |
| 101 |
monkeypatch.setenv("FAKE_SSH_DROP_WHEN", str(path)) |
| 102 |
return path |
| 103 |
|
| 104 |
|
| 105 |
@pytest.fixture |
| 106 |
def tiny_duck(tmp_path: Path, data_dir: Path) -> Path: |
| 107 |
""" |
| 108 |
Creates a duckdb database file from the contents of |
| 109 |
data_dir/functional_tests/tiny |
| 110 |
""" |
| 111 |
path_to_data = data_dir / "functional_tests" / "tiny" |
| 112 |
path_to_db = tmp_path / "tiny.db" |
| 113 |
conn = duckdb.connect(str(path_to_db)) |
| 114 |
conn.execute(f"import database '{path_to_data}';") |
| 115 |
return path_to_db |
| 116 |
|
| 117 |
|
| 118 |
@pytest.fixture |
| 119 |
def tiny_sqlite(tmp_path: Path, data_dir: Path) -> Path: |
| 120 |
path_to_data = data_dir / "functional_tests" / "tiny" |
| 121 |
path_to_db = tmp_path / "tiny.sqlite" |
| 122 |
_create_sqlite_db_from_data_dir(path_to_data, path_to_db) |
| 123 |
return path_to_db |
| 124 |
|
| 125 |
|
| 126 |
@pytest.fixture |
| 127 |
def small_duck(tmp_path: Path, data_dir: Path) -> Path: |
| 128 |
""" |
| 129 |
Creates a duckdb database file from the contents of |
| 130 |
data_dir/functional_tests/small |
| 131 |
""" |
| 132 |
path_to_data = data_dir / "functional_tests" / "small" |
| 133 |
path_to_db = tmp_path / "small.db" |
| 134 |
conn = duckdb.connect(str(path_to_db)) |
| 135 |
conn.execute(f"import database '{path_to_data}';") |
| 136 |
return path_to_db |
| 137 |
|
| 138 |
|
| 139 |
@pytest.fixture |
| 140 |
def small_sqlite(tmp_path: Path, data_dir: Path) -> Path: |
| 141 |
path_to_data = data_dir / "functional_tests" / "small" |
| 142 |
path_to_db = tmp_path / "small.sqlite" |
| 143 |
_create_sqlite_db_from_data_dir(path_to_data, path_to_db) |
| 144 |
return path_to_db |
| 145 |
|
| 146 |
|
| 147 |
@pytest.fixture |
| 148 |
def duckdb_adapter() -> type[HarlequinAdapter]: |
| 149 |
eps = entry_points(group="harlequin.adapter") |
| 150 |
cls: type[HarlequinAdapter] = eps["duckdb"].load() |
| 151 |
return cls |
| 152 |
|
| 153 |
|
| 154 |
@pytest.fixture |
| 155 |
def sqlite_adapter() -> type[HarlequinAdapter]: |
| 156 |
eps = entry_points(group="harlequin.adapter") |
| 157 |
cls: type[HarlequinAdapter] = eps["sqlite"].load() |
| 158 |
return cls |
| 159 |
|
| 160 |
|
| 161 |
@pytest.fixture(params=["duckdb", "sqlite"]) |
| 162 |
def all_adapters(request: pytest.FixtureRequest) -> type[HarlequinAdapter]: |
| 163 |
eps = entry_points(group="harlequin.adapter") |
| 164 |
cls: type[HarlequinAdapter] = eps[request.param].load() |
| 165 |
return cls |
| 166 |
|
| 167 |
|
| 168 |
@pytest.fixture |
| 169 |
def app(duckdb_adapter: type[HarlequinAdapter]) -> Harlequin: |
| 170 |
return Harlequin(duckdb_adapter([":memory:"], no_init=True), connection_hash="foo") |
| 171 |
|
| 172 |
|
| 173 |
@pytest.fixture |
| 174 |
def app_all_adapters(all_adapters: type[HarlequinAdapter]) -> Harlequin: |
| 175 |
return Harlequin(all_adapters([":memory:"], no_init=True), connection_hash="foo") |
| 176 |
|
| 177 |
|
| 178 |
@pytest.fixture |
| 179 |
def app_small_duck( |
| 180 |
duckdb_adapter: type[HarlequinAdapter], small_duck: Path |
| 181 |
) -> Harlequin: |
| 182 |
return Harlequin( |
| 183 |
duckdb_adapter([str(small_duck)], no_init=True), connection_hash="small" |
| 184 |
) |
| 185 |
|
| 186 |
|
| 187 |
@pytest.fixture |
| 188 |
def app_small_sqlite( |
| 189 |
sqlite_adapter: type[HarlequinAdapter], small_sqlite: Path |
| 190 |
) -> Harlequin: |
| 191 |
return Harlequin( |
| 192 |
sqlite_adapter([str(small_sqlite)], no_init=True), connection_hash="bar" |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
@pytest.fixture(params=["duckdb", "sqlite"]) |
| 197 |
def app_all_adapters_small_db( |
| 198 |
request: pytest.FixtureRequest, |
| 199 |
app_small_duck: Harlequin, |
| 200 |
app_small_sqlite: Harlequin, |
| 201 |
) -> Harlequin: |
| 202 |
if request.param == "duckdb": |
| 203 |
return app_small_duck |
| 204 |
else: |
| 205 |
return app_small_sqlite |
| 206 |
|
| 207 |
|
| 208 |
@pytest.fixture |
| 209 |
def app_multi_duck( |
| 210 |
duckdb_adapter: type[HarlequinAdapter], tiny_duck: Path, small_duck: Path |
| 211 |
) -> Harlequin: |
| 212 |
return Harlequin( |
| 213 |
duckdb_adapter([str(tiny_duck), str(small_duck)], no_init=True), |
| 214 |
connection_hash="multi", |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
def _create_sqlite_db_from_data_dir(data_dir: Path, db_path: Path) -> None: |
| 219 |
SINGLE_QUOTE = "'" |
| 220 |
DOUBLED_SINGLE_QUOTE = "''" |
| 221 |
conn = sqlite3.connect(str(db_path)) |
| 222 |
with open(data_dir / "schema_sqlite.sql") as f: |
| 223 |
ddl = f.read() |
| 224 |
for q in ddl.split(";"): |
| 225 |
conn.execute(q) |
| 226 |
for p in data_dir.iterdir(): |
| 227 |
if p.is_file() and p.suffix == ".csv": |
| 228 |
with p.open("r", encoding="utf-8") as f: |
| 229 |
reader = csv.reader(f) |
| 230 |
for row in reader: |
| 231 |
quoted = [ |
| 232 |
( |
| 233 |
val |
| 234 |
if isinstance(val, (int, float)) |
| 235 |
else f"'{val.replace(SINGLE_QUOTE, DOUBLED_SINGLE_QUOTE)}'" |
| 236 |
) |
| 237 |
for val in row |
| 238 |
] |
| 239 |
conn.execute(f"insert into {p.stem} values({', '.join(quoted)})") |
| 240 |
conn.commit() |