From cc4124a7862ea2ad0bc56fe4da8a9e927c61ba47 Mon Sep 17 00:00:00 2001 From: Mika Date: Tue, 24 Mar 2026 11:10:15 +0000 Subject: [PATCH] Add retry_tail_analysis/tests/test_core.py --- retry_tail_analysis/tests/test_core.py | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 retry_tail_analysis/tests/test_core.py diff --git a/retry_tail_analysis/tests/test_core.py b/retry_tail_analysis/tests/test_core.py new file mode 100644 index 0000000..5f6f6a2 --- /dev/null +++ b/retry_tail_analysis/tests/test_core.py @@ -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 \ No newline at end of file