Skip to content

Commit 50dfaae

Browse files
committed
tests(regression): replace hardcoded system list with auto-discovered test cases
1 parent a2177f5 commit 50dfaae

2 files changed

Lines changed: 58 additions & 50 deletions

File tree

‎tests/regression/cases.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from dataclasses import dataclass
2+
from pathlib import Path
3+
import pytest
4+
5+
6+
@dataclass(frozen=True)
7+
class RegressionCase:
8+
system: str
9+
config_path: Path
10+
baseline_path: Path
11+
12+
13+
def repo_root() -> Path:
14+
return Path(__file__).resolve().parents[2]
15+
16+
17+
def discover_cases():
18+
configs_root = repo_root() / "tests/regression/configs"
19+
baselines_root = repo_root() / "tests/regression/baselines"
20+
21+
cases = []
22+
23+
for system_dir in sorted(configs_root.iterdir()):
24+
if not system_dir.is_dir():
25+
continue
26+
27+
system = system_dir.name
28+
config_path = system_dir / "config.yaml"
29+
baseline_path = baselines_root / f"{system}.json"
30+
31+
if not config_path.exists():
32+
continue
33+
34+
cases.append(
35+
pytest.param(
36+
RegressionCase(system, config_path, baseline_path),
37+
id=system,
38+
marks=pytest.mark.slow if system != "dna" else (),
39+
)
40+
)
41+
42+
return cases

‎tests/regression/test_regression.py‎

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,11 @@
88
import pytest
99

1010
from tests.regression.helpers import run_codeentropy_with_config
11+
from tests.regression.cases import discover_cases
1112

1213

1314
def _group_index(payload: dict[str, Any]) -> dict[str, dict[str, Any]]:
14-
"""Return the groups mapping from a regression payload.
15-
16-
Args:
17-
payload: Parsed JSON payload.
18-
19-
Returns:
20-
Mapping of group id to group data.
21-
22-
Raises:
23-
TypeError: If payload["groups"] is not a dict.
24-
"""
15+
"""Return the groups mapping from a regression payload."""
2516
groups = payload.get("groups", {})
2617
if not isinstance(groups, dict):
2718
raise TypeError("payload['groups'] must be a dict")
@@ -35,17 +26,7 @@ def _compare_grouped(
3526
rtol: float,
3627
atol: float,
3728
) -> None:
38-
"""Compare grouped regression outputs against baseline values.
39-
40-
Args:
41-
got_payload: Newly produced payload.
42-
baseline_payload: Baseline payload.
43-
rtol: Relative tolerance.
44-
atol: Absolute tolerance.
45-
46-
Raises:
47-
AssertionError: If any required group/component differs from baseline.
48-
"""
29+
"""Compare grouped regression outputs against baseline values."""
4930
got_groups = _group_index(got_payload)
5031
base_groups = _group_index(baseline_payload)
5132

@@ -100,50 +81,35 @@ def _compare_grouped(
10081

10182

10283
@pytest.mark.regression
103-
@pytest.mark.parametrize(
104-
"system",
105-
[
106-
"dna",
107-
pytest.param("benzaldehyde", marks=pytest.mark.slow),
108-
pytest.param("benzene", marks=pytest.mark.slow),
109-
pytest.param("cyclohexane", marks=pytest.mark.slow),
110-
pytest.param("ethyl-acetate", marks=pytest.mark.slow),
111-
pytest.param("methane", marks=pytest.mark.slow),
112-
pytest.param("methanol", marks=pytest.mark.slow),
113-
pytest.param("octonol", marks=pytest.mark.slow),
114-
pytest.param("water", marks=pytest.mark.slow),
115-
],
116-
)
84+
@pytest.mark.parametrize("case", discover_cases())
11785
def test_regression_matches_baseline(
118-
tmp_path: Path, system: str, request: pytest.FixtureRequest
86+
tmp_path: Path, case, request: pytest.FixtureRequest
11987
) -> None:
120-
"""Run a regression test for one system and compare to its baseline.
121-
122-
Args:
123-
tmp_path: Pytest-provided temporary directory.
124-
system: System name parameter.
125-
request: Pytest request fixture for reading CLI options.
126-
"""
127-
repo_root = Path(__file__).resolve().parents[2]
128-
config_path = (
129-
repo_root / "tests" / "regression" / "configs" / system / "config.yaml"
130-
)
131-
baseline_path = repo_root / "tests" / "regression" / "baselines" / f"{system}.json"
88+
"""Run a regression test for one system and compare to its baseline."""
89+
system = case.system
90+
config_path = case.config_path
91+
baseline_path = case.baseline_path
13292

13393
assert config_path.exists(), f"Missing config: {config_path}"
13494
assert baseline_path.exists(), f"Missing baseline: {baseline_path}"
13595

13696
baseline_payload = json.loads(baseline_path.read_text())
137-
run = run_codeentropy_with_config(workdir=tmp_path, config_src=config_path)
97+
98+
run = run_codeentropy_with_config(
99+
workdir=tmp_path,
100+
config_src=config_path,
101+
)
138102

139103
if request.config.getoption("--codeentropy-debug"):
140104
print("\n[CodeEntropy regression debug]")
105+
print("system:", system)
141106
print("workdir:", run.workdir)
142107
print("job_dir:", run.job_dir)
143108
print("output_json:", run.output_json)
144109
print("payload copy saved:", run.workdir / "codeentropy_output.json")
145110

146111
if request.config.getoption("--update-baselines"):
112+
baseline_path.parent.mkdir(parents=True, exist_ok=True)
147113
baseline_path.write_text(json.dumps(run.payload, indent=2))
148114
pytest.skip(f"Baseline updated for {system}: {baseline_path}")
149115

0 commit comments

Comments
 (0)