|
| 1 | +"""Regression tests for MySQL autocomplete in multi-database scenarios (#151). |
| 2 | +
|
| 3 | +Two narrow bugs are covered: |
| 4 | +
|
| 5 | +1. Qualified identifiers for databases without schemas (MySQL/MariaDB) used |
| 6 | + to render as `db`.``.`table` — an empty-backticked middle segment. The |
| 7 | + qualifying logic is now a Dialect method so each adapter owns its own |
| 8 | + composition rule. |
| 9 | +
|
| 10 | +2. Autocomplete returned a permanent "Loading..." sentinel whenever the |
| 11 | + table reference in the query didn't resolve to anything in the schema |
| 12 | + cache (e.g. the user typed `SELECT * FROM shop.cu`: `shop` was treated |
| 13 | + as an alias and the loader spun forever for an unknown key). |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | + |
| 21 | +# -------------------------------------------------------------------------- |
| 22 | +# Bug 1: Dialect.qualified_name |
| 23 | +# -------------------------------------------------------------------------- |
| 24 | + |
| 25 | + |
| 26 | +def _get_dialect(db_type: str): |
| 27 | + from sqlit.domains.connections.providers.catalog import get_provider |
| 28 | + |
| 29 | + return get_provider(db_type).dialect |
| 30 | + |
| 31 | + |
| 32 | +def test_mysql_qualified_name_is_two_part() -> None: |
| 33 | + """MySQL has no schema-within-database; the qualified form is |
| 34 | + `db`.`table`, NOT `db`.``.`table`.""" |
| 35 | + dialect = _get_dialect("mysql") |
| 36 | + assert dialect.qualified_name("shop", "", "customers") == "`shop`.`customers`" |
| 37 | + |
| 38 | + |
| 39 | +def test_mariadb_qualified_name_is_two_part() -> None: |
| 40 | + dialect = _get_dialect("mariadb") |
| 41 | + assert dialect.qualified_name("shop", "", "customers") == "`shop`.`customers`" |
| 42 | + |
| 43 | + |
| 44 | +def test_postgresql_qualified_name_uses_schema_only() -> None: |
| 45 | + """PostgreSQL databases are isolated; only schema.table makes sense |
| 46 | + for cross-reference within the connected database.""" |
| 47 | + dialect = _get_dialect("postgresql") |
| 48 | + # No db segment expected when schema is present. |
| 49 | + assert dialect.qualified_name(None, "public", "users") == '"public"."users"' |
| 50 | + |
| 51 | + |
| 52 | +def test_sqlserver_qualified_name_is_three_part() -> None: |
| 53 | + """SQL Server explicitly uses [db].[schema].[table].""" |
| 54 | + dialect = _get_dialect("mssql") |
| 55 | + assert dialect.qualified_name("app", "dbo", "Users") == "[app].[dbo].[Users]" |
| 56 | + |
| 57 | + |
| 58 | +def test_qualified_name_skips_empty_segments_everywhere() -> None: |
| 59 | + """Regardless of dialect, empty db/schema segments must be omitted, |
| 60 | + never rendered as empty-quoted placeholders.""" |
| 61 | + for db_type in ("mysql", "postgresql", "mssql", "sqlite"): |
| 62 | + dialect = _get_dialect(db_type) |
| 63 | + # bare table name: single quoted segment, no dot joins. |
| 64 | + bare = dialect.qualified_name(None, None, "t") |
| 65 | + assert "t" in bare, f"{db_type}: {bare}" |
| 66 | + assert "." not in bare, f"{db_type} unexpected joins: {bare}" |
| 67 | + # empty schema segment must not produce empty quote pair like `` or "" or []. |
| 68 | + out = dialect.qualified_name(None, "", "t") |
| 69 | + for marker in ("``", '""', "[]"): |
| 70 | + assert marker not in out, f"{db_type} emitted empty-quoted segment: {out}" |
| 71 | + |
| 72 | + |
| 73 | +def test_qualified_name_escapes_embedded_quote_chars() -> None: |
| 74 | + """Each dialect must escape its own quote char, not leak raw input.""" |
| 75 | + for db_type, payload, expected_substr in [ |
| 76 | + ("mysql", "app`evil", "app``evil"), |
| 77 | + ("postgresql", 'app"evil', 'app""evil'), |
| 78 | + ("mssql", "app]evil", "app]]evil"), |
| 79 | + ]: |
| 80 | + dialect = _get_dialect(db_type) |
| 81 | + out = dialect.qualified_name(None, None, payload) |
| 82 | + assert expected_substr in out, f"{db_type}: {out}" |
| 83 | + |
| 84 | + |
| 85 | +# -------------------------------------------------------------------------- |
| 86 | +# Bug 2: stuck Loading... for unknown table references |
| 87 | +# -------------------------------------------------------------------------- |
| 88 | + |
| 89 | + |
| 90 | +class _SchemaHost: |
| 91 | + """Minimal stand-in for AutocompleteMixinHost — just enough to drive |
| 92 | + `_get_autocomplete_suggestions`.""" |
| 93 | + |
| 94 | + def __init__(self, tables, metadata, columns=None, loading=None): |
| 95 | + self._schema_cache = { |
| 96 | + "tables": tables, |
| 97 | + "views": [], |
| 98 | + "columns": columns or {}, |
| 99 | + "procedures": [], |
| 100 | + } |
| 101 | + self._table_metadata = metadata |
| 102 | + self._columns_loading = loading or set() |
| 103 | + self.load_calls: list[str] = [] |
| 104 | + |
| 105 | + def _load_columns_for_table(self, table_name: str) -> None: |
| 106 | + self.load_calls.append(table_name) |
| 107 | + |
| 108 | + def _build_alias_map(self, text: str) -> dict: |
| 109 | + return {} |
| 110 | + |
| 111 | + |
| 112 | +def test_unknown_table_ref_does_not_stick_on_loading() -> None: |
| 113 | + """`SELECT * FROM shop.cu` parses `shop` as an ALIAS_COLUMN scope. It |
| 114 | + isn't a real table. Before the fix the completion engine called |
| 115 | + _load_columns_for_table('shop') and returned `Loading...`; the loader |
| 116 | + skipped unknown keys silently, so the sentinel never cleared.""" |
| 117 | + from sqlit.domains.query.ui.mixins.autocomplete_suggestions import AutocompleteSuggestionsMixin |
| 118 | + |
| 119 | + host = _SchemaHost( |
| 120 | + tables=["customers", "orders", "products"], |
| 121 | + metadata={ |
| 122 | + "customers": ("", "customers", "shop"), |
| 123 | + "shop.customers": ("", "customers", "shop"), |
| 124 | + "orders": ("", "orders", "shop"), |
| 125 | + "products": ("", "products", "shop"), |
| 126 | + }, |
| 127 | + ) |
| 128 | + |
| 129 | + get_suggestions = AutocompleteSuggestionsMixin._get_autocomplete_suggestions.__get__(host) |
| 130 | + |
| 131 | + text = "SELECT * FROM shop.cu" |
| 132 | + result = get_suggestions(text, len(text)) |
| 133 | + |
| 134 | + assert result != ["Loading..."], ( |
| 135 | + "unknown table key must not pin Loading... — loader never clears it" |
| 136 | + ) |
| 137 | + assert "shop" not in host.load_calls |
| 138 | + |
| 139 | + |
| 140 | +def test_known_table_ref_still_triggers_loading_on_first_call() -> None: |
| 141 | + """Sanity check: the fix must not regress legit lazy loading.""" |
| 142 | + from sqlit.domains.query.ui.mixins.autocomplete_suggestions import AutocompleteSuggestionsMixin |
| 143 | + |
| 144 | + host = _SchemaHost( |
| 145 | + tables=["customers"], |
| 146 | + metadata={"customers": ("", "customers", None)}, |
| 147 | + columns={}, |
| 148 | + ) |
| 149 | + get_suggestions = AutocompleteSuggestionsMixin._get_autocomplete_suggestions.__get__(host) |
| 150 | + |
| 151 | + text = "SELECT * FROM customers WHERE em" |
| 152 | + result = get_suggestions(text, len(text)) |
| 153 | + |
| 154 | + assert result == ["Loading..."] |
| 155 | + assert "customers" in host.load_calls |
0 commit comments