Add stability_analysis/tests/test_core.py
This commit is contained in:
parent
a4e9e98165
commit
919997538d
1 changed files with 57 additions and 0 deletions
57
stability_analysis/tests/test_core.py
Normal file
57
stability_analysis/tests/test_core.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import pytest
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from stability_analysis.core import analyze_data
|
||||
|
||||
@pytest.fixture
|
||||
def sample_data():
|
||||
return [
|
||||
{"expires_at_dist_hours": 1.5, "retry_total_overhead_ms": 10},
|
||||
{"expires_at_dist_hours": 1.6, "retry_total_overhead_ms": 11},
|
||||
{"expires_at_dist_hours": 5.2, "retry_total_overhead_ms": 150}, # outlier
|
||||
{"expires_at_dist_hours": 1.4, "retry_total_overhead_ms": 9},
|
||||
{"expires_at_dist_hours": 1.5, "retry_total_overhead_ms": 10}
|
||||
]
|
||||
|
||||
|
||||
def test_analyze_data_returns_expected_structure(sample_data):
|
||||
result = analyze_data(sample_data)
|
||||
assert isinstance(result, dict)
|
||||
for key in ("stable_cluster", "outlier_counts", "patterns"):
|
||||
assert key in result, f"Missing key: {key}"
|
||||
|
||||
assert isinstance(result["stable_cluster"], list)
|
||||
assert isinstance(result["outlier_counts"], dict)
|
||||
assert isinstance(result["patterns"], list)
|
||||
|
||||
|
||||
def test_outlier_detection(sample_data):
|
||||
result = analyze_data(sample_data)
|
||||
assert any(count > 0 for count in result["outlier_counts"].values()), "Outliers should be detected"
|
||||
|
||||
|
||||
def test_stable_cluster_identification(sample_data):
|
||||
result = analyze_data(sample_data)
|
||||
assert len(result["stable_cluster"]) >= 1, "Should identify at least one stable cluster"
|
||||
|
||||
|
||||
def test_pattern_recognition(sample_data):
|
||||
result = analyze_data(sample_data)
|
||||
# Should return some patterns based on grouped similarities
|
||||
assert isinstance(result["patterns"], list)
|
||||
assert all(isinstance(p, str) for p in result["patterns"])
|
||||
|
||||
|
||||
def test_invalid_input_raises():
|
||||
with pytest.raises((TypeError, ValueError)):
|
||||
analyze_data(None)
|
||||
with pytest.raises((TypeError, ValueError)):
|
||||
analyze_data([{"expires_at_dist_hours": "NaN"}])
|
||||
|
||||
|
||||
def test_edge_case_empty_input():
|
||||
result = analyze_data([])
|
||||
assert isinstance(result, dict)
|
||||
assert result["stable_cluster"] == []
|
||||
assert result["outlier_counts"] == {}
|
||||
assert result["patterns"] == []
|
||||
Loading…
Reference in a new issue