Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
203 lines (168 loc) · 5.31 KB
/
Copy pathtest_cli.py
File metadata and controls
203 lines (168 loc) · 5.31 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
from pathlib import Path
from typer.testing import CliRunner
from sqlrooms.cli import (
DEFAULT_CONFIG_PATH,
_load_ai_runtime_config,
_load_connector_config,
_normalize_config_string,
_resolve_config_path,
app,
)
from sqlrooms.web.db_bridge import PostgresConnectorSettings, SnowflakeConnectorSettings
runner = CliRunner()
def test_cli_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Start the SQLRooms local experience" in result.stdout
def test_cli_export_help():
result = runner.invoke(app, ["export", "--help"])
assert result.exit_code == 0
assert "Usage" in result.stdout
assert "export" in result.stdout
def test_load_connector_config_toml(tmp_path):
config_path = tmp_path / "config.toml"
config_path.write_text(
"""
[[db.connectors]]
id = "pg-local"
engine = "postgres"
host = "localhost"
port = "5432"
database = "db"
user = "u"
password = "p"
title = "Local Postgres"
[[db.connectors]]
id = "sf-local"
engine = "snowflake"
account = "demo-account"
user = "demo-user"
warehouse = "DEMO_WH"
title = "Local Snowflake"
""".strip(),
encoding="utf-8",
)
data = _load_connector_config(config_path)
assert isinstance(data[0], PostgresConnectorSettings)
assert data[0].host == "localhost"
assert data[0].port == "5432"
assert data[0].database == "db"
assert data[0].user == "u"
assert data[0].password == "p"
assert data[0].connection_id == "pg-local"
assert data[0].title == "Local Postgres"
assert isinstance(data[1], SnowflakeConnectorSettings)
assert data[1].account == "demo-account"
assert data[1].user == "demo-user"
assert data[1].warehouse == "DEMO_WH"
assert data[1].connection_id == "sf-local"
assert data[1].title == "Local Snowflake"
def test_load_ai_runtime_config_toml(tmp_path, monkeypatch):
monkeypatch.setenv("OPENAI_API_KEY", "env-openai-key")
config_path = tmp_path / "config.toml"
config_path.write_text(
"""
[ai]
default_provider = "openai"
default_model = "gpt-5"
[[ai.providers]]
id = "openai"
base_url = "https://api.openai.com/v1"
api_key_env = "OPENAI_API_KEY"
models = ["gpt-5", "gpt-4.1"]
[[ai.providers]]
id = "anthropic"
base_url = "https://api.anthropic.com"
api_key = "anthropic-key"
models = ["claude-4-sonnet"]
""".strip(),
encoding="utf-8",
)
default_provider, default_model, providers = _load_ai_runtime_config(config_path)
assert default_provider == "openai"
assert default_model == "gpt-5"
assert providers["openai"]["apiKey"] == "env-openai-key"
assert providers["openai"]["models"][0]["modelName"] == "gpt-5"
assert providers["anthropic"]["apiKey"] == "anthropic-key"
def test_load_connector_config_rejects_duplicate_ids(tmp_path):
config_path = tmp_path / "config.toml"
config_path.write_text(
"""
[[db.connectors]]
id = "dup"
engine = "postgres"
host = "localhost"
database = "db"
user = "u"
[[db.connectors]]
id = "dup"
engine = "snowflake"
account = "demo-account"
user = "demo-user"
""".strip(),
encoding="utf-8",
)
try:
_load_connector_config(config_path)
except RuntimeError as exc:
assert "Duplicate connector id" in str(exc)
else:
raise AssertionError("Expected duplicate connector id failure")
def test_load_ai_runtime_config_rejects_unknown_default_provider(tmp_path):
config_path = tmp_path / "config.toml"
config_path.write_text(
"""
[ai]
default_provider = "missing"
[[ai.providers]]
id = "openai"
base_url = "https://api.openai.com/v1"
models = ["gpt-5"]
""".strip(),
encoding="utf-8",
)
try:
_load_ai_runtime_config(config_path)
except RuntimeError as exc:
assert "default_provider" in str(exc)
else:
raise AssertionError("Expected invalid default provider failure")
def test_resolve_config_path_prefers_explicit(tmp_path):
explicit = tmp_path / "explicit.toml"
explicit.write_text("[db.connectors]\n", encoding="utf-8")
resolved = _resolve_config_path(str(explicit), no_config=False)
assert resolved == explicit
def test_resolve_config_path_honors_no_config():
assert _resolve_config_path(None, no_config=True) is None
def test_default_config_path():
import sys
if sys.platform.startswith("win"):
import os
expected = Path(os.environ.get("APPDATA", "")) / "sqlrooms" / "config.toml"
else:
expected = Path.home() / ".config" / "sqlrooms" / "config.toml"
assert DEFAULT_CONFIG_PATH == expected
def test_normalize_config_string_coerces_int():
assert _normalize_config_string(5432) == "5432"
assert _normalize_config_string(0) == "0"
assert _normalize_config_string(3.14) == "3.14"
def test_load_connector_config_int_port(tmp_path):
"""tomllib parses bare ``port = 6543`` as int; ensure it's preserved."""
config_path = tmp_path / "config.toml"
config_path.write_text(
"""
[[db.connectors]]
id = "pg"
engine = "postgres"
host = "db.example.com"
port = 6543
database = "mydb"
user = "admin"
""".strip(),
encoding="utf-8",
)
data = _load_connector_config(config_path)
assert data[0].port == "6543"
# Since the main function in cli.py starts an asyncio loop and a server,
# unit testing it without mocks is hard. We'll skip deep integration tests
# of the full server startup here.