-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
418 lines (367 loc) · 15.4 KB
/
Copy pathtest_cli.py
File metadata and controls
418 lines (367 loc) · 15.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""Tests for evalwire.cli — Click commands."""
from pathlib import Path
from unittest.mock import MagicMock, patch
from click.testing import CliRunner
from evalwire.cli import main
def _runner() -> CliRunner:
return CliRunner()
def _mock_client() -> MagicMock:
client = MagicMock()
ds = MagicMock()
ds.id = "ds-1"
client.datasets.create_dataset.return_value = ds
client.datasets.get_dataset.return_value = ds
client.datasets.add_examples_to_dataset.return_value = ds
return client
class TestUploadCommand:
def test_missing_csv_exits_with_usage_error(self):
result = _runner().invoke(main, ["upload"])
assert result.exit_code != 0
assert "No CSV path provided" in result.output
def test_upload_with_csv_flag_succeeds(self, sample_csv: Path):
client = _mock_client()
# skip path: get_dataset succeeds → no upload needed, just returns existing
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["upload", "--csv", str(sample_csv)])
assert result.exit_code == 0
assert "Uploaded" in result.output
def test_upload_reports_correct_dataset_count(self, sample_csv: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["upload", "--csv", str(sample_csv)])
assert "2 dataset(s)" in result.output
def test_upload_on_exist_flag_passed_through(self, sample_csv: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
["upload", "--csv", str(sample_csv), "--on-exist", "overwrite"],
)
assert result.exit_code == 0
def test_upload_reads_csv_from_config_file(self, sample_csv: Path, tmp_path: Path):
toml = tmp_path / "evalwire.toml"
toml.write_text(f'[dataset]\ncsv_path = "{sample_csv}"\n')
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["upload", "--config", str(toml)])
assert result.exit_code == 0
def test_upload_cli_csv_overrides_config(self, sample_csv: Path, tmp_path: Path):
# Config points to a nonexistent file; CLI --csv should win
toml = tmp_path / "evalwire.toml"
toml.write_text('[dataset]\ncsv_path = "nonexistent.csv"\n')
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
["upload", "--csv", str(sample_csv), "--config", str(toml)],
)
assert result.exit_code == 0
def test_upload_custom_input_output_keys(self, tmp_path: Path):
csv_file = tmp_path / "custom.csv"
csv_file.write_text("q,ans,grp\nq1,a1,g1\n")
client = _mock_client()
client.datasets.get_dataset.side_effect = ValueError("not found")
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
[
"upload",
"--csv",
str(csv_file),
"--input-keys",
"q",
"--output-keys",
"ans",
"--tag-column",
"grp",
],
)
assert result.exit_code == 0
def test_upload_exits_2_on_unexpected_error(self, sample_csv: Path):
client = _mock_client()
# Both get_dataset (skip check) and create_dataset raise
client.datasets.get_dataset.side_effect = RuntimeError("also bad")
client.datasets.create_dataset.side_effect = RuntimeError("unexpected")
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["upload", "--csv", str(sample_csv)])
assert result.exit_code == 2
class TestRunCommand:
def test_run_all_experiments(self, experiments_dir: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main, ["run", "--experiments", str(experiments_dir)]
)
assert result.exit_code == 0
assert "2 experiment(s)" in result.output
def test_run_single_named_experiment(self, experiments_dir: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
[
"run",
"--experiments",
str(experiments_dir),
"--name",
"es_search",
],
)
assert result.exit_code == 0
assert client.experiments.run_experiment.call_count == 1
def test_run_dry_run_flag(self, experiments_dir: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
["run", "--experiments", str(experiments_dir), "--dry-run", "2"],
)
assert result.exit_code == 0
for c in client.experiments.run_experiment.call_args_list:
assert c.kwargs.get("dry_run") == 2
def test_run_custom_prefix(self, experiments_dir: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
[
"run",
"--experiments",
str(experiments_dir),
"--prefix",
"myci",
],
)
assert result.exit_code == 0
for c in client.experiments.run_experiment.call_args_list:
assert c.kwargs["experiment_name"].startswith("myci_")
def test_run_reads_experiments_dir_from_config(
self, experiments_dir: Path, tmp_path: Path
):
toml = tmp_path / "evalwire.toml"
toml.write_text(f'[experiments]\ndir = "{experiments_dir}"\n')
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["run", "--config", str(toml)])
assert result.exit_code == 0
def test_run_exits_1_when_experiment_fails(self, experiments_dir: Path):
client = _mock_client()
client.experiments.run_experiment.side_effect = RuntimeError("boom")
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main, ["run", "--experiments", str(experiments_dir)]
)
assert result.exit_code == 1
def test_run_exits_1_when_dataset_missing(self, experiments_dir: Path):
client = _mock_client()
client.datasets.get_dataset.side_effect = Exception("no dataset")
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main, ["run", "--experiments", str(experiments_dir)]
)
assert result.exit_code == 1
def test_run_concurrency_option(self, experiments_dir: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
[
"run",
"--experiments",
str(experiments_dir),
"--concurrency",
"4",
],
)
assert result.exit_code == 0
def test_help_text_available(self):
result = _runner().invoke(main, ["--help"])
assert result.exit_code == 0
result_upload = _runner().invoke(main, ["upload", "--help"])
assert result_upload.exit_code == 0
result_run = _runner().invoke(main, ["run", "--help"])
assert result_run.exit_code == 0
def _make_ran_experiment(experiment_id="exp-1", scores=None):
"""Return a mock RanExperiment-like dict."""
task_run = MagicMock()
task_run.id = "run-1"
task_run.output = "answer"
task_run.error = None
eval_runs = []
for name, score in (scores or {}).items():
ev = MagicMock()
ev.experiment_run_id = "run-1"
ev.name = name
ev.result = MagicMock()
ev.result.get = lambda k, default=None, _s=score: {"score": _s}.get(k, default)
ev.error = None
eval_runs.append(ev)
return {
"experiment_id": experiment_id,
"task_runs": [task_run],
"evaluation_runs": eval_runs,
"dataset_id": "ds-1",
"dataset_version_id": "dv-1",
"experiment_metadata": {},
"project_name": None,
}
class TestExportCommand:
def test_export_csv_exits_zero(self, tmp_path: Path):
ran = _make_ran_experiment(scores={"accuracy": 0.9})
client = _mock_client()
client.experiments.get_experiment.return_value = ran
out = tmp_path / "out.csv"
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
[
"export",
"--experiment",
"exp-1",
"--format",
"csv",
"--output",
str(out),
],
)
assert result.exit_code == 0
assert out.exists()
def test_export_json_exits_zero(self, tmp_path: Path):
ran = _make_ran_experiment(scores={"accuracy": 0.9})
client = _mock_client()
client.experiments.get_experiment.return_value = ran
out = tmp_path / "out.json"
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
[
"export",
"--experiment",
"exp-1",
"--format",
"json",
"--output",
str(out),
],
)
assert result.exit_code == 0
assert out.exists()
def test_export_missing_experiment_flag_exits_nonzero(self):
result = _runner().invoke(main, ["export"])
assert result.exit_code != 0
def test_export_not_found_experiment_exits_nonzero(self, tmp_path: Path):
client = _mock_client()
client.experiments.get_experiment.side_effect = ValueError("not found")
out = tmp_path / "out.csv"
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main,
["export", "--experiment", "missing", "--output", str(out)],
)
assert result.exit_code != 0
class TestCompareCommand:
def test_compare_exits_zero(self):
ran_a = _make_ran_experiment("exp-a", scores={"accuracy": 0.8})
ran_b = _make_ran_experiment("exp-b", scores={"accuracy": 0.9})
client = _mock_client()
client.experiments.get_experiment.side_effect = lambda experiment_id: (
ran_a if experiment_id == "exp-a" else ran_b
)
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["compare", "exp-a", "exp-b"])
assert result.exit_code == 0
assert "accuracy" in result.output
def test_compare_shows_delta(self):
ran_a = _make_ran_experiment("exp-a", scores={"accuracy": 0.8})
ran_b = _make_ran_experiment("exp-b", scores={"accuracy": 0.9})
client = _mock_client()
client.experiments.get_experiment.side_effect = lambda experiment_id: (
ran_a if experiment_id == "exp-a" else ran_b
)
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["compare", "exp-a", "exp-b"])
assert (
"0.10" in result.output
or "+0.1" in result.output
or "delta" in result.output.lower()
)
def test_compare_missing_args_exits_nonzero(self):
result = _runner().invoke(main, ["compare"])
assert result.exit_code != 0
class TestReportCommand:
def test_report_exits_zero(self):
ran = _make_ran_experiment(scores={"accuracy": 0.75})
client = _mock_client()
client.experiments.get_experiment.return_value = ran
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["report", "--experiment", "exp-1"])
assert result.exit_code == 0
assert "accuracy" in result.output
def test_report_missing_experiment_flag_exits_nonzero(self):
result = _runner().invoke(main, ["report"])
assert result.exit_code != 0
class TestValidateCommand:
def test_valid_csv_exits_zero(self, sample_csv: Path):
result = _runner().invoke(main, ["validate", "--csv", str(sample_csv)])
assert result.exit_code == 0
assert "valid" in result.output.lower()
def test_missing_csv_exits_nonzero(self):
result = _runner().invoke(main, ["validate"])
assert result.exit_code != 0
def test_invalid_csv_exits_nonzero(self, tmp_path: Path):
bad = tmp_path / "bad.csv"
bad.write_text("wrong_col\nval\n")
result = _runner().invoke(main, ["validate", "--csv", str(bad)])
assert result.exit_code != 0
assert "issue" in result.output.lower() or "missing" in result.output.lower()
def test_invalid_csv_reports_all_issues(self, tmp_path: Path):
bad = tmp_path / "bad.csv"
bad.write_text("wrong,also_wrong\nval,val\n")
result = _runner().invoke(
main,
[
"validate",
"--csv",
str(bad),
"--input-keys",
"user_query",
"--output-keys",
"expected_output",
],
)
assert result.exit_code != 0
assert "user_query" in result.output or "expected_output" in result.output
def test_custom_input_output_tag_columns(self, tmp_path: Path):
f = tmp_path / "custom.csv"
f.write_text("q,ans,grp\nhello,world,g1\n")
result = _runner().invoke(
main,
[
"validate",
"--csv",
str(f),
"--input-keys",
"q",
"--output-keys",
"ans",
"--tag-column",
"grp",
],
)
assert result.exit_code == 0
class TestUploadStrictFlag:
def test_upload_strict_passes_for_valid_csv(self, sample_csv: Path):
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(
main, ["upload", "--csv", str(sample_csv), "--strict"]
)
assert result.exit_code == 0
def test_upload_strict_fails_for_invalid_csv(self, tmp_path: Path):
bad = tmp_path / "bad.csv"
bad.write_text("wrong\nval\n")
client = _mock_client()
with patch("evalwire.cli._make_client", return_value=client):
result = _runner().invoke(main, ["upload", "--csv", str(bad), "--strict"])
assert result.exit_code != 0
assert client.datasets.create_dataset.call_count == 0