|
1 | 1 | """Tests for pipeline orchestrator metadata and helpers.""" |
2 | 2 |
|
3 | 3 | import json |
| 4 | +import sys |
4 | 5 | import time |
| 6 | +from types import ModuleType |
5 | 7 | from unittest.mock import MagicMock, patch |
6 | 8 |
|
7 | 9 | import pytest |
|
10 | 12 |
|
11 | 13 | from modal_app.pipeline import ( |
12 | 14 | RunMetadata, |
| 15 | + _build_diagnostics_upload_script, |
13 | 16 | _step_completed, |
14 | 17 | _record_step, |
15 | 18 | generate_run_id, |
@@ -259,3 +262,43 @@ def test_read_nonexistent_raises(self): |
259 | 262 | ): |
260 | 263 | with pytest.raises(FileNotFoundError): |
261 | 264 | read_run_meta("fake_run", mock_vol) |
| 265 | + |
| 266 | + |
| 267 | +def test_diagnostics_upload_script_is_valid_python(monkeypatch, capsys): |
| 268 | + entries = [ |
| 269 | + ( |
| 270 | + "/pipeline/runs/test/diagnostics/unified_diagnostics.csv", |
| 271 | + "calibration/runs/test/diagnostics/unified_diagnostics.csv", |
| 272 | + ) |
| 273 | + ] |
| 274 | + entries_json = json.dumps(entries) |
| 275 | + |
| 276 | + script = _build_diagnostics_upload_script(entries_json) |
| 277 | + |
| 278 | + compile(script, "<diagnostics-upload>", "exec") |
| 279 | + assert "\t" not in script |
| 280 | + assert "api.upload_file(" in script |
| 281 | + |
| 282 | + calls = [] |
| 283 | + |
| 284 | + class FakeHfApi: |
| 285 | + def upload_file(self, **kwargs): |
| 286 | + calls.append(kwargs) |
| 287 | + |
| 288 | + fake_hub = ModuleType("huggingface_hub") |
| 289 | + fake_hub.HfApi = FakeHfApi |
| 290 | + monkeypatch.setitem(sys.modules, "huggingface_hub", fake_hub) |
| 291 | + monkeypatch.setenv("HUGGING_FACE_TOKEN", "token") |
| 292 | + |
| 293 | + exec(compile(script, "<diagnostics-upload>", "exec"), {}) |
| 294 | + |
| 295 | + assert calls == [ |
| 296 | + { |
| 297 | + "path_or_fileobj": entries[0][0], |
| 298 | + "path_in_repo": entries[0][1], |
| 299 | + "repo_id": "policyengine/policyengine-us-data", |
| 300 | + "repo_type": "model", |
| 301 | + "token": "token", |
| 302 | + } |
| 303 | + ] |
| 304 | + assert capsys.readouterr().out == f"Uploaded {entries[0][1]}\n" |
0 commit comments