From 661503c2f001f5a81bd6187863eb7d1a62d7b1d5 Mon Sep 17 00:00:00 2001 From: Mika Date: Fri, 6 Feb 2026 15:31:49 +0000 Subject: [PATCH] Add policy_eval_script/tests/test_core.py --- policy_eval_script/tests/test_core.py | 73 +++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 policy_eval_script/tests/test_core.py diff --git a/policy_eval_script/tests/test_core.py b/policy_eval_script/tests/test_core.py new file mode 100644 index 0000000..d3bf38c --- /dev/null +++ b/policy_eval_script/tests/test_core.py @@ -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}' \ No newline at end of file