Repository navigation
Expand file tree
/
Copy pathtest_setup_wizard.py
More file actions
342 lines (269 loc) · 11.8 KB
/
Copy pathtest_setup_wizard.py
File metadata and controls
342 lines (269 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""/setup wizard: pure DSN assembly + register-and-test + per-scope routing.
The Discord UI layer (setup_wizard.py modal/select) is exercised only by an
import-smoke; its async on_submit eventually calls
``CommandHandlers.register_db_for_guild``, which is what we cover end-to-end
against a real sqlite database.
"""
from __future__ import annotations
import asyncio
import pytest
from sqlalchemy.exc import NoSuchModuleError
from lang2sql.adapters.db import D1Explorer, SqlAlchemyExplorer
from lang2sql.adapters.db.dsn_builder import assemble
from lang2sql.core.identity import Identity
from lang2sql.frontends.discord.commands import CommandHandlers
from lang2sql.semantic.catalog import SemanticCatalog
from lang2sql.tenancy.concierge import ContextConcierge
# --- dsn_builder ---------------------------------------------------------
def test_assemble_postgres_url():
spec = assemble(
"postgresql",
{
"host": "db.example.com",
"port": "5432",
"database": "analytics",
"user": "u",
"password": "p",
},
)
assert spec.dsn == "postgresql+psycopg://u:p@db.example.com:5432/analytics"
assert spec.extras == {}
def test_assemble_url_encodes_special_chars_in_password():
spec = assemble(
"postgresql",
{
"host": "h",
"port": "5432",
"database": "d",
"user": "u",
"password": "p@ss/w:rd",
},
)
assert "p%40ss%2Fw%3Ard" in spec.dsn # @, /, : all encoded
def test_assemble_snowflake_attaches_warehouse():
spec = assemble(
"snowflake",
{
"account": "ab12345.us-east-1",
"user": "u",
"password": "p",
"database": "DB",
"warehouse": "WH",
},
)
assert "warehouse=WH" in spec.dsn and "@ab12345.us-east-1/DB" in spec.dsn
def test_assemble_d1_returns_token_in_extras():
spec = assemble(
"d1",
{
"account_id": "acct",
"database_id": "db",
"api_token": "secret",
},
)
assert spec.dsn == "d1://acct/db"
assert spec.extras == {"d1_token": "secret"}
def test_assemble_sqlite_path():
spec = assemble("sqlite", {"path": "/tmp/demo.db"})
assert spec.dsn == "sqlite:////tmp/demo.db"
assert spec.extras == {}
def test_assemble_missing_required_field_raises():
with pytest.raises(ValueError, match="missing required"):
assemble("postgresql", {"host": "h"}) # no user/password/db
def test_assemble_unknown_db_type_raises():
with pytest.raises(ValueError, match="unsupported"):
assemble("unsupported-db", {})
# --- register_db_for_guild end-to-end (real sqlite) ----------------------
def _seed_sqlite(path: str) -> None:
from sqlalchemy import create_engine, text
eng = create_engine(f"sqlite:///{path}")
with eng.begin() as conn:
conn.execute(text("CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT)"))
conn.execute(text("INSERT INTO products VALUES (1, 'a'), (2, 'b')"))
def test_register_db_for_guild_success_stores_encrypted(tmp_path):
db = tmp_path / "demo.db"
_seed_sqlite(str(db))
concierge = ContextConcierge()
handlers = CommandHandlers(concierge)
identity = Identity(user_id="alice", guild_id="g1", channel_id="c", is_admin=True)
result = asyncio.run(
handlers.register_db_for_guild(identity, "sqlite", {"path": str(db)})
)
assert "연결 완료" in result.text
assert asyncio.run(concierge.secrets.get("g1", "db_dsn")) == f"sqlite:///{db}"
ctx = asyncio.run(concierge.build_context(identity))
assert isinstance(ctx.explorer, SqlAlchemyExplorer)
tables = asyncio.run(ctx.explorer.list_tables())
assert "products" in {t.name for t in tables}
def test_register_db_for_guild_unknown_driver_gives_friendly_error():
concierge = ContextConcierge()
handlers = CommandHandlers(concierge)
identity = Identity(user_id="u", guild_id="g-x", channel_id="c", is_admin=True)
# Snowflake driver isn't installed in this env; the handler should catch
# ModuleNotFoundError and produce a clear, non-technical message.
res = asyncio.run(
handlers.register_db_for_guild(
identity,
"snowflake",
{
"account": "a",
"user": "u",
"password": "p",
"database": "d",
"warehouse": "w",
},
)
)
assert "uv sync --extra snowflake" in res.text or "Couldn't connect" in res.text
def test_register_missing_duckdb_dialect_gives_install_command(
monkeypatch, tmp_path
) -> None:
concierge = ContextConcierge()
handlers = CommandHandlers(concierge)
identity = Identity(user_id="u", guild_id="g", channel_id="c", is_admin=True)
def missing_driver(*_args, **_kwargs):
raise NoSuchModuleError("Can't load plugin: sqlalchemy.dialects:duckdb")
monkeypatch.setattr(
"lang2sql.frontends.discord.commands.build_explorer", missing_driver
)
result = asyncio.run(
handlers.register_db_for_guild(
identity, "duckdb", {"path": str(tmp_path / "warehouse.duckdb")}
)
)
assert "uv sync --extra duckdb" in result.text
assert "파일 절대경로" not in result.text
def test_register_db_for_guild_missing_field_reports_setup_error():
concierge = ContextConcierge()
handlers = CommandHandlers(concierge)
identity = Identity(user_id="u", guild_id="g", channel_id="c", is_admin=True)
res = asyncio.run(
handlers.register_db_for_guild(
identity,
"postgresql",
{"host": "h"}, # missing user/password/db
)
)
assert "Setup error" in res.text and "missing required" in res.text
def test_register_missing_sqlite_is_file_specific_and_does_not_create(tmp_path):
missing = tmp_path / "missing.db"
concierge = ContextConcierge()
handlers = CommandHandlers(concierge)
identity = Identity(user_id="u", guild_id="g", channel_id="c", is_admin=True)
result = asyncio.run(
handlers.register_db_for_guild(identity, "sqlite", {"path": str(missing)})
)
assert "파일 절대경로" in result.text
assert "읽기 권한" in result.text
assert not missing.exists()
# --- concierge per-scope explorer routing --------------------------------
def test_concierge_per_scope_dsn_routes_correctly(tmp_path):
db = tmp_path / "scoped.db"
_seed_sqlite(str(db))
concierge = ContextConcierge()
g_with = Identity(user_id="u", guild_id="g-real", channel_id="c", is_admin=True)
g_without = Identity(user_id="u", guild_id="g-default", channel_id="c")
result = asyncio.run(CommandHandlers(concierge).connect(g_with, f"sqlite:///{db}"))
assert "연결 완료" in result.text
ctx_with = asyncio.run(concierge.build_context(g_with))
ctx_without = asyncio.run(concierge.build_context(g_without))
assert isinstance(ctx_with.explorer, SqlAlchemyExplorer)
# The guild without a stored DSN falls back to the concierge default
# (PostgresExplorer stub in this offline env).
assert ctx_with.explorer is not ctx_without.explorer
def test_concierge_d1_extras_threaded_through_secrets():
concierge = ContextConcierge()
concierge.activate_connection(
scope="g-d1",
dsn="d1://acct/db",
extras={"d1_token": "tok-1"},
catalog=SemanticCatalog(fingerprint="fixture"),
expected_generation=0,
)
identity = Identity(user_id="u", guild_id="g-d1", channel_id="c")
ctx = asyncio.run(concierge.build_context(identity))
assert isinstance(ctx.explorer, D1Explorer)
assert ctx.explorer._token == "tok-1"
def test_reactivation_rotates_generation_and_explorer_cache(tmp_path):
db1 = tmp_path / "a.db"
db2 = tmp_path / "b.db"
_seed_sqlite(str(db1))
_seed_sqlite(str(db2))
concierge = ContextConcierge()
identity = Identity(user_id="u", guild_id="g", channel_id="c", is_admin=True)
handlers = CommandHandlers(concierge)
first = asyncio.run(handlers.connect(identity, f"sqlite:///{db1}"))
assert "연결 완료" in first.text
ctx1 = asyncio.run(concierge.build_context(identity))
binding1 = concierge.connection_binding("g")
second = asyncio.run(handlers.connect(identity, f"sqlite:///{db2}"))
assert "연결 완료" in second.text
ctx_fresh = asyncio.run(concierge.build_context(identity))
binding2 = concierge.connection_binding("g")
assert ctx_fresh.explorer is not ctx1.explorer
assert binding1 is not None and binding2 is not None
assert binding2.generation == binding1.generation + 1
# --- UI module import smoke ----------------------------------------------
def test_setup_wizard_module_imports_without_discord_runtime():
# The wizard imports discord.ui at module level. Make sure that succeeds in
# a no-gateway environment — the same contract as bot.py's import-safety.
import lang2sql.frontends.discord.setup_wizard # noqa: F401
def test_setup_picker_has_a_label_for_every_supported_database():
from lang2sql.adapters.db.dsn_builder import SUPPORTED_DB_TYPES
from lang2sql.frontends.discord.setup_wizard import _LABELS
assert set(SUPPORTED_DB_TYPES) <= set(_LABELS)
def test_discord_review_storage_failure_returns_retryable_message(monkeypatch):
concierge = ContextConcierge()
handlers = CommandHandlers(concierge)
identity = Identity(user_id="u", guild_id="g", channel_id="c", is_admin=True)
def fail_review(*_args, **_kwargs):
raise RuntimeError("forced review storage failure")
monkeypatch.setattr(concierge.semantic, "confirm_pending", fail_review)
result = asyncio.run(handlers.semantic_review(identity, "sum"))
assert "BLOCKED" in result.text
assert "다시 시도" in result.text
@pytest.mark.asyncio
async def test_pre_catalog_credentials_migrate_once_across_restart(tmp_path):
db = tmp_path / "source.sqlite"
_seed_sqlite(str(db))
metadata = str(tmp_path / "metadata.sqlite")
identity = Identity(user_id="analyst", guild_id="finance", channel_id="team")
dsn = f"sqlite:///{db}"
old = ContextConcierge(path=metadata)
await old.secrets.set("finance", "db_dsn", dsn)
old.store.close()
for _ in range(2):
concierge = ContextConcierge(path=metadata)
try:
ctx = await concierge.build_context(identity)
assert ctx.connection_generation == 1
assert ctx.source_id == concierge.source_identity("finance", dsn, {})
assert isinstance(ctx.explorer, SqlAlchemyExplorer)
assert [t.name for t in await ctx.explorer.list_tables()] == ["products"]
assert "run_sql" not in {tool.name for tool in ctx.tools.specs()}
assert concierge.connection_binding("other") is None
finally:
concierge.close()
concierge.store.close()
@pytest.mark.asyncio
async def test_legacy_migration_cannot_overwrite_changed_credentials(
tmp_path, monkeypatch
):
db = tmp_path / "old.sqlite"
_seed_sqlite(str(db))
concierge = ContextConcierge()
scope = "finance"
new_dsn = "d1://account/replacement"
await concierge.secrets.set(scope, "db_dsn", f"sqlite:///{db}")
from lang2sql.semantic.onboarding import build_catalog
async def replace_during_scan(*args):
summary = await build_catalog(*args)
await concierge.secrets.set(scope, "db_dsn", new_dsn)
return summary
monkeypatch.setattr("lang2sql.tenancy.concierge.build_catalog", replace_during_scan)
ctx = await concierge.build_context(Identity("analyst", scope, "team"))
assert await concierge.secrets.get(scope, "db_dsn") == new_dsn
assert concierge.connection_binding(scope) is None
assert "run_sql" not in {tool.name for tool in ctx.tools.specs()}
concierge.close()
concierge.store.close()