constructelligence commited on
Commit
c8b0601
·
verified ·
1 Parent(s): 2b35862

Upload test_run_registry.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. test_run_registry.py +62 -0
test_run_registry.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Tests for run_registry.py (stdlib only). Run: python3 test_run_registry.py"""
3
+ import json
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+ import run_registry as rr
8
+
9
+
10
+ def test_append_chains_and_verifies():
11
+ with tempfile.TemporaryDirectory() as folder:
12
+ registry = Path(folder) / "runs.jsonl"
13
+ first = rr.append(registry, "train", dataset_id="pv1:abc", arch="segformer_b2",
14
+ config={"epochs": 60}, metrics={"val_mean_iou": 0.5})
15
+ second = rr.append(registry, "eval", dataset_id="pv1:abc", metrics={"mIoU": 0.51})
16
+ assert first["seq"] == 0 and first["prev_hash"] == rr.GENESIS
17
+ assert second["seq"] == 1 and second["prev_hash"] == first["hash"]
18
+ ok, report = rr.verify(registry)
19
+ assert ok and report["records"] == 2 and report["head"] == second["hash"]
20
+
21
+
22
+ def test_tampering_is_detected():
23
+ with tempfile.TemporaryDirectory() as folder:
24
+ registry = Path(folder) / "runs.jsonl"
25
+ rr.append(registry, "train", dataset_id="pv1:abc", metrics={"val_mean_iou": 0.5})
26
+ rr.append(registry, "eval", metrics={"mIoU": 0.51})
27
+ lines = registry.read_text().splitlines()
28
+ record = json.loads(lines[0])
29
+ record["metrics"]["val_mean_iou"] = 0.99 # quietly inflate a past metric
30
+ lines[0] = json.dumps(record, sort_keys=True)
31
+ registry.write_text("\n".join(lines) + "\n")
32
+ ok, report = rr.verify(registry)
33
+ assert not ok and report["reason"] == "record tampered" and report["at"] == 0
34
+
35
+
36
+ def test_deleting_a_record_breaks_the_chain():
37
+ with tempfile.TemporaryDirectory() as folder:
38
+ registry = Path(folder) / "runs.jsonl"
39
+ for index in range(3):
40
+ rr.append(registry, "train", metrics={"i": index})
41
+ lines = registry.read_text().splitlines()
42
+ registry.write_text("\n".join([lines[0], lines[2]]) + "\n")
43
+ ok, report = rr.verify(registry)
44
+ assert not ok and report["reason"] == "sequence gap"
45
+
46
+
47
+ def test_empty_registry_verifies():
48
+ with tempfile.TemporaryDirectory() as folder:
49
+ ok, report = rr.verify(Path(folder) / "missing.jsonl")
50
+ assert ok and report["records"] == 0
51
+
52
+
53
+ def main():
54
+ tests = [value for name, value in sorted(globals().items()) if name.startswith("test_")]
55
+ for test in tests:
56
+ test()
57
+ print(f"ok {test.__name__}")
58
+ print(f"{len(tests)} tests passed")
59
+
60
+
61
+ if __name__ == "__main__":
62
+ main()