| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from importlib.metadata import entry_points |
| 4 |
|
| 5 |
import pytest |
| 6 |
from harlequin import ( |
| 7 |
HarlequinAdapter, |
| 8 |
HarlequinCompletion, |
| 9 |
HarlequinConnection, |
| 10 |
HarlequinCursor, |
| 11 |
) |
| 12 |
from harlequin.catalog import Catalog, CatalogItem |
| 13 |
from harlequin.exception import HarlequinConnectionError, HarlequinQueryError |
| 14 |
from mysql.connector.cursor import MySQLCursor |
| 15 |
from mysql.connector.pooling import PooledMySQLConnection |
| 16 |
from textual_fastdatatable.backend import create_backend |
| 17 |
|
| 18 |
from harlequin_mysql.adapter import ( |
| 19 |
HarlequinMySQLAdapter, |
| 20 |
HarlequinMySQLConnection, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
def test_plugin_discovery() -> None: |
| 25 |
PLUGIN_NAME = "mysql" |
| 26 |
eps = entry_points(group="harlequin.adapter") |
| 27 |
assert eps[PLUGIN_NAME] |
| 28 |
adapter_cls = eps[PLUGIN_NAME].load() |
| 29 |
assert issubclass(adapter_cls, HarlequinAdapter) |
| 30 |
assert adapter_cls == HarlequinMySQLAdapter |
| 31 |
|
| 32 |
|
| 33 |
def test_connect() -> None: |
| 34 |
conn = HarlequinMySQLAdapter( |
| 35 |
conn_str=tuple(), user="root", password="example" |
| 36 |
).connect() |
| 37 |
assert isinstance(conn, HarlequinConnection) |
| 38 |
|
| 39 |
|
| 40 |
def test_init_extra_kwargs() -> None: |
| 41 |
assert HarlequinMySQLAdapter( |
| 42 |
conn_str=tuple(), user="root", password="example", foo=1, bar="baz" |
| 43 |
).connect() |
| 44 |
|
| 45 |
|
| 46 |
def test_enable_cleartext_plugin_default() -> None: |
| 47 |
adapter = HarlequinMySQLAdapter(conn_str=tuple(), user="root", password="example") |
| 48 |
assert adapter.options["allow_local_infile"] is False |
| 49 |
|
| 50 |
|
| 51 |
def test_enable_cleartext_plugin_true() -> None: |
| 52 |
adapter = HarlequinMySQLAdapter( |
| 53 |
conn_str=tuple(), user="root", password="example", enable_cleartext_plugin=True |
| 54 |
) |
| 55 |
assert adapter.options["allow_local_infile"] is True |
| 56 |
|
| 57 |
|
| 58 |
def test_enable_cleartext_plugin_false() -> None: |
| 59 |
adapter = HarlequinMySQLAdapter( |
| 60 |
conn_str=tuple(), user="root", password="example", enable_cleartext_plugin=False |
| 61 |
) |
| 62 |
assert adapter.options["allow_local_infile"] is False |
| 63 |
|
| 64 |
|
| 65 |
def test_enable_cleartext_plugin_string_true() -> None: |
| 66 |
adapter = HarlequinMySQLAdapter( |
| 67 |
conn_str=tuple(), |
| 68 |
user="root", |
| 69 |
password="example", |
| 70 |
enable_cleartext_plugin="true", |
| 71 |
) |
| 72 |
assert adapter.options["allow_local_infile"] == "true" |
| 73 |
|
| 74 |
|
| 75 |
def test_connect_raises_connection_error() -> None: |
| 76 |
with pytest.raises(HarlequinConnectionError): |
| 77 |
_ = HarlequinMySQLAdapter(conn_str=("foo",)).connect() |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.parametrize( |
| 81 |
"options,expected", |
| 82 |
[ |
| 83 |
({}, "127.0.0.1:3306/"), |
| 84 |
({"host": "foo.bar"}, "foo.bar:3306/"), |
| 85 |
({"host": "foo.bar", "port": "3305"}, "foo.bar:3305/"), |
| 86 |
({"unix_socket": "/foo/bar"}, "/foo/bar:3306/"), |
| 87 |
({"unix_socket": "/foo/bar", "database": "baz"}, "/foo/bar:3306/baz"), |
| 88 |
], |
| 89 |
) |
| 90 |
def test_connection_id(options: dict[str, str | int | None], expected: str) -> None: |
| 91 |
adapter = HarlequinMySQLAdapter( |
| 92 |
conn_str=tuple(), |
| 93 |
**options, |
| 94 |
) |
| 95 |
assert adapter.connection_id == expected |
| 96 |
|
| 97 |
|
| 98 |
def test_get_catalog(connection: HarlequinMySQLConnection) -> None: |
| 99 |
catalog = connection.get_catalog() |
| 100 |
assert isinstance(catalog, Catalog) |
| 101 |
assert catalog.items |
| 102 |
assert isinstance(catalog.items[0], CatalogItem) |
| 103 |
assert any( |
| 104 |
item.label == "test" and item.type_label == "db" for item in catalog.items |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
def test_get_completions(connection: HarlequinMySQLConnection) -> None: |
| 109 |
completions = connection.get_completions() |
| 110 |
assert completions |
| 111 |
assert isinstance(completions[0], HarlequinCompletion) |
| 112 |
expected = ["action", "var_pop"] |
| 113 |
filtered = list(filter(lambda x: x.label in expected, completions)) |
| 114 |
assert len(filtered) == len(expected) |
| 115 |
|
| 116 |
|
| 117 |
def test_execute_ddl(connection: HarlequinMySQLConnection) -> None: |
| 118 |
cur = connection.execute("create table foo (a int)") |
| 119 |
assert cur is None |
| 120 |
|
| 121 |
|
| 122 |
def test_execute_select(connection: HarlequinMySQLConnection) -> None: |
| 123 |
cur = connection.execute("select 1 as a") |
| 124 |
assert isinstance(cur, HarlequinCursor) |
| 125 |
assert cur.columns() == [("a", "##")] |
| 126 |
data = cur.fetchall() |
| 127 |
backend = create_backend(data) |
| 128 |
assert backend.column_count == 1 |
| 129 |
assert backend.row_count == 1 |
| 130 |
|
| 131 |
|
| 132 |
def test_execute_select_no_records(connection: HarlequinMySQLConnection) -> None: |
| 133 |
cur = connection.execute("select 1 as a where false") |
| 134 |
assert isinstance(cur, HarlequinCursor) |
| 135 |
assert cur.columns() == [("a", "##")] |
| 136 |
data = cur.fetchall() |
| 137 |
backend = create_backend(data) |
| 138 |
assert backend.row_count == 0 |
| 139 |
|
| 140 |
|
| 141 |
def test_execute_select_dupe_cols(connection: HarlequinMySQLConnection) -> None: |
| 142 |
cur = connection.execute("select 1 as a, 2 as a, 3 as a") |
| 143 |
assert isinstance(cur, HarlequinCursor) |
| 144 |
assert len(cur.columns()) == 3 |
| 145 |
data = cur.fetchall() |
| 146 |
backend = create_backend(data) |
| 147 |
assert backend.column_count == 3 |
| 148 |
assert backend.row_count == 1 |
| 149 |
|
| 150 |
|
| 151 |
def test_set_limit(connection: HarlequinMySQLConnection) -> None: |
| 152 |
cur = connection.execute("select 1 as a union all select 2 union all select 3") |
| 153 |
assert isinstance(cur, HarlequinCursor) |
| 154 |
cur = cur.set_limit(2) |
| 155 |
assert isinstance(cur, HarlequinCursor) |
| 156 |
data = cur.fetchall() |
| 157 |
backend = create_backend(data) |
| 158 |
assert backend.column_count == 1 |
| 159 |
assert backend.row_count == 2 |
| 160 |
|
| 161 |
|
| 162 |
def test_execute_raises_query_error(connection: HarlequinMySQLConnection) -> None: |
| 163 |
with pytest.raises(HarlequinQueryError): |
| 164 |
_ = connection.execute("selec;") |
| 165 |
|
| 166 |
|
| 167 |
def test_can_execute_pool_size_queries(connection: HarlequinMySQLConnection) -> None: |
| 168 |
pool_size = connection._pool.pool_size |
| 169 |
cursors: list[HarlequinCursor] = [] |
| 170 |
for _ in range(pool_size): |
| 171 |
cur = connection.execute("select 1") |
| 172 |
assert cur is not None |
| 173 |
cursors.append(cur) |
| 174 |
assert len(cursors) == pool_size |
| 175 |
|
| 176 |
|
| 177 |
def test_can_execute_pool_size_ddl(connection: HarlequinMySQLConnection) -> None: |
| 178 |
pool_size = connection._pool.pool_size |
| 179 |
cursors: list[None] = [] |
| 180 |
for i in range(pool_size): |
| 181 |
cur = connection.execute(f"create table t_{i} as select {i}") |
| 182 |
assert cur is None |
| 183 |
cursors.append(cur) |
| 184 |
assert len(cursors) == pool_size |
| 185 |
|
| 186 |
|
| 187 |
def test_execute_more_than_pool_size_queries_does_not_raise( |
| 188 |
connection: HarlequinMySQLConnection, |
| 189 |
) -> None: |
| 190 |
pool_size = connection._pool.pool_size |
| 191 |
cursors: list[HarlequinCursor] = [] |
| 192 |
for _ in range(pool_size * 2): |
| 193 |
cur = connection.execute("select 1") |
| 194 |
if cur is not None: |
| 195 |
cursors.append(cur) |
| 196 |
assert len(cursors) == pool_size |
| 197 |
|
| 198 |
|
| 199 |
def test_execute_more_than_pool_size_ddl_does_not_raise( |
| 200 |
connection: HarlequinMySQLConnection, |
| 201 |
) -> None: |
| 202 |
pool_size = connection._pool.pool_size |
| 203 |
number_of_ddl_queries = pool_size * 2 |
| 204 |
cursors: list[None] = [] |
| 205 |
for i in range(number_of_ddl_queries): |
| 206 |
cur = connection.execute(f"create table t_{i} as select {i}") |
| 207 |
assert cur is None |
| 208 |
cursors.append(cur) |
| 209 |
assert len(cursors) == number_of_ddl_queries |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.parametrize( |
| 213 |
"query", |
| 214 |
[ |
| 215 |
"use mysql", |
| 216 |
"use mysql;", |
| 217 |
"USE mysql;\n\n", |
| 218 |
"use `mysql`;", |
| 219 |
" use\n mysql ; ", |
| 220 |
], |
| 221 |
) |
| 222 |
def test_use_database_updates_pool( |
| 223 |
connection: HarlequinMySQLConnection, query: str |
| 224 |
) -> None: |
| 225 |
conn, cur = connection.safe_get_mysql_cursor() |
| 226 |
assert conn is not None |
| 227 |
assert cur is not None |
| 228 |
assert conn.database == "test" |
| 229 |
cur.close() |
| 230 |
conn.close() |
| 231 |
|
| 232 |
connection.execute(query) |
| 233 |
|
| 234 |
pool_size = connection._pool.pool_size |
| 235 |
|
| 236 |
conns: list[PooledMySQLConnection] = [] |
| 237 |
curs: list[MySQLCursor] = [] |
| 238 |
for _ in range(pool_size): |
| 239 |
conn, cur = connection.safe_get_mysql_cursor() |
| 240 |
assert conn is not None |
| 241 |
assert cur is not None |
| 242 |
assert conn.database == "mysql" |
| 243 |
conns.append(conn) |
| 244 |
curs.append(cur) |
| 245 |
|
| 246 |
assert len(conns) == pool_size |
| 247 |
for cur in curs: |
| 248 |
cur.close() |
| 249 |
for conn in conns: |
| 250 |
conn.close() |
| 251 |
|
| 252 |
|
| 253 |
def test_use_database_script(connection: HarlequinMySQLConnection) -> None: |
| 254 |
""" |
| 255 |
A script that starts with a USE statement should execute against the |
| 256 |
named database. See harlequin#982. |
| 257 |
""" |
| 258 |
for query in [ |
| 259 |
"use test;", |
| 260 |
"create table temp1 (\n id int\n);", |
| 261 |
"insert into temp1 values (1);", |
| 262 |
]: |
| 263 |
assert connection.execute(query) is None |
| 264 |
|
| 265 |
cur = connection.execute("select id from test.temp1") |
| 266 |
assert cur is not None |
| 267 |
assert cur.fetchall() == [(1,)] |
| 268 |
|
| 269 |
|
| 270 |
def test_close(connection: HarlequinMySQLConnection) -> None: |
| 271 |
connection.close() |
| 272 |
|
| 273 |
connection.close() |
| 274 |
|
| 275 |
|
| 276 |
def test_implements_catalog_search() -> None: |
| 277 |
assert HarlequinMySQLAdapter.IMPLEMENTS_CATALOG_SEARCH is True |
| 278 |
|
| 279 |
|
| 280 |
def test_implements_read_only() -> None: |
| 281 |
assert HarlequinMySQLAdapter.IMPLEMENTS_READ_ONLY is True |
| 282 |
|
| 283 |
|
| 284 |
@pytest.mark.parametrize("value", [True, False]) |
| 285 |
def test_read_only_option(value: bool) -> None: |
| 286 |
adapter = HarlequinMySQLAdapter( |
| 287 |
conn_str=tuple(), read_only=value, user="root", password="example" |
| 288 |
) |
| 289 |
assert adapter.read_only is value |
| 290 |
|
| 291 |
assert "read_only" not in adapter.options |
| 292 |
|
| 293 |
|
| 294 |
def test_read_only_default() -> None: |
| 295 |
adapter = HarlequinMySQLAdapter(conn_str=tuple(), user="root", password="example") |
| 296 |
assert adapter.read_only is False |
| 297 |
assert "read_only" not in adapter.options |
| 298 |
|
| 299 |
|
| 300 |
def test_read_only_connection_can_read( |
| 301 |
read_only_connection: HarlequinMySQLConnection, |
| 302 |
) -> None: |
| 303 |
cur = read_only_connection.execute("select a from foo") |
| 304 |
assert cur is not None |
| 305 |
assert cur.fetchall() == [(1,)] |
| 306 |
|
| 307 |
|
| 308 |
def test_read_only_connection_can_get_catalog( |
| 309 |
read_only_connection: HarlequinMySQLConnection, |
| 310 |
) -> None: |
| 311 |
catalog = read_only_connection.get_catalog() |
| 312 |
assert any(item.label == "test_read_only" for item in catalog.items) |
| 313 |
|
| 314 |
|
| 315 |
def test_read_only_connection_can_search_catalog( |
| 316 |
read_only_connection: HarlequinMySQLConnection, |
| 317 |
) -> None: |
| 318 |
results = read_only_connection.search_catalog("foo", "relations") |
| 319 |
assert [(result.item.label, result.parents) for result in results] == [ |
| 320 |
("foo", ("test_read_only",)) |
| 321 |
] |
| 322 |
|
| 323 |
|
| 324 |
@pytest.mark.parametrize( |
| 325 |
"query", |
| 326 |
[ |
| 327 |
"insert into foo values (2)", |
| 328 |
"update foo set a = 2", |
| 329 |
"delete from foo", |
| 330 |
"truncate table foo", |
| 331 |
"create table bar (a int)", |
| 332 |
"create temporary table bar (a int)", |
| 333 |
"drop table foo", |
| 334 |
"alter table foo add column b int", |
| 335 |
"create index foo_idx on foo (a)", |
| 336 |
"rename table foo to baz", |
| 337 |
"create view v as select a from foo", |
| 338 |
"create database test_read_only_new", |
| 339 |
], |
| 340 |
) |
| 341 |
def test_read_only_connection_refuses_writes( |
| 342 |
read_only_connection: HarlequinMySQLConnection, query: str |
| 343 |
) -> None: |
| 344 |
with pytest.raises(HarlequinQueryError): |
| 345 |
_ = read_only_connection.execute(query) |
| 346 |
|
| 347 |
cur = read_only_connection.execute("select a from foo") |
| 348 |
assert cur is not None |
| 349 |
assert cur.fetchall() == [(1,)] |
| 350 |
|
| 351 |
|
| 352 |
def test_read_only_applies_to_every_pooled_connection( |
| 353 |
read_only_connection: HarlequinMySQLConnection, |
| 354 |
) -> None: |
| 355 |
""" |
| 356 |
The pool creates connections lazily and does not reset their sessions, so |
| 357 |
check that more connections than the pool holds all refuse writes. |
| 358 |
""" |
| 359 |
for _ in range(read_only_connection._pool.pool_size * 2): |
| 360 |
with pytest.raises(HarlequinQueryError): |
| 361 |
_ = read_only_connection.execute("insert into foo values (2)") |
| 362 |
|
| 363 |
|
| 364 |
def test_read_only_survives_set_transaction_read_write( |
| 365 |
read_only_connection: HarlequinMySQLConnection, |
| 366 |
) -> None: |
| 367 |
|
| 368 |
|
| 369 |
_ = read_only_connection.execute("set session transaction read write") |
| 370 |
with pytest.raises(HarlequinQueryError): |
| 371 |
_ = read_only_connection.execute("insert into foo values (2)") |
| 372 |
|
| 373 |
|
| 374 |
def test_read_only_connection_refuses_multiple_statements( |
| 375 |
read_only_connection: HarlequinMySQLConnection, |
| 376 |
) -> None: |
| 377 |
with pytest.raises(HarlequinQueryError): |
| 378 |
_ = read_only_connection.execute( |
| 379 |
"set session transaction read write; insert into foo values (2)" |
| 380 |
) |
| 381 |
|
| 382 |
|
| 383 |
def test_read_only_connection_leaves_no_open_transaction( |
| 384 |
read_only_connection: HarlequinMySQLConnection, |
| 385 |
) -> None: |
| 386 |
_ = read_only_connection.execute("start transaction") |
| 387 |
with pytest.raises(HarlequinQueryError): |
| 388 |
_ = read_only_connection.execute("insert into foo values (2)") |
| 389 |
|
| 390 |
|
| 391 |
def test_writes_still_work_without_read_only( |
| 392 |
connection: HarlequinMySQLConnection, |
| 393 |
) -> None: |
| 394 |
assert connection.read_only is False |
| 395 |
assert connection.execute("create table rw (a int)") is None |
| 396 |
assert connection.execute("insert into rw values (1)") is None |