Add max_alert_logging/tests/test_core.py

This commit is contained in:
Mika 2026-03-13 16:22:59 +00:00
parent 15fbd84b32
commit c40828e561

View file

@ -0,0 +1,81 @@
import pytest
import json
from pathlib import Path
from typing import Any, Dict
import src.max_alert_logging.core as core
def load_test_data() -> list[Dict[str, Any]]:
data_path = Path(__file__).parent / 'data' / 'test_logs.json'
assert data_path.exists(), f"Missing test data file: {data_path}"
with open(data_path, 'r', encoding='utf-8') as f:
data = json.load(f)
assert isinstance(data, list), "Expected a list of log entries"
return data
@pytest.fixture(scope='module')
def sample_entries():
return load_test_data()
def test_log_alert_nominal(sample_entries):
entry = sample_entries[0]
result = core.log_alert(
corr_id=entry['corr_id'],
stratum=entry['stratum'],
job_parallelism=entry['job_parallelism'],
expires_at_dist_hours=entry['expires_at_dist_hours'],
t_gate_read=entry['t_gate_read'],
t_index_visible=entry['t_index_visible'],
retry_taken=entry['retry_taken'],
retry_total_overhead_ms=entry['retry_total_overhead_ms'],
policy_hash=entry['policy_hash'],
setup_fingerprint=entry['setup_fingerprint']
)
assert isinstance(result, bool)
assert result is True
def test_log_alert_duplicate(sample_entries):
# Logging same entry twice should return False on second try
entry = sample_entries[0]
first = core.log_alert(**entry)
second = core.log_alert(**entry)
assert first is True
assert second is False
def test_log_alert_invalid_types():
# Invalid param type should raise TypeError or AssertionError
with pytest.raises((TypeError, AssertionError)):
core.log_alert(
corr_id=123,
stratum='alpha',
job_parallelism='not_int',
expires_at_dist_hours=1.0,
t_gate_read=2.0,
t_index_visible=3.0,
retry_taken=1,
retry_total_overhead_ms=100.0,
policy_hash='abc123',
setup_fingerprint='fp1'
)
def test_log_alert_edge_cases():
result = core.log_alert(
corr_id='edgecase1',
stratum='',
job_parallelism=0,
expires_at_dist_hours=0.0,
t_gate_read=0.0,
t_index_visible=0.0,
retry_taken=0,
retry_total_overhead_ms=0.0,
policy_hash='',
setup_fingerprint=''
)
assert isinstance(result, bool)
assert result is True