Add decision_rule_generation/tests/test_core.py
This commit is contained in:
parent
db16e71848
commit
fd1925ad07
1 changed files with 66 additions and 0 deletions
66
decision_rule_generation/tests/test_core.py
Normal file
66
decision_rule_generation/tests/test_core.py
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
import json
|
||||||
|
import pandas as pd
|
||||||
|
import pytest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import decision_rule_generation.core as core
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_grid_df():
|
||||||
|
data = [
|
||||||
|
{ 'phase': 'pinned', 'grace_minutes': 5, 'delay_seconds': 60, 'coverage_percent': 92.0, 'unknown_rate_percent': 1.2, 'worst_case_delay': 5.0 },
|
||||||
|
{ 'phase': 'pinned', 'grace_minutes': 10, 'delay_seconds': 30, 'coverage_percent': 93.5, 'unknown_rate_percent': 0.8, 'worst_case_delay': 4.0 },
|
||||||
|
{ 'phase': 'unpinned', 'grace_minutes': 5, 'delay_seconds': 60, 'coverage_percent': 88.5, 'unknown_rate_percent': 1.5, 'worst_case_delay': 6.0 },
|
||||||
|
{ 'phase': 'unpinned', 'grace_minutes': 8, 'delay_seconds': 40, 'coverage_percent': 90.0, 'unknown_rate_percent': 1.0, 'worst_case_delay': 5.5 }
|
||||||
|
]
|
||||||
|
return pd.DataFrame(data)
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_decision_rules_valid_df(sample_grid_df):
|
||||||
|
rules = core.generate_decision_rules(sample_grid_df)
|
||||||
|
assert isinstance(rules, dict)
|
||||||
|
assert 'policy_pinned' in rules
|
||||||
|
assert 'policy_unpinned' in rules
|
||||||
|
pinned = rules['policy_pinned']
|
||||||
|
unpinned = rules['policy_unpinned']
|
||||||
|
assert isinstance(pinned, dict)
|
||||||
|
assert isinstance(unpinned, dict)
|
||||||
|
assert set(pinned.keys()) >= {'grace_minutes', 'delay_seconds'}
|
||||||
|
assert set(unpinned.keys()) >= {'grace_minutes', 'delay_seconds'}
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_decision_rules_from_csv(tmp_path, sample_grid_df):
|
||||||
|
csv_path = tmp_path / 'grid_results.csv'
|
||||||
|
sample_grid_df.to_csv(csv_path, index=False)
|
||||||
|
rules = core.generate_decision_rules(str(csv_path))
|
||||||
|
assert isinstance(rules, dict)
|
||||||
|
assert 'policy_pinned' in rules and 'policy_unpinned' in rules
|
||||||
|
assert rules['policy_pinned']['grace_minutes'] in sample_grid_df['grace_minutes'].values
|
||||||
|
assert rules['policy_unpinned']['delay_seconds'] in sample_grid_df['delay_seconds'].values
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_policy_constants(tmp_path, sample_grid_df):
|
||||||
|
rules = core.generate_decision_rules(sample_grid_df)
|
||||||
|
json_path = tmp_path / 'policy_constants.json'
|
||||||
|
# initialize file
|
||||||
|
json_path.write_text(json.dumps({'old_key': 'preserved'}))
|
||||||
|
|
||||||
|
core.update_policy_constants(rules, str(json_path))
|
||||||
|
with open(json_path, 'r', encoding='utf-8') as f:
|
||||||
|
updated = json.load(f)
|
||||||
|
# Must contain previous and new keys
|
||||||
|
assert 'old_key' in updated
|
||||||
|
assert 'policy_pinned' in updated
|
||||||
|
assert 'policy_unpinned' in updated
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_decision_rules_with_invalid_input():
|
||||||
|
with pytest.raises((ValueError, TypeError)):
|
||||||
|
core.generate_decision_rules(123)
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_policy_constants_invalid_file_path(tmp_path, sample_grid_df):
|
||||||
|
rules = core.generate_decision_rules(sample_grid_df)
|
||||||
|
invalid_path = tmp_path / 'nonexisting_dir' / 'policy_constants.json'
|
||||||
|
with pytest.raises(IOError):
|
||||||
|
core.update_policy_constants(rules, str(invalid_path))
|
||||||
Loading…
Reference in a new issue