|
| 1 | +"""Integration tests for MySQL autocomplete column suggestions.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from sqlit.domains.shell.app.main import SSMSTUI |
| 11 | +from tests.fixtures.mysql import MYSQL_HOST, MYSQL_PASSWORD, MYSQL_PORT, MYSQL_USER |
| 12 | +from tests.helpers import ConnectionConfig |
| 13 | +from tests.integration.browsing_base import wait_for_condition |
| 14 | + |
| 15 | + |
| 16 | +EXTRA_DB_NAME = os.environ.get("MYSQL_AUTOCOMPLETE_EXTRA_DB", "test_sqlit_autocomplete_other") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def temp_config_dir(): |
| 21 | + """Create a temporary config directory for tests.""" |
| 22 | + with tempfile.TemporaryDirectory(prefix="sqlit-test-") as tmpdir: |
| 23 | + original = os.environ.get("SQLIT_CONFIG_DIR") |
| 24 | + os.environ["SQLIT_CONFIG_DIR"] = tmpdir |
| 25 | + yield tmpdir |
| 26 | + if original: |
| 27 | + os.environ["SQLIT_CONFIG_DIR"] = original |
| 28 | + else: |
| 29 | + os.environ.pop("SQLIT_CONFIG_DIR", None) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def mysql_extra_db(mysql_server_ready: bool): |
| 34 | + """Create a second user database to force multi-db autocomplete mode.""" |
| 35 | + if not mysql_server_ready: |
| 36 | + pytest.skip("MySQL is not available") |
| 37 | + |
| 38 | + try: |
| 39 | + import pymysql |
| 40 | + except ImportError: |
| 41 | + pytest.skip("PyMySQL is not installed") |
| 42 | + |
| 43 | + conn = pymysql.connect( |
| 44 | + host=MYSQL_HOST, |
| 45 | + port=MYSQL_PORT, |
| 46 | + user=MYSQL_USER, |
| 47 | + password=MYSQL_PASSWORD, |
| 48 | + database="mysql", |
| 49 | + connect_timeout=10, |
| 50 | + ) |
| 51 | + cursor = conn.cursor() |
| 52 | + cursor.execute(f"CREATE DATABASE IF NOT EXISTS `{EXTRA_DB_NAME}`") |
| 53 | + conn.commit() |
| 54 | + conn.close() |
| 55 | + |
| 56 | + yield EXTRA_DB_NAME |
| 57 | + |
| 58 | + conn = pymysql.connect( |
| 59 | + host=MYSQL_HOST, |
| 60 | + port=MYSQL_PORT, |
| 61 | + user=MYSQL_USER, |
| 62 | + password=MYSQL_PASSWORD, |
| 63 | + database="mysql", |
| 64 | + connect_timeout=10, |
| 65 | + ) |
| 66 | + cursor = conn.cursor() |
| 67 | + cursor.execute(f"DROP DATABASE IF EXISTS `{EXTRA_DB_NAME}`") |
| 68 | + conn.commit() |
| 69 | + conn.close() |
| 70 | + |
| 71 | + |
| 72 | +def _set_cursor_to_end(app: SSMSTUI) -> None: |
| 73 | + text = app.query_input.text |
| 74 | + lines = text.split("\n") |
| 75 | + app.query_input.cursor_location = (len(lines) - 1, len(lines[-1]) if lines else 0) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_mysql_alias_autocomplete_shows_columns_multi_db( |
| 80 | + mysql_server_ready: bool, |
| 81 | + mysql_db: str, |
| 82 | + mysql_extra_db: str, # noqa: ARG001 - fixture used for setup/teardown |
| 83 | + temp_config_dir: str, # noqa: ARG001 - fixture used for isolation |
| 84 | +) -> None: |
| 85 | + """Autocomplete should resolve alias columns even with multi-db schema cache.""" |
| 86 | + if not mysql_server_ready: |
| 87 | + pytest.skip("MySQL is not available") |
| 88 | + |
| 89 | + config = ConnectionConfig( |
| 90 | + name="test-mysql-autocomplete", |
| 91 | + db_type="mysql", |
| 92 | + server=MYSQL_HOST, |
| 93 | + port=str(MYSQL_PORT), |
| 94 | + database="", |
| 95 | + username=MYSQL_USER, |
| 96 | + password=MYSQL_PASSWORD, |
| 97 | + ) |
| 98 | + |
| 99 | + app = SSMSTUI() |
| 100 | + async with app.run_test(size=(120, 40)) as pilot: |
| 101 | + await pilot.pause(0.1) |
| 102 | + |
| 103 | + app.connections = [config] |
| 104 | + app.refresh_tree() |
| 105 | + await pilot.pause(0.1) |
| 106 | + |
| 107 | + await wait_for_condition( |
| 108 | + pilot, |
| 109 | + lambda: len(app.object_tree.root.children) > 0, |
| 110 | + timeout_seconds=5.0, |
| 111 | + description="tree to be populated with connections", |
| 112 | + ) |
| 113 | + |
| 114 | + app.connect_to_server(config) |
| 115 | + await wait_for_condition( |
| 116 | + pilot, |
| 117 | + lambda: app.current_connection is not None, |
| 118 | + timeout_seconds=15.0, |
| 119 | + description="connection to be established", |
| 120 | + ) |
| 121 | + |
| 122 | + # Ensure schema cache is available for autocomplete in headless tests. |
| 123 | + load_schema_async = getattr(app, "_load_schema_cache_async", None) |
| 124 | + if callable(load_schema_async): |
| 125 | + await load_schema_async() |
| 126 | + |
| 127 | + await wait_for_condition( |
| 128 | + pilot, |
| 129 | + lambda: ( |
| 130 | + "test_users" in getattr(app, "_table_metadata", {}) |
| 131 | + or f"{mysql_db}.test_users" in getattr(app, "_table_metadata", {}) |
| 132 | + ), |
| 133 | + timeout_seconds=20.0, |
| 134 | + description="table metadata to be loaded", |
| 135 | + ) |
| 136 | + |
| 137 | + app.query_input.text = f"SELECT * FROM {mysql_db}.test_users u WHERE u." |
| 138 | + _set_cursor_to_end(app) |
| 139 | + app._trigger_autocomplete(app.query_input) |
| 140 | + |
| 141 | + expected = {"id", "name", "email"} |
| 142 | + |
| 143 | + await wait_for_condition( |
| 144 | + pilot, |
| 145 | + lambda: bool(app._schema_cache.get("columns", {}).get("test_users")), |
| 146 | + timeout_seconds=10.0, |
| 147 | + description="autocomplete columns to load", |
| 148 | + ) |
| 149 | + |
| 150 | + text = app.query_input.text |
| 151 | + cursor_loc = app.query_input.cursor_location |
| 152 | + cursor_pos = app._location_to_offset(text, cursor_loc) |
| 153 | + suggestions = app._get_autocomplete_suggestions(text, cursor_pos) |
| 154 | + assert expected.issubset({item.lower() for item in suggestions}) |
| 155 | + assert "Loading..." not in suggestions |
0 commit comments