#!/usr/bin/env python3 # -*- coding: utf-8 -*- """test_run_all.py -- fail-closed integrity tests for the top-level driver. run_all.py aggregates the three engines' exit codes. These tests prove, against the REAL engines and REAL PDG-2024 data, that the driver: * exits 0 (and prints "OVERALL: PASS") when every engine passes; and * exits NON-ZERO (and prints "OVERALL: FAIL") when ANY engine fails -- here exercised by injecting one quantum-number-inconsistent row (charge != sum of constituents) into a throwaway copy of the data, which must make qn_check fail and the driver fail closed. The driver is invoked as a real subprocess via its documented CLI, so the test exercises exactly what an operator runs. Nothing here is a trivially-true stub. Run: python -m pytest tests/test_run_all.py -q (from .) """ from __future__ import annotations import shutil import subprocess import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] DATA = ROOT / "data" CONFIG = ROOT / "relations_config.json" SECTIONS = Path("/sections") FOUNDATION = Path("/foundation") def _run_all(data_dir: Path, out_dir: Path) -> subprocess.CompletedProcess: return subprocess.run( [sys.executable, str(ROOT / "run_all.py"), "--data", str(data_dir), "--out", str(out_dir), "--config", str(CONFIG), "--sections", str(SECTIONS)], capture_output=True, text=True, ) def _skip_if_inputs_absent() -> None: if not SECTIONS.is_dir() or not FOUNDATION.is_dir(): pytest.skip(f"section/foundation inputs not present: {SECTIONS}") if not list(DATA.glob("dataset_*.csv")): pytest.skip("no dataset_*.csv present") def test_run_all_exits_zero_when_all_pass(tmp_path): """All three engines pass on the unmodified real data => driver exits 0 + 'OVERALL: PASS'.""" _skip_if_inputs_absent() proc = _run_all(DATA, tmp_path / "out") assert proc.returncode == 0, ( "run_all must exit 0 when every engine passes\n" + proc.stdout + proc.stderr) assert "OVERALL: PASS" in proc.stdout # every per-engine verdict line is a PASS assert "[FAIL]" not in proc.stdout for engine in ("qn_check", "mass_relations", "coverage_check"): assert f"[PASS] {engine}" in proc.stdout def test_run_all_exits_nonzero_when_an_engine_fails(tmp_path): """FAIL-CLOSED: inject one charge!=sum-of-constituents row into a copy of the data. qn_check must fail and the driver must propagate a NON-ZERO exit code + 'OVERALL: FAIL'.""" _skip_if_inputs_absent() data = tmp_path / "data" shutil.copytree(DATA, data) # 'uud' sums to charge +1; claim charge 7 => qn_check MUST fail closed. header = ("pdg_name,quark_content,charge_Q,J,P,C,isospin_I,I3,baryon_B," "strangeness_S,charm_C,bottom_Bprime,mass_MeV,mass_unc_MeV," "status_stars,sector,chunk,pdg_source") bad = ("DELIBERATELY_BROKEN,uud,7,1/2,+,,1/2,1/2,1,0,0,0,938.272,0.0001,4," "test_injected,T,run-all-fail-closed-test") (data / "dataset_INJECTED_BROKEN.csv").write_text( header + "\n" + bad + "\n", encoding="utf-8") proc = _run_all(data, tmp_path / "out") assert proc.returncode != 0, ( "run_all MUST exit non-zero when an engine fails (fail-closed)\n" + proc.stdout + proc.stderr) assert "OVERALL: FAIL" in proc.stdout assert "[FAIL] qn_check" in proc.stdout if __name__ == "__main__": sys.exit(pytest.main([__file__, "-q"]))