Add trace_agg/tests/test_core.py

This commit is contained in:
Mika 2026-01-19 12:48:36 +00:00
parent 622d6c5037
commit 553c674b6d

View file

@ -0,0 +1,62 @@
import pytest
from trace_agg import core
import math
class TestCoreFunctions:
def test_compute_spearman_correlation_nominal(self):
retry_counts = [1, 2, 3, 4, 5]
window_durations = [2.0, 4.0, 6.0, 8.0, 10.0]
result = core.compute_spearman_correlation(retry_counts, window_durations)
assert isinstance(result, float)
# Spearman correlation for perfectly monotonic relationship should be 1.0
assert math.isclose(result, 1.0, rel_tol=1e-6)
def test_compute_spearman_correlation_inverse(self):
retry_counts = [1, 2, 3, 4, 5]
window_durations = [10.0, 8.0, 6.0, 4.0, 2.0]
result = core.compute_spearman_correlation(retry_counts, window_durations)
assert math.isclose(result, -1.0, rel_tol=1e-6)
def test_compute_spearman_correlation_invalid_inputs(self):
with pytest.raises(ValueError):
core.compute_spearman_correlation([1, 2], [3.0])
def test_calculate_edit_distance_identical_sequences(self):
seq = ["a", "b", "c"]
score = core.calculate_edit_distance(seq, seq)
assert score == 0.0
def test_calculate_edit_distance_completely_different(self):
seq1 = ["a", "b", "c"]
seq2 = ["x", "y", "z"]
score = core.calculate_edit_distance(seq1, seq2)
assert isinstance(score, float)
assert score == pytest.approx(3.0)
def test_calculate_edit_distance_partial_difference(self):
seq1 = ["a", "b", "c"]
seq2 = ["a", "x", "c"]
score = core.calculate_edit_distance(seq1, seq2)
assert score == pytest.approx(1.0)
def test_classify_runs_nominal(self):
run_data = [
{"run_id": "run1", "retry_free_reads": 10, "window_duration": 50.0},
{"run_id": "run2", "retry_free_reads": 20, "window_duration": 60.0}
]
result = core.classify_runs(run_data)
assert isinstance(result, list)
assert len(result) == 2
for r in result:
assert set(r.keys()) == {"run_id", "step_stability_score", "correlation_coefficient"}
assert isinstance(r["run_id"], str)
assert isinstance(r["step_stability_score"], float)
assert isinstance(r["correlation_coefficient"], float)
def test_classify_runs_invalid_input_type(self):
with pytest.raises((AssertionError, TypeError, ValueError)):
core.classify_runs("not_a_list")
def test_classify_runs_edge_empty(self):
result = core.classify_runs([])
assert result == []