-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli.py
More file actions
386 lines (289 loc) · 12.3 KB
/
Copy pathtest_cli.py
File metadata and controls
386 lines (289 loc) · 12.3 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import json
from unittest.mock import patch
from test_croissant import CROISSANT_FIXTURE
from typer.testing import CliRunner
from otai import catalog, sql_guard
from otai.cli import app
runner = CliRunner()
SAMPLE_LISTING_XML = b"""<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CommonPrefixes><Prefix>platform/25.12/</Prefix></CommonPrefixes>
<CommonPrefixes><Prefix>platform/26.06/</Prefix></CommonPrefixes>
</ListBucketResult>
"""
def _invoke(args, cache_dir):
with patch(
"otai.releases.default_fetch_listing_xml", return_value=SAMPLE_LISTING_XML
):
return runner.invoke(app, args, env={"OTAI_CACHE_DIR": str(cache_dir)})
def _invoke_with_fixtures(args, cache_dir, base_uri):
with (
patch(
"otai.releases.default_fetch_listing_xml", return_value=SAMPLE_LISTING_XML
),
patch(
"otai.croissant.default_fetch_croissant",
return_value=json.dumps(CROISSANT_FIXTURE).encode(),
),
):
return runner.invoke(
app,
args,
env={"OTAI_CACHE_DIR": str(cache_dir), "OTAI_BASE_URI": base_uri},
)
def test_list_releases_json_output_default(tmp_path):
result = _invoke(["list-releases"], tmp_path / "cache")
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload["ok"] is True
releases = {r["release"]: r for r in payload["data"]["releases"]}
assert releases["26.06"]["latest"] is True
assert releases["25.12"]["latest"] is False
def test_list_releases_table_format(tmp_path):
result = _invoke(["list-releases", "--format", "table"], tmp_path / "cache")
assert result.exit_code == 0
assert "release" in result.stdout
assert "26.06" in result.stdout
assert "yes" in result.stdout
def test_list_releases_has_no_release_flag(tmp_path):
result = _invoke(["list-releases", "--release", "26.06"], tmp_path / "cache")
assert result.exit_code != 0
def test_list_releases_does_not_create_catalog_file(tmp_path):
# list-releases never builds a schema, so it must never take the
# catalog's write lock - it should simply report nothing cached rather
# than unconditionally creating the file.
cache_dir = tmp_path / "cache"
assert not (cache_dir / "catalog.duckdb").exists()
result = _invoke(["list-releases"], cache_dir)
assert result.exit_code == 0
assert not (cache_dir / "catalog.duckdb").exists()
def test_unknown_format_is_rejected(tmp_path):
result = _invoke(["list-releases", "--format", "yaml"], tmp_path / "cache")
assert result.exit_code != 0
def test_list_datasets_json_output_defaults_to_latest(tmp_path, fixture_release_layout):
base_uri, release, dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(["list-datasets"], tmp_path / "cache", base_uri)
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload["ok"] is True
assert payload["data"]["release"] == release
names = {d["dataset"] for d in payload["data"]["datasets"]}
assert names == set(dataset_rows)
def test_list_datasets_table_format(tmp_path, fixture_release_layout):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["list-datasets", "--format", "table"], tmp_path / "cache", base_uri
)
assert result.exit_code == 0
assert "dataset" in result.stdout
assert "description" in result.stdout
assert "target" in result.stdout
def test_list_datasets_accepts_explicit_release(tmp_path, fixture_release_layout):
base_uri, release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["list-datasets", "--release", release], tmp_path / "cache", base_uri
)
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload["data"]["release"] == release
def test_describe_dataset_json_output_defaults_to_latest(
tmp_path, fixture_release_layout
):
base_uri, release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["describe-dataset", "target"], tmp_path / "cache", base_uri
)
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload["ok"] is True
assert payload["data"]["release"] == release
assert payload["data"]["dataset"] == "target"
fields_by_name = {f["name"]: f for f in payload["data"]["fields"]}
assert fields_by_name["id"]["dataType"] == "sc:Text"
def test_describe_dataset_includes_references_and_subfields(
tmp_path, fixture_release_layout
):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["describe-dataset", "association_by_datasource_direct"],
tmp_path / "cache",
base_uri,
)
payload = json.loads(result.stdout)
fields_by_name = {f["name"]: f for f in payload["data"]["fields"]}
assert fields_by_name["targetId"]["references"] == {
"dataset": "target",
"field": "id",
}
result = _invoke_with_fixtures(
["describe-dataset", "target"], tmp_path / "cache", base_uri
)
payload = json.loads(result.stdout)
fields_by_name = {f["name"]: f for f in payload["data"]["fields"]}
sub_by_name = {sf["name"]: sf for sf in fields_by_name["proteinIds"]["subFields"]}
assert set(sub_by_name) == {"id", "source"}
def test_describe_dataset_table_format(tmp_path, fixture_release_layout):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["describe-dataset", "target", "--format", "table"],
tmp_path / "cache",
base_uri,
)
assert result.exit_code == 0
assert "name" in result.stdout
assert "dataType" in result.stdout
assert "id" in result.stdout
def test_describe_dataset_accepts_explicit_release(tmp_path, fixture_release_layout):
base_uri, release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["describe-dataset", "target", "--release", release],
tmp_path / "cache",
base_uri,
)
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload["data"]["release"] == release
def test_describe_dataset_unknown_dataset_returns_error(
tmp_path, fixture_release_layout
):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["describe-dataset", "no_such_dataset"], tmp_path / "cache", base_uri
)
assert result.exit_code != 0
payload = json.loads(result.stdout)
assert payload["ok"] is False
assert payload["error"]["type"] == "dataset_not_found"
def test_list_datasets_builds_catalog_schema_on_first_run(
tmp_path, fixture_release_layout
):
base_uri, release, _dataset_rows = fixture_release_layout
cache_dir = tmp_path / "cache"
_invoke_with_fixtures(["list-datasets"], cache_dir, base_uri)
import duckdb
conn = duckdb.connect(str(cache_dir / "catalog.duckdb"))
try:
schemas = {
row[0]
for row in conn.execute(
"SELECT schema_name FROM information_schema.schemata"
).fetchall()
}
assert release in schemas
finally:
conn.close()
def test_run_sql_json_output_against_latest(tmp_path, fixture_release_layout):
base_uri, release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["run-sql", "SELECT id, approvedSymbol FROM target ORDER BY id"],
tmp_path / "cache",
base_uri,
)
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload["ok"] is True
assert payload["data"]["release"] == release
assert payload["data"]["columns"] == ["id", "approvedSymbol"]
assert payload["data"]["rows"] == [
["ENSG00000141510", "TP53"],
["ENSG00000157764", "BRAF"],
]
def test_run_sql_table_format(tmp_path, fixture_release_layout):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
[
"run-sql",
"SELECT id, approvedSymbol FROM target ORDER BY id",
"--format",
"table",
],
tmp_path / "cache",
base_uri,
)
assert result.exit_code == 0
assert "approvedSymbol" in result.stdout
assert "BRAF" in result.stdout
def test_run_sql_has_no_release_flag(tmp_path, fixture_release_layout):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["run-sql", "SELECT 1", "--release", "26.06"], tmp_path / "cache", base_uri
)
assert result.exit_code != 0
def test_run_sql_rejects_non_select_statement(tmp_path, fixture_release_layout):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["run-sql", "DROP TABLE target"], tmp_path / "cache", base_uri
)
assert result.exit_code != 0
payload = json.loads(result.stdout)
assert payload["ok"] is False
assert payload["error"]["type"] == "guardrail_violation"
def test_run_sql_malformed_query_returns_sql_error(tmp_path, fixture_release_layout):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["run-sql", "SELECT * FROM target WHERE ("], tmp_path / "cache", base_uri
)
assert result.exit_code != 0
payload = json.loads(result.stdout)
assert payload["ok"] is False
assert payload["error"]["type"] == "sql_error"
def test_run_sql_rejects_non_positive_timeout(tmp_path, fixture_release_layout):
base_uri, _release, _dataset_rows = fixture_release_layout
result = _invoke_with_fixtures(
["run-sql", "SELECT 1", "--timeout", "0"], tmp_path / "cache", base_uri
)
# Match this file's convention for invalid-option tests (e.g.
# test_unknown_format_is_rejected): check the exit code only, not the
# styled error text - Typer/Click's box-drawn error panel wraps
# differently depending on the terminal width Rich detects, so exact
# substring matches against result.output are environment-fragile.
assert result.exit_code == 2
def test_run_sql_passes_custom_timeout_through_to_commands(
tmp_path, fixture_release_layout
):
base_uri, _release, _dataset_rows = fixture_release_layout
with patch("otai.cli.commands.run_sql") as mock_run_sql:
mock_run_sql.return_value = {"ok": True, "data": {"columns": [], "rows": []}}
_invoke_with_fixtures(
["run-sql", "SELECT 1", "--timeout", "120"], tmp_path / "cache", base_uri
)
assert mock_run_sql.call_args.kwargs["timeout_seconds"] == 120.0
def test_run_sql_uses_default_timeout_when_flag_omitted(
tmp_path, fixture_release_layout
):
base_uri, _release, _dataset_rows = fixture_release_layout
with patch("otai.cli.commands.run_sql") as mock_run_sql:
mock_run_sql.return_value = {"ok": True, "data": {"columns": [], "rows": []}}
_invoke_with_fixtures(["run-sql", "SELECT 1"], tmp_path / "cache", base_uri)
assert (
mock_run_sql.call_args.kwargs["timeout_seconds"]
== sql_guard.DEFAULT_TIMEOUT_SECONDS
)
def test_run_sql_timeout_flag_actually_shortens_execution(
tmp_path, fixture_release_layout
):
# Real end-to-end confirmation (not mocked): a view wrapping range()
# (created outside the guard, same pattern used in test_commands.py/
# test_sql_guard.py) is cheap to set up but genuinely slow to query -
# a short --timeout must cause it to time out for real.
base_uri, release, _dataset_rows = fixture_release_layout
cache_dir = tmp_path / "cache"
_invoke_with_fixtures(["list-datasets"], cache_dir, base_uri)
conn = catalog.connect_catalog(cache_dir)
try:
conn.execute(
f'CREATE VIEW "{release}".slow_a AS SELECT * FROM range(100000000)'
)
conn.execute(f'CREATE VIEW "{release}".slow_b AS SELECT * FROM range(100000)')
finally:
conn.close()
result = _invoke_with_fixtures(
["run-sql", "SELECT count(*) FROM slow_a a, slow_b b", "--timeout", "0.2"],
cache_dir,
base_uri,
)
assert result.exit_code != 0
payload = json.loads(result.stdout)
assert payload["ok"] is False
assert payload["error"]["type"] == "timeout"
assert "0.2" in payload["error"]["message"]