Spaces:
Sleeping
Sleeping
Download code/tests/test_metrics_export.py from acra-thesis/acra-backend: direct link, hf CLI and curl.
- Browser
- Download file 6.74 kB
-
https://huggingface.co/spaces/acra-thesis/acra-backend/resolve/main/code/tests/test_metrics_export.py
- Command line
-
hf download hf://spaces/acra-thesis/acra-backend/code/tests/test_metrics_export.py
-
curl -L -o test_metrics_export.py https://huggingface.co/spaces/acra-thesis/acra-backend/resolve/main/code/tests/test_metrics_export.py
6.74 kB
| """Export keeps failed images and images that had nothing to resolve.""" | |
| import csv | |
| import io | |
| import json | |
| from metrics_export import metric_row, to_csv, to_json | |
| def _failed(): | |
| return { | |
| "job_id": "fail-1", | |
| "created_at": "2026-10-12T01:00:00+00:00", | |
| "cvd_type": "deutan", | |
| "severity": 0.8, | |
| "metrics": { | |
| "de_before_mean": 15.4, | |
| "de_after_mean": 28.3, | |
| "delta_e_improvement": 12.9, | |
| "conflict_resolution_rate": 0.5, | |
| "conflicts_found": 2, | |
| "n_conflicts_resolved": 1, | |
| "naturalness_preservation": 2.8, | |
| "already_accessible": False, | |
| "pass_de_improvement": False, | |
| "pass_resolution_rate": False, | |
| "pass_naturalness": True, | |
| "conflict_diagnostics": { | |
| "resolved_pairs": [[1, 2]], | |
| "unresolved_pairs": [[3, 4]], | |
| "new_pairs": [[4, 5]], | |
| }, | |
| }, | |
| } | |
| def _no_conflict(): | |
| return { | |
| "job_id": "clear-1", | |
| "filename": "poster.png", | |
| "created_at": "2026-10-12T02:00:00+00:00", | |
| "cvd_type": "deutan", | |
| "severity": 1.0, | |
| "metrics": { | |
| "de_before_mean": 0.0, | |
| "de_after_mean": 0.0, | |
| "delta_e_improvement": 0.0, | |
| "conflict_resolution_rate": 1.0, | |
| "conflicts_found": 0, | |
| "n_conflicts_resolved": 0, | |
| "naturalness_preservation": 0.0, | |
| "already_accessible": True, | |
| "pass_de_improvement": True, | |
| "pass_resolution_rate": True, | |
| "pass_naturalness": True, | |
| "conflict_diagnostics": { | |
| "resolved_pairs": [], | |
| "unresolved_pairs": [], | |
| "new_pairs": [], | |
| }, | |
| }, | |
| } | |
| def test_failed_and_no_conflict_rows_both_export(): | |
| rows = [metric_row("job", _failed()), metric_row("job", _no_conflict())] | |
| parsed = list(csv.DictReader(io.StringIO(to_csv(rows)))) | |
| assert [r["id"] for r in parsed] == ["fail-1", "clear-1"] | |
| failed = parsed[0] | |
| assert failed["de_before"] == "15.4" | |
| assert failed["de_after"] == "28.3" | |
| assert failed["conflicts_resolved"] == "1" | |
| assert failed["new_conflicts"] == "1" | |
| assert failed["new_conflict_pairs"] == "4-5" | |
| assert failed["unresolved_pairs"] == "3-4" | |
| assert failed["naturalness"] == "2.8" | |
| assert failed["no_conflict"] == "false" | |
| assert failed["pass_de"] == "false" | |
| assert failed["pass_resolution"] == "false" | |
| assert failed["pass_naturalness"] == "true" | |
| clear = parsed[1] | |
| assert clear["filename"] == "poster.png" | |
| assert clear["conflicts_found"] == "0" | |
| assert clear["no_conflict"] == "true" | |
| assert clear["pass_de"] == "true" | |
| assert clear["pass_resolution"] == "true" | |
| assert clear["new_conflicts"] == "0" | |
| def test_json_keeps_numbers_and_flags(): | |
| payload = json.loads(to_json([metric_row("test_run", _no_conflict())])) | |
| assert payload[0]["source"] == "test_run" | |
| assert payload[0]["no_conflict"] is True | |
| assert payload[0]["de_before"] == 0.0 | |
| assert payload[0]["pass_naturalness"] is True | |
| def test_csv_filename_is_text_not_a_formula(): | |
| row = metric_row("job", {**_failed(), "filename": '=HYPERLINK("example")'}) | |
| parsed = list(csv.DictReader(io.StringIO(to_csv([row])))) | |
| assert parsed[0]["filename"] == '\'=HYPERLINK("example")' | |
| assert json.loads(to_json([row]))[0]["filename"] == '=HYPERLINK("example")' | |
| def test_export_routes_auth_ownership_expiry_and_all_verdicts(monkeypatch, tmp_path, request): | |
| import main | |
| import local_store | |
| import jwt | |
| from datetime import datetime, timedelta, timezone | |
| from fastapi.testclient import TestClient | |
| from concurrent.futures import ThreadPoolExecutor | |
| pool = ThreadPoolExecutor(max_workers=2) | |
| monkeypatch.setattr(main, "_executor", pool) | |
| request.addfinalizer(pool.shutdown) | |
| _, db = local_store.init_paths(tmp_path) | |
| monkeypatch.setattr(main, "DB_PATH", db) | |
| monkeypatch.setattr(main, "USE_LOCAL", True) | |
| monkeypatch.setattr(main, "SKIP_AUTH", False) | |
| monkeypatch.setattr(main, "SUPABASE_URL", "") | |
| monkeypatch.setattr(main, "SUPABASE_JWT_SECRET", "test-only-export-signing-key-with-32-characters") | |
| now = datetime.now(timezone.utc) | |
| def headers(user): | |
| token = jwt.encode({"sub": user, "exp": now + timedelta(minutes=5)}, main.SUPABASE_JWT_SECRET, algorithm="HS256") | |
| return {"Authorization": "Bearer " + token} | |
| def insert(identifier, owner, metrics, expired=False): | |
| local_store.insert_job(db, { | |
| "job_id": identifier, "user_id": owner, "cvd_type": "deutan", "severity": 0.8, | |
| "conf_threshold": 0.45, "use_segmentation": 1, "seg_soft": 8, | |
| "n_clusters": None, "seg_clusters_per_roi": None, | |
| "created_at": now.isoformat(), | |
| "expires_at": (now + timedelta(hours=-1 if expired else 1)).isoformat(), | |
| "original_url": "", "corrected_url": "", "metrics": metrics, "boxes": [], | |
| }) | |
| insert("failure", "alice", _failed()["metrics"]) | |
| insert("clear", "alice", _no_conflict()["metrics"]) | |
| insert("other", "bob", _failed()["metrics"]) | |
| insert("expired", "alice", _failed()["metrics"], expired=True) | |
| client = TestClient(main.app) | |
| assert client.get("/metrics/export").status_code == 401 | |
| assert client.get("/jobs/other/metrics", headers=headers("alice")).status_code == 404 | |
| assert client.get("/jobs/expired/metrics", headers=headers("alice")).status_code == 404 | |
| response = client.get("/metrics/export?format=json", headers=headers("alice")) | |
| assert response.status_code == 200 | |
| rows = {row["id"]: row for row in response.json()} | |
| assert set(rows) == {"failure", "clear"} | |
| assert rows["failure"]["pass_resolution"] is False | |
| assert rows["clear"]["no_conflict"] is True | |
| assert client.get("/metrics/export?format=xml", headers=headers("alice")).status_code == 400 | |
| def test_remote_export_does_not_silently_stop_at_5000_rows(monkeypatch): | |
| import main | |
| class Query: | |
| def table(self, _): return self | |
| def select(self, _): return self | |
| def eq(self, _, value): | |
| assert value == "alice" | |
| return self | |
| def order(self, *args, **kwargs): return self | |
| def range(self, start, end): | |
| self.start, self.end = start, end | |
| return self | |
| def execute(self): | |
| from types import SimpleNamespace | |
| return SimpleNamespace(data=[{"job_id": i} for i in range(self.start, min(self.end + 1, 5001))]) | |
| monkeypatch.setattr(main, "_get_sb", lambda: Query()) | |
| assert len(main._remote_user_rows("jobs", "alice")) == 5001 | |