Add retry_tail_analysis/tests/test_core.py

This commit is contained in:
Mika 2026-03-24 11:10:15 +00:00
parent e93f453f68
commit cc4124a786

View file

@ -0,0 +1,51 @@
import json
import pytest
from pathlib import Path
import src.retry_tail_analysis.core as core
@pytest.fixture
def sample_experiment_data():
return [
core.ExperimentData(run_id="run1", retry_tailp99=120.5, threshold=150.0),
core.ExperimentData(run_id="run2", retry_tailp99=130.0, threshold=150.0),
core.ExperimentData(run_id="run3", retry_tailp99=110.0, threshold=150.0)
]
def test_analyze_retry_tail_basic(sample_experiment_data):
result = core.analyze_retry_tail(sample_experiment_data)
expected = (120.5 + 130.0 + 110.0) / 3
assert pytest.approx(result, rel=1e-6) == expected
def test_analyze_retry_tail_empty_list():
with pytest.raises(ValueError):
core.analyze_retry_tail([])
def test_experiment_data_fields():
data = core.ExperimentData(run_id="xyz", retry_tailp99=101.1, threshold=120.0)
assert data.run_id == "xyz"
assert isinstance(data.retry_tailp99, float)
assert data.threshold == 120.0
def test_json_parsing(tmp_path):
sample = [
{"run_id": "a", "retry_tailp99": 10.0, "threshold": 15.0},
{"run_id": "b", "retry_tailp99": 20.0, "threshold": 25.0}
]
fpath = tmp_path / "sample.json"
fpath.write_text(json.dumps(sample), encoding="utf-8")
with fpath.open() as f:
data_objects = [core.ExperimentData(**entry) for entry in json.load(f)]
assert all(isinstance(obj, core.ExperimentData) for obj in data_objects)
def test_threshold_exceed_detection(sample_experiment_data):
# Überschreite Schwelle bei einem Wert, prüfe ob Aggregation korrekt bleibt
sample_experiment_data[0].retry_tailp99 = 200.0
result = core.analyze_retry_tail(sample_experiment_data)
expected = (200.0 + 130.0 + 110.0) / 3
assert pytest.approx(result, rel=1e-6) == expected