Add ci_report_hook/tests/test_core.py

This commit is contained in:
Mika 2026-01-28 16:22:44 +00:00
parent 3ee0c95611
commit 3760b6640d

View file

@ -0,0 +1,104 @@
import json
import os
from pathlib import Path
from datetime import datetime
import pytest
import src.ci_report_hook.core as core
def make_sample_report(tmp_path: Path):
run_id = "RUN123"
path = tmp_path / f"{run_id}.json"
report = core.CIReport(
timestamp=datetime.utcnow().isoformat(),
run_id=run_id,
decision="PASS",
margin=0.95,
flaky_flag=False,
subset_flip_count=1,
mischfenster_p95=0.87,
)
return report, path
def test_cireport_to_json_types(tmp_path):
report, _ = make_sample_report(tmp_path)
data = report.to_json()
expected_fields = [
"timestamp",
"run_id",
"decision",
"margin",
"flaky_flag",
"subset_flip_count",
"mischfenster_p95",
]
for field in expected_fields:
assert field in data, f"Missing field {field}"
assert isinstance(data["timestamp"], str)
assert isinstance(data["run_id"], str)
assert data["decision"] in {"PASS", "WARN", "FAIL"}
assert isinstance(data["margin"], float)
assert isinstance(data["flaky_flag"], bool)
assert isinstance(data["subset_flip_count"], int)
assert isinstance(data["mischfenster_p95"], float)
def test_record_ci_run_creates_valid_json(tmp_path, monkeypatch):
# prepare directories
reports_dir = tmp_path / "reports"
reports_dir.mkdir()
monkeypatch.chdir(tmp_path)
run_id = "RUN456"
core.record_ci_run(
run_id=run_id,
decision="WARN",
margin=0.12,
flaky_flag=True,
subset_flip_count=2,
mischfenster_p95=0.83,
)
target = reports_dir / f"{run_id}.json"
assert target.exists(), "Report file should be created."
with target.open("r", encoding="utf-8") as f:
data = json.load(f)
# simple field checks
assert data["run_id"] == run_id
assert data["decision"] == "WARN"
assert pytest.approx(data["margin"], 0.12)
assert data["flaky_flag"] is True
assert data["subset_flip_count"] == 2
assert pytest.approx(data["mischfenster_p95"], 0.83)
# timestamp field sanity
assert isinstance(data["timestamp"], str) and len(data["timestamp"]) >= 19
def test_record_ci_run_overwrites_existing(tmp_path, monkeypatch):
reports_dir = tmp_path / "reports"
reports_dir.mkdir()
monkeypatch.chdir(tmp_path)
run_id = "RUN789"
path = reports_dir / f"{run_id}.json"
path.write_text(json.dumps({"dummy": 1}))
core.record_ci_run(
run_id=run_id,
decision="FAIL",
margin=0.0,
flaky_flag=False,
subset_flip_count=0,
mischfenster_p95=0.0,
)
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
assert data["decision"] == "FAIL"
assert "dummy" not in data