Add policy_eval_script/tests/test_core.py
This commit is contained in:
parent
1a1e66701e
commit
661503c2f0
1 changed files with 73 additions and 0 deletions
73
policy_eval_script/tests/test_core.py
Normal file
73
policy_eval_script/tests/test_core.py
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
import json
|
||||||
|
import hashlib
|
||||||
|
import pytest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Modul unter Test
|
||||||
|
def _import_evaluate_policy():
|
||||||
|
import importlib.util
|
||||||
|
import sys
|
||||||
|
core_path = Path('src/policy_eval_script/core.py')
|
||||||
|
spec = importlib.util.spec_from_file_location('policy_eval_script.core', core_path)
|
||||||
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
sys.modules['policy_eval_script.core'] = module
|
||||||
|
spec.loader.exec_module(module)
|
||||||
|
return module.evaluate_policy
|
||||||
|
|
||||||
|
@pytest.fixture(scope='module')
|
||||||
|
def evaluate_policy():
|
||||||
|
return _import_evaluate_policy()
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_policy_constants(tmp_path):
|
||||||
|
constants = {"alpha_threshold": 0.05, "beta": 0.1}
|
||||||
|
path = tmp_path / 'policy_constants.json'
|
||||||
|
path.write_text(json.dumps(constants))
|
||||||
|
return path, constants
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_drift_report():
|
||||||
|
return {
|
||||||
|
'stratum_A': {'drift_score': 0.03},
|
||||||
|
'stratum_B': {'drift_score': 0.08}
|
||||||
|
}
|
||||||
|
|
||||||
|
def _expected_hash(constants: dict) -> str:
|
||||||
|
const_str = json.dumps(constants, sort_keys=True)
|
||||||
|
return hashlib.sha256(const_str.encode('utf-8')).hexdigest()
|
||||||
|
|
||||||
|
def test_evaluate_policy_pass_case(evaluate_policy, sample_drift_report, sample_policy_constants, monkeypatch):
|
||||||
|
_, constants = sample_policy_constants
|
||||||
|
policy_hash = _expected_hash(constants)
|
||||||
|
|
||||||
|
monkeypatch.setattr('src.policy_eval_script.core._load_policy_constants', lambda: constants)
|
||||||
|
result = evaluate_policy(sample_drift_report)
|
||||||
|
|
||||||
|
assert isinstance(result, dict)
|
||||||
|
assert set(result.keys()) >= {'stratum', 'decision', 'reason', 'policy_hash'}
|
||||||
|
assert result['policy_hash'] == policy_hash
|
||||||
|
assert result['decision'] in {'PASS', 'WARN', 'FAIL'}
|
||||||
|
assert isinstance(result['reason'], str)
|
||||||
|
|
||||||
|
def test_evaluate_policy_fail_case(evaluate_policy, sample_policy_constants, monkeypatch):
|
||||||
|
report = {'stratum_C': {'drift_score': 0.2}}
|
||||||
|
_, constants = sample_policy_constants
|
||||||
|
constants['alpha_threshold'] = 0.05
|
||||||
|
|
||||||
|
monkeypatch.setattr('src.policy_eval_script.core._load_policy_constants', lambda: constants)
|
||||||
|
result = evaluate_policy(report)
|
||||||
|
|
||||||
|
assert result['decision'] == 'FAIL'
|
||||||
|
assert 'drift_score' in result['reason']
|
||||||
|
|
||||||
|
def test_invalid_input_validation(evaluate_policy):
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
evaluate_policy('not_a_dict')
|
||||||
|
|
||||||
|
def test_evaluate_policy_structure_is_complete(evaluate_policy, sample_drift_report, sample_policy_constants, monkeypatch):
|
||||||
|
_, constants = sample_policy_constants
|
||||||
|
monkeypatch.setattr('src.policy_eval_script.core._load_policy_constants', lambda: constants)
|
||||||
|
|
||||||
|
result = evaluate_policy(sample_drift_report)
|
||||||
|
for field in ('stratum', 'decision', 'reason', 'policy_hash'):
|
||||||
|
assert field in result, f'Missing field {field}'
|
||||||
Loading…
Reference in a new issue