-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
231 lines (182 loc) · 8.04 KB
/
Copy pathtest_client.py
File metadata and controls
231 lines (182 loc) · 8.04 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
from __future__ import annotations
import os
from types import SimpleNamespace
from unittest.mock import patch
import pytest
from hotdata_runtime.env import normalize_host, pick_workspace, resolve_workspace_selection
from hotdata_runtime.client import HotdataClient
def _clear_workspace_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("HOTDATA_WORKSPACE", raising=False)
@pytest.mark.parametrize(
("raw", "expected"),
[
("https://api.hotdata.dev", "https://api.hotdata.dev"),
("https://api.hotdata.dev/", "https://api.hotdata.dev"),
("https://api.hotdata.dev/v1", "https://api.hotdata.dev"),
("https://api.hotdata.dev/v1/", "https://api.hotdata.dev"),
("http://localhost:8000/v1", "http://localhost:8000"),
("http://localhost:8000", "http://localhost:8000"),
],
)
def test_normalize_host(raw: str, expected: str):
assert normalize_host(raw) == expected
def test_pick_workspace_prefers_env(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setenv("HOTDATA_WORKSPACE", "ws_explicit")
assert pick_workspace("k", "https://api.hotdata.dev", None) == "ws_explicit"
def test_resolve_workspace_selection_prefers_env_without_listing(
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setenv("HOTDATA_WORKSPACE", "ws_explicit")
with patch("hotdata_runtime.env.list_workspaces") as listing:
resolved = resolve_workspace_selection(
"k", "https://api.hotdata.dev", None
)
listing.assert_not_called()
assert resolved.workspace_id == "ws_explicit"
assert resolved.source == "explicit_env"
assert resolved.workspaces == []
def test_pick_workspace_chooses_first_active(monkeypatch: pytest.MonkeyPatch):
_clear_workspace_env(monkeypatch)
items = [
SimpleNamespace(public_id="ws_1", active=False),
SimpleNamespace(public_id="ws_2", active=True),
SimpleNamespace(public_id="ws_3", active=True),
]
listing = SimpleNamespace(workspaces=items)
with patch("hotdata_runtime.env.WorkspacesApi") as Api:
Api.return_value.list_workspaces.return_value = listing
assert pick_workspace("k", "https://api.hotdata.dev", None) == "ws_2"
def test_pick_workspace_falls_back_to_first(monkeypatch: pytest.MonkeyPatch):
_clear_workspace_env(monkeypatch)
items = [
SimpleNamespace(public_id="ws_1", active=False),
SimpleNamespace(public_id="ws_2", active=False),
]
listing = SimpleNamespace(workspaces=items)
with patch("hotdata_runtime.env.WorkspacesApi") as Api:
Api.return_value.list_workspaces.return_value = listing
assert pick_workspace("k", "https://api.hotdata.dev", None) == "ws_1"
def test_resolve_workspace_selection_source_first(monkeypatch: pytest.MonkeyPatch):
_clear_workspace_env(monkeypatch)
items = [
SimpleNamespace(public_id="ws_1", active=False),
SimpleNamespace(public_id="ws_2", active=False),
]
listing = SimpleNamespace(workspaces=items)
with patch("hotdata_runtime.env.WorkspacesApi") as Api:
Api.return_value.list_workspaces.return_value = listing
resolved = resolve_workspace_selection(
"k", "https://api.hotdata.dev", None
)
assert resolved.workspace_id == "ws_1"
assert resolved.source == "first"
assert resolved.workspaces == items
def test_resolve_workspace_selection_returns_workspaces_and_source(
monkeypatch: pytest.MonkeyPatch,
):
_clear_workspace_env(monkeypatch)
items = [
SimpleNamespace(public_id="ws_1", active=False),
SimpleNamespace(public_id="ws_2", active=True),
]
listing = SimpleNamespace(workspaces=items)
with patch("hotdata_runtime.env.WorkspacesApi") as Api:
Api.return_value.list_workspaces.return_value = listing
resolved = resolve_workspace_selection(
"k", "https://api.hotdata.dev", None
)
assert resolved.workspace_id == "ws_2"
assert resolved.source == "active"
assert resolved.workspaces == items
def test_list_qualified_table_names_passes_connection_id():
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
with patch.object(client, "iter_tables", return_value=iter([])) as it:
client.list_qualified_table_names(limit=5, connection_id="conn_a")
it.assert_called_once()
assert it.call_args.kwargs["connection_id"] == "conn_a"
def test_wait_result_ready_raises_on_cancelled():
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
class FakeResultsApi:
def get_result(self, result_id: str):
return SimpleNamespace(status="cancelled", error_message=None)
with patch.object(client, "_results_api", return_value=FakeResultsApi()):
with pytest.raises(RuntimeError, match="cancelled"):
client._wait_result_ready("res_1", timeout_s=0.1, interval_s=0)
def test_connection_id_by_name_raises_on_duplicate_names():
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
listing = SimpleNamespace(
connections=[
SimpleNamespace(name="warehouse", id="conn_1"),
SimpleNamespace(name="warehouse", id="conn_2"),
]
)
class FakeConnectionsApi:
def list_connections(self):
return listing
with patch.object(client, "connections", return_value=FakeConnectionsApi()):
with pytest.raises(RuntimeError, match="Duplicate connection names"):
client.connection_id_by_name()
def test_columns_for_qualified_prefers_explicit_connection_id():
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
col = SimpleNamespace(name="a", data_type="INTEGER", nullable=True)
table = SimpleNamespace(columns=[col])
response = SimpleNamespace(tables=[table])
class FakeInformationSchemaApi:
def __init__(self):
self.kwargs = None
def information_schema(self, **kwargs):
self.kwargs = kwargs
return response
fake_api = FakeInformationSchemaApi()
with patch.object(client, "_information_schema", return_value=fake_api), patch.object(
client, "connection_id_by_name"
) as id_map:
cols = client.columns_for_qualified(
"warehouse.public.orders",
connection_id="conn_explicit",
)
id_map.assert_not_called()
assert cols == [col]
assert fake_api.kwargs["connection_id"] == "conn_explicit"
def test_list_recent_results_returns_normalized_summaries():
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
listing = SimpleNamespace(
results=[
SimpleNamespace(id="res_1", status="ready", created_at="2026-01-01T00:00:00Z"),
SimpleNamespace(id="res_2", status="failed", created_at=None),
]
)
class FakeResultsApi:
def list_results(self, *, limit: int, offset: int):
return listing
with patch.object(client, "results", return_value=FakeResultsApi()):
out = client.list_recent_results(limit=10, offset=2)
assert [r.result_id for r in out] == ["res_1", "res_2"]
assert out[0].status == "ready"
assert out[0].to_dict()["created_at"] == "2026-01-01T00:00:00Z"
def test_list_run_history_returns_normalized_items():
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
listing = SimpleNamespace(
query_runs=[
SimpleNamespace(
id="run_1",
status="succeeded",
created_at="2026-01-01T00:00:00Z",
execution_time_ms=7,
result_id="res_1",
),
]
)
class FakeRunsApi:
def __init__(self):
self.kwargs = None
def list_query_runs(self, *, limit: int):
self.kwargs = {"limit": limit}
return listing
fake_runs = FakeRunsApi()
with patch.object(client, "query_runs", return_value=fake_runs):
out = client.list_run_history(limit=5)
assert [r.query_run_id for r in out] == ["run_1"]
assert out[0].execution_time_ms == 7
assert out[0].to_dict()["result_id"] == "res_1"
assert fake_runs.kwargs == {"limit": 5}